#6 uzupełnione metadane (wg makiety)
[wolnelektury.git] / src / catalogue / views.py
index 084396b..271e796 100644 (file)
@@ -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)
-    pictures = Picture.objects.all()
-    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,20 +52,27 @@ 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):
-    books = models.Book.objects.filter(Q(media__type='mp3') | Q(media__type='ogg')).distinct()
+    books = models.Book.objects.filter(media__type__in=('mp3', 'ogg')).distinct().order_by(
+        'sort_key_author', 'sort_key')
     books = list(books)
     if len(books) > 3:
         best = random.sample(books, 3)
     else:
         best = books
 
-    daisy = models.Book.objects.filter(media__type='daisy').distinct()
+    daisy = models.Book.objects.filter(media__type='daisy').distinct().order_by('sort_key_author', 'sort_key')
 
     return render(request, 'catalogue/audiobook_list.html', {
         'books': books,
@@ -163,11 +171,9 @@ def tagged_object_list(request, tags='', gallery=False):
                 raise Http404
             else:
                 if tags:
-                    objects = Picture.tagged.with_all(tags).order_by(
-                        'sort_key_author', 'title')
+                    objects = Picture.tagged.with_all(tags)
                 else:
-                    objects = Picture.objects.all().order_by(
-                        'sort_key_author', 'title')
+                    objects = Picture.objects.all()
             areas = PictureArea.objects.filter(picture__in=objects)
             categories = split_tags(
                 models.Tag.objects.usage_for_queryset(
@@ -183,17 +189,17 @@ def tagged_object_list(request, tags='', gallery=False):
             else:
                 all_books = models.Book.objects.filter(parent=None)
             if shelf_is_set:
-                objects = all_books.order_by('sort_key_author', 'title')
+                objects = all_books
                 related_book_tags = models.Tag.objects.usage_for_queryset(
                     objects, counts=True).exclude(
                     category='set').exclude(pk__in=tags_pks)
             else:
                 if tags:
-                    objects = models.Book.tagged_top_level(tags).order_by(
-                        'sort_key_author', 'title')
+                    objects = models.Book.tagged_top_level(tags)
                 else:
-                    objects = all_books.order_by('sort_key_author', 'title')
-            related_book_tags = get_top_level_related_tags(tags)
+                    objects = all_books
+                # WTF: was outside if, overwriting value assigned if shelf_is_set
+                related_book_tags = get_top_level_related_tags(tags)
 
             fragments = models.Fragment.objects.filter(book__in=all_books)
 
@@ -203,6 +209,7 @@ def tagged_object_list(request, tags='', gallery=False):
                     fragments, counts=True).filter(
                     category='theme').exclude(pk__in=tags_pks),
             )
+        objects = objects.order_by('sort_key_author', 'sort_key')
 
     objects = list(objects)
     if len(objects) > 3:
@@ -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):
@@ -711,15 +730,18 @@ def tag_catalogue(request, category):
     else:
         tags = list(get_top_level_related_tags((), categories=(category,)))
 
-    if len(tags) > 3:
-        best = random.sample(tags, 3)
+    described_tags = [tag for tag in tags if tag.description]
+
+    if len(described_tags) > 4:
+        best = random.sample(described_tags, 4)
     else:
-        best = tags
+        best = described_tags
 
     return render(request, 'catalogue/tag_catalogue.html', {
         'tags': tags,
         'best': best,
         'title': constants.CATEGORIES_NAME_PLURAL[category],
+        'whole_category': constants.WHOLE_CATEGORY[category],
     })