zaznaczanie pozycji w menu
[wolnelektury.git] / src / catalogue / views.py
index 17b46cc..b39bb87 100644 (file)
@@ -38,6 +38,7 @@ def catalogue(request):
         'books': Book.objects.filter(parent=None).order_by('sort_key_author', 'sort_key'),
         'pictures': Picture.objects.order_by('sort_key_author', 'sort_key'),
         'collections': Collection.objects.all(),
         'books': Book.objects.filter(parent=None).order_by('sort_key_author', 'sort_key'),
         'pictures': Picture.objects.order_by('sort_key_author', 'sort_key'),
         'collections': Collection.objects.all(),
+        'active_menu_item': 'all_works',
     })
 
 
     })
 
 
@@ -107,9 +108,11 @@ def differentiate_tags(request, tags, ambiguous_slugs):
 
 
 # TODO: Rewrite this hellish piece of code which tries to do everything
 
 
 # TODO: Rewrite this hellish piece of code which tries to do everything
-def tagged_object_list(request, tags='', gallery=False):
+def tagged_object_list(request, tags='', list_type='books'):
     raw_tags = tags
     # preliminary tests and conditions
     raw_tags = tags
     # preliminary tests and conditions
+    gallery = list_type == 'gallery'
+    audiobooks = list_type == 'audiobooks'
     try:
         tags = Tag.get_tag_list(tags)
     except Tag.DoesNotExist:
     try:
         tags = Tag.get_tag_list(tags)
     except Tag.DoesNotExist:
@@ -134,14 +137,16 @@ def tagged_object_list(request, tags='', gallery=False):
         pass
 
     # beginning of digestion
         pass
 
     # beginning of digestion
-    theme_is_set = [tag for tag in tags if tag.category == 'theme']
-    shelf_is_set = [tag for tag in tags if tag.category == 'set']
+    theme_is_set = any(tag.category == 'theme' for tag in tags)
+    shelf_is_set = any(tag.category == 'set' for tag in tags)
     only_shelf = shelf_is_set and len(tags) == 1
     only_shelf = shelf_is_set and len(tags) == 1
-    only_my_shelf = only_shelf and request.user.is_authenticated() and request.user == tags[0].user
+    only_my_shelf = only_shelf and request.user == tags[0].user
     tags_pks = [tag.pk for tag in tags]
 
     tags_pks = [tag.pk for tag in tags]
 
-    objects = None
+    if gallery and shelf_is_set:
+        raise Http404
 
 
+    daisy = None
     if theme_is_set:
         # Only fragments (or pirctureareas) here.
         shelf_tags = [tag for tag in tags if tag.category == 'set']
     if theme_is_set:
         # Only fragments (or pirctureareas) here.
         shelf_tags = [tag for tag in tags if tag.category == 'set']
@@ -152,12 +157,9 @@ def tagged_object_list(request, tags='', gallery=False):
             fragments = Fragment.tagged.with_all(fragment_tags)
 
         if shelf_tags:
             fragments = Fragment.tagged.with_all(fragment_tags)
 
         if shelf_tags:
