From: Jan Szejko Date: Tue, 2 Feb 2016 14:53:13 +0000 (+0100) Subject: refactoring: replaced locals() by explicit dicts X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/90e4a87f6ebffca42088cb952281c40555d2c520?ds=sidebyside refactoring: replaced locals() by explicit dicts --- diff --git a/src/api/management/commands/mobileinit.py b/src/api/management/commands/mobileinit.py index b89fede9f..ed4d624f9 100755 --- a/src/api/management/commands/mobileinit.py +++ b/src/api/management/commands/mobileinit.py @@ -97,7 +97,7 @@ CREATE TABLE state (last_checked INTEGER); """ db.executescript(schema) - db.execute("INSERT INTO state VALUES (:last_checked)", locals()) + db.execute("INSERT INTO state VALUES (:last_checked)", {'last_checked': last_checked}) return db @@ -133,29 +133,30 @@ categories = {'author': 'autor', def add_book(db, book): - title = book.title if book.html_file: html_file = book.html_file.url html_file_size = book.html_file.size else: html_file = html_file_size = None - if book.cover: - cover = book.cover.url - else: - cover = None - parent = book.parent_id - parent_number = book.parent_number - sort_key = book.sort_key - size_str = pretty_size(html_file_size) - authors = ", ".join(t.name for t in book.tags.filter(category='author')) - db.execute(book_sql, locals()) + db.execute(book_sql, { + 'title': book.title, + 'cover': book.cover.url if book.cover else None, + 'html_file': html_file, + 'html_file_size': html_file_size, + 'parent': book.parent_id, + 'parent_number': book.parent_number, + 'sort_key': book.sort_key, + 'size_str': pretty_size(html_file_size), + 'authors': ", ".join(t.name for t in book.tags.filter(category='author')), + }) def add_tag(db, tag): - category = categories[tag.category] - name = tag.name - sort_key = tag.sort_key - books = Book.tagged_top_level([tag]) - book_ids = ','.join(str(b.id) for b in books) - db.execute(tag_sql, locals()) + book_ids = ','.join(str(book_id) for book_id in books.values_list('id', flat=True)) + db.execute(tag_sql, { + 'category': categories[tag.category], + 'name': tag.name, + 'sort_key': tag.sort_key, + 'book_ids': book_ids, + }) diff --git a/src/catalogue/templates/catalogue/book_wide.html b/src/catalogue/templates/catalogue/book_wide.html index 26ee55d71..aa78292a8 100644 --- a/src/catalogue/templates/catalogue/book_wide.html +++ b/src/catalogue/templates/catalogue/book_wide.html @@ -1,6 +1,6 @@ {% extends "catalogue/book_short.html" %} {% load i18n %} -{% load choose_fragment download_audio tag_list custom_pdf_link_li license_icon source_name from catalogue_tags %} +{% load choose_fragment download_audio custom_pdf_link_li license_icon source_name from catalogue_tags %} {% load choose_cite from social_tags %} {% load ssi_include from ssify %} diff --git a/src/catalogue/templatetags/catalogue_tags.py b/src/catalogue/templatetags/catalogue_tags.py index 23eeedacc..16d5de7a9 100644 --- a/src/catalogue/templatetags/catalogue_tags.py +++ b/src/catalogue/templatetags/catalogue_tags.py @@ -306,9 +306,9 @@ class CatalogueURLNode(Node): return reverse('book_list') -@register.inclusion_tag('catalogue/tag_list.html') +# @register.inclusion_tag('catalogue/tag_list.html') def tag_list(tags, choices=None, category=None, gallery=False): - print(tags, choices, category) + # print(tags, choices, category) if choices is None: choices = [] @@ -319,6 +319,8 @@ def tag_list(tags, choices=None, category=None, gallery=False): if len(tags) == 1 and category not in [t.category for t in choices]: one_tag = tags[0] + else: + one_tag = None if category is not None: other = Tag.objects.filter(category=category).exclude(pk__in=[t.pk for t in tags])\ @@ -326,8 +328,15 @@ def tag_list(tags, choices=None, category=None, gallery=False): # Filter out empty tags. ct = ContentType.objects.get_for_model(Picture if gallery else Book) other = other.filter(items__content_type=ct).distinct() + else: + other = [] - return locals() + return { + 'one_tag': one_tag, + 'choices': choices, + 'tags': tags, + 'other': other, + } @register.inclusion_tag('catalogue/inline_tag_list.html') @@ -337,7 +346,7 @@ def inline_tag_list(tags, choices=None, category=None, gallery=False): @register.inclusion_tag('catalogue/collection_list.html') def collection_list(collections): - return locals() + return {'collections': collections} @register.inclusion_tag('catalogue/book_info.html') @@ -351,7 +360,7 @@ def book_info(book): @register.inclusion_tag('catalogue/work-list.html', takes_context=True) def work_list(context, object_list): request = context.get('request') - return locals() + return {'object_list': object_list, 'request': request} @register.inclusion_tag('catalogue/plain_list.html', takes_context=True) @@ -372,7 +381,14 @@ def plain_list(context, object_list, with_initials=True, by_author=False, choice last_initial = initial names.append((obj.author_str() if by_author else initial, [])) names[-1][1].append(obj) - return locals() + return { + 'paged': paged, + 'names': names, + 'initial_blocks': initial_blocks, + 'book': book, + 'gallery': gallery, + 'choice': choice, + } # TODO: These are no longer just books. diff --git a/src/catalogue/views.py b/src/catalogue/views.py index 09f48c7d4..271e7967c 100644 --- a/src/catalogue/views.py +++ b/src/catalogue/views.py @@ -33,11 +33,12 @@ from catalogue.utils import split_tags staff_required = user_passes_test(lambda user: user.is_staff) -def catalogue(request, as_json=False): - books = models.Book.objects.filter(parent=None).order_by('sort_key_author', 'sort_key') - pictures = Picture.objects.order_by('sort_key_author', 'sort_key') - collections = models.Collection.objects.all() - return render(request, 'catalogue/catalogue.html', locals()) +def catalogue(request): + return render(request, 'catalogue/catalogue.html', { + 'books': models.Book.objects.filter(parent=None).order_by('sort_key_author', 'sort_key'), + 'pictures': Picture.objects.order_by('sort_key_author', 'sort_key'), + 'collections': models.Collection.objects.all(), + }) def book_list(request, filter=None, get_filter=None, template_name='catalogue/book_list.html', @@ -51,9 +52,15 @@ def book_list(request, filter=None, get_filter=None, template_name='catalogue/bo for tag in books_by_author: if books_by_author[tag]: books_nav.setdefault(tag.sort_key[0], []).append(tag) - rendered_nav = render_to_string(nav_template_name, locals()) - rendered_book_list = render_to_string(list_template_name, locals()) - return render_to_response(template_name, locals(), context_instance=RequestContext(request)) + # WTF: dlaczego nie include? + return render_to_response(template_name, { + 'rendered_nav': render_to_string(nav_template_name, {'books_nav': books_nav}), + 'rendered_book_list': render_to_string(list_template_name, { + 'books_by_author': books_by_author, + 'orphans': orphans, + 'books_by_parent': books_by_parent, + }) + }, context_instance=RequestContext(request)) def audiobook_list(request): @@ -239,7 +246,11 @@ def book_fragments(request, slug, theme_slug): fragments = models.Fragment.tagged.with_all([theme]).filter( Q(book=book) | Q(book__ancestor=book)) - return render_to_response('catalogue/book_fragments.html', locals(), context_instance=RequestContext(request)) + return render_to_response('catalogue/book_fragments.html', { + 'book': book, + 'theme': theme, + 'fragments': fragments, + }, context_instance=RequestContext(request)) def book_detail(request, slug): @@ -248,9 +259,11 @@ def book_detail(request, slug): except models.Book.DoesNotExist: return pdcounter_views.book_stub_detail(request, slug) - tags = book.tags.exclude(category__in=('set', 'theme')) - book_children = book.children.all().order_by('parent_number', 'sort_key') - return render_to_response('catalogue/book_detail.html', locals(), context_instance=RequestContext(request)) + return render_to_response('catalogue/book_detail.html', { + 'book': book, + 'tags': book.tags.exclude(category__in=('set', 'theme')), + 'book_children': book.children.all().order_by('parent_number', 'sort_key'), + }, context_instance=RequestContext(request)) def get_audiobooks(book): @@ -284,6 +297,7 @@ def get_audiobooks(book): return audiobooks, projects, have_oggs +# używane tylko do audiobook_tree, które jest używane tylko w snippets/audiobook_list.html, które nie jest używane def player(request, slug): book = get_object_or_404(models.Book, slug=slug) if not book.has_media('mp3'): @@ -291,9 +305,14 @@ def player(request, slug): audiobooks, projects, have_oggs = get_audiobooks(book) - extra_info = book.extra_info + # extra_info = book.extra_info - return render_to_response('catalogue/player.html', locals(), context_instance=RequestContext(request)) + return render_to_response('catalogue/player.html', { + 'book': book, + 'audiobook': '', + 'audiobooks': audiobooks, + 'projects': projects, + }, context_instance=RequestContext(request)) def book_text(request, slug): @@ -301,7 +320,7 @@ def book_text(request, slug): if not book.has_html_file(): raise Http404 - return render_to_response('catalogue/book_text.html', locals(), context_instance=RequestContext(request)) + return render_to_response('catalogue/book_text.html', {'book': book,}, context_instance=RequestContext(request)) # ========== @@ -561,7 +580,7 @@ def book_info(request, book_id, lang='pl'): book = get_object_or_404(models.Book, id=book_id) # set language by hand translation.activate(lang) - return render_to_response('catalogue/book_info.html', locals(), context_instance=RequestContext(request)) + return render_to_response('catalogue/book_info.html', {'book': book}, context_instance=RequestContext(request)) def tag_info(request, tag_id): diff --git a/src/infopages/views.py b/src/infopages/views.py index 443fbdab1..4d1731fa0 100644 --- a/src/infopages/views.py +++ b/src/infopages/views.py @@ -19,6 +19,10 @@ def infopage(request, slug): try: right_column = Template(page.right_column).render(rc) except TemplateSyntaxError: - left_column = '' + right_column = '' - return render_to_response('infopages/infopage.html', locals(), context_instance=RequestContext(request)) + return render_to_response('infopages/infopage.html', { + 'page': page, + 'left_column': left_column, + 'right_columns': right_column, + }, context_instance=RequestContext(request)) diff --git a/src/pdcounter/views.py b/src/pdcounter/views.py index eee6bfc7d..b8a685eb8 100644 --- a/src/pdcounter/views.py +++ b/src/pdcounter/views.py @@ -15,10 +15,16 @@ def book_stub_detail(request, slug): book = get_object_or_404(models.BookStub, slug=slug) if book.pd and not book.in_pd(): pd_counter = datetime(book.pd, 1, 1) + else: + pd_counter = None form = PublishingSuggestForm(initial={"books": u"%s — %s, \n" % (book.author, book.title)}) - return render_to_response('pdcounter/book_stub_detail.html', locals(), context_instance=RequestContext(request)) + return render_to_response('pdcounter/book_stub_detail.html', { + 'book': book, + 'pd_counter': pd_counter, + 'form': form, + }, context_instance=RequestContext(request)) @cache.never_cache @@ -26,7 +32,13 @@ def author_detail(request, slug): author = get_object_or_404(models.Author, slug=slug) if not author.alive(): pd_counter = datetime(author.goes_to_pd(), 1, 1) + else: + pd_counter = None form = PublishingSuggestForm(initial={"books": author.name + ", \n"}) - return render_to_response('pdcounter/author_detail.html', locals(), context_instance=RequestContext(request)) + return render_to_response('pdcounter/author_detail.html', { + 'author': author, + 'pd_counter': pd_counter, + 'form': form, + }, context_instance=RequestContext(request)) diff --git a/src/picture/templates/picture/picture_wide.html b/src/picture/templates/picture/picture_wide.html index 462e8b0e2..00260f55f 100644 --- a/src/picture/templates/picture/picture_wide.html +++ b/src/picture/templates/picture/picture_wide.html @@ -81,12 +81,12 @@

{% trans "See" %}