fix in publishing
[wolnelektury.git] / src / catalogue / views.py
index 084396b..c263608 100644 (file)
@@ -34,8 +34,8 @@ 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()
+    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())
 
@@ -57,14 +57,15 @@ def book_list(request, filter=None, get_filter=None, template_name='catalogue/bo
 
 
 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 +164,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 +182,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 +202,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:
@@ -321,7 +321,7 @@ def _no_diacritics_regexp(query):
 
     def repl(m):
         l = m.group()
-        return u"(%s)" % '|'.join(names[l])
+        return u"(?:%s)" % '|'.join(names[l])
 
     return re.sub(u'[%s]' % (u''.join(names.keys())), repl, query)
 
@@ -609,11 +609,14 @@ class CustomPDFFormView(AjaxableFormView):
 
 @ssi_included
 def book_mini(request, pk, with_link=True):
-    book = get_object_or_404(models.Book, pk=pk)
-    author_str = ", ".join(tag.name for tag in book.tags.filter(category='author'))
+    # book = get_object_or_404(models.Book, pk=pk)
+    try:
+        book = models.Book.objects.only('cover_thumb', 'title', 'language', 'slug').get(pk=pk)
+    except models.Book.DoesNotExist:
+        raise Http404
     return render(request, 'catalogue/book_mini_box.html', {
         'book': book,
-        'author_str': author_str,
+        'author': book.author_unicode(),
         'with_link': with_link,
         'show_lang': book.language_code() != settings.LANGUAGE_CODE,
     })
@@ -711,15 +714,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],
     })