-            if gallery:
-                # TODO: Pictures on shelves not supported yet.
-                raise Http404
-            else:
-                books = Book.tagged.with_all(shelf_tags).order_by()
-                fragments = fragments.filter(Q(book__in=books) | Q(book__ancestor__in=books))
+            # TODO: Pictures on shelves not supported yet.
+            books = Book.tagged.with_all(shelf_tags).order_by()
+            fragments = fragments.filter(Q(book__in=books) | Q(book__ancestor__in=books))
 
         categories = split_tags(
             Tag.objects.usage_for_queryset(fragments, counts=True).exclude(pk__in=tags_pks),
 
         categories = split_tags(
             Tag.objects.usage_for_queryset(fragments, counts=True).exclude(pk__in=tags_pks),
@@ -166,14 +168,11 @@ def tagged_object_list(request, tags='', gallery=False):
         objects = fragments
     else:
         if gallery:
         objects = fragments
     else:
         if gallery:
-            if shelf_is_set:
-                # TODO: Pictures on shelves not supported yet.
-                raise Http404
+            # TODO: Pictures on shelves not supported yet.
+            if tags:
+                objects = Picture.tagged.with_all(tags)
             else:
             else:
-                if tags:
-                    objects = Picture.tagged.with_all(tags)
-                else:
-                    objects = Picture.objects.all()
+                objects = Picture.objects.all()
             areas = PictureArea.objects.filter(picture__in=objects)
             categories = split_tags(
                 Tag.objects.usage_for_queryset(
             areas = PictureArea.objects.filter(picture__in=objects)
             categories = split_tags(
                 Tag.objects.usage_for_queryset(
@@ -184,22 +183,32 @@ def tagged_object_list(request, tags='', gallery=False):
                     pk__in=tags_pks),
             )
         else:
                     pk__in=tags_pks),
             )
         else:
-            if tags:
-                all_books = Book.tagged.with_all(tags)
-            else:
-                all_books = Book.objects.filter(parent=None)
-            if shelf_is_set:
+            if audiobooks:
+                all_books = Book.objects.filter(media__type__in=('mp3', 'ogg')).distinct()
+                if tags:
+                    all_books = Book.tagged.with_all(tags, all_books)
                 objects = all_books
                 objects = all_books
+                # there's never only the daisy audiobook
+                daisy = objects.filter(media__type='daisy').distinct().order_by('sort_key_author', 'sort_key')
                 related_book_tags = Tag.objects.usage_for_queryset(
                     objects, counts=True).exclude(
                     category='set').exclude(pk__in=tags_pks)
             else:
                 if tags:
                 related_book_tags = Tag.objects.usage_for_queryset(
                     objects, counts=True).exclude(
                     category='set').exclude(pk__in=tags_pks)
             else:
                 if tags:
-                    objects = Book.tagged_top_level(tags)
+                    all_books = Book.tagged.with_all(tags)
                 else:
                 else:
+                    all_books = Book.objects.filter(parent=None)
+                if shelf_is_set:
                     objects = all_books
                     objects = all_books
-                # WTF: was outside if, overwriting value assigned if shelf_is_set
-                related_book_tags = get_top_level_related_tags(tags)
+                    related_book_tags = Tag.objects.usage_for_queryset(
+                        objects, counts=True).exclude(
+                        category='set').exclude(pk__in=tags_pks)
+                else:
+                    if tags:
+                        objects = Book.tagged_top_level(tags)
+                    else:
+                        objects = all_books
+                    related_book_tags = get_top_level_related_tags(tags)
 
             fragments = Fragment.objects.filter(book__in=all_books)
 
 
             fragments = Fragment.objects.filter(book__in=all_books)
 
@@ -219,9 +228,16 @@ def tagged_object_list(request, tags='', gallery=False):
 
     if not gallery and not objects and len(tags) == 1:
         tag = tags[0]
 
     if not gallery and not objects and len(tags) == 1:
         tag = tags[0]
-        if (tag.category in ('theme', 'thing') and PictureArea.tagged.with_any([tag]).exists() or
+        if tag.category in ('theme', 'thing') and (
+                PictureArea.tagged.with_any([tag]).exists() or
                 Picture.tagged.with_any([tag]).exists()):
                 Picture.tagged.with_any([tag]).exists()):
-            return redirect('tagged_object_list_gallery', raw_tags, permanent=False)
+            return redirect('tagged_object_list_gallery', raw_tags)
+
+    # this is becoming more and more hacky
+    if list_type == 'books' and not tags:
+        last_published = Book.objects.exclude(cover_thumb='').filter(parent=None).order_by('-created_at')[:20]
+    else:
+        last_published = None
 
     return render_to_response(
         'catalogue/tagged_object_list.html',
 
     return render_to_response(
         'catalogue/tagged_object_list.html',
@@ -235,7 +251,10 @@ def tagged_object_list(request, tags='', gallery=False):
             'tag_ids': tags_pks,
             'theme_is_set': theme_is_set,
             'best': best,
             'tag_ids': tags_pks,
             'theme_is_set': theme_is_set,
             'best': best,
-            'gallery': gallery,
+            'list_type': list_type,
+            'daisy': daisy,
+            'last_published': last_published,
+            'active_menu_item': 'theme' if theme_is_set else list_type,
         },
         context_instance=RequestContext(request))
 
         },
         context_instance=RequestContext(request))
 
@@ -742,6 +761,7 @@ def tag_catalogue(request, category):
         'best': best,
         'title': constants.CATEGORIES_NAME_PLURAL[category],
         'whole_category': constants.WHOLE_CATEGORY[category],
         'best': best,
         'title': constants.CATEGORIES_NAME_PLURAL[category],
         'whole_category': constants.WHOLE_CATEGORY[category],
+        'active_menu_item': 'theme' if category == 'theme' else None,
     })
 
 
     })