Add chunk under forms.
[wolnelektury.git] / src / catalogue / views.py
index 2c6692f..38c6165 100644 (file)
@@ -5,7 +5,6 @@ from collections import OrderedDict
 import random
 
 from django.conf import settings
 import random
 
 from django.conf import settings
-from django.http.response import HttpResponseForbidden
 from django.template.loader import render_to_string
 from django.shortcuts import get_object_or_404, render, redirect
 from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect
 from django.template.loader import render_to_string
 from django.shortcuts import get_object_or_404, render, redirect
 from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect
@@ -18,7 +17,8 @@ from django.utils.translation import ugettext as _, ugettext_lazy
 from django.views.decorators.cache import never_cache
 
 from ajaxable.utils import AjaxableFormView
 from django.views.decorators.cache import never_cache
 
 from ajaxable.utils import AjaxableFormView
-from club.models import Membership
+from club.forms import ScheduleForm
+from club.models import Club, Membership
 from annoy.models import DynamicTextInsert
 from pdcounter import views as pdcounter_views
 from picture.models import Picture, PictureArea
 from annoy.models import DynamicTextInsert
 from pdcounter import views as pdcounter_views
 from picture.models import Picture, PictureArea
@@ -28,16 +28,15 @@ from catalogue.helpers import get_top_level_related_tags
 from catalogue.models import Book, Collection, Tag, Fragment
 from catalogue.utils import split_tags
 from catalogue.models.tag import prefetch_relations
 from catalogue.models import Book, Collection, Tag, Fragment
 from catalogue.utils import split_tags
 from catalogue.models.tag import prefetch_relations
-from wolnelektury.utils import is_crawler
 
 staff_required = user_passes_test(lambda user: user.is_staff)
 
 
 def catalogue(request):
     return render(request, 'catalogue/catalogue.html', {
 
 staff_required = user_passes_test(lambda user: user.is_staff)
 
 
 def catalogue(request):
     return render(request, 'catalogue/catalogue.html', {
-        'books': Book.objects.filter(parent=None),
+        'books': Book.objects.filter(findable=True, parent=None),
         'pictures': Picture.objects.all(),
         'pictures': Picture.objects.all(),
-        'collections': Collection.objects.all(),
+        'collections': Collection.objects.filter(listed=True),
         'active_menu_item': 'all_works',
     })
 
         'active_menu_item': 'all_works',
     })
 
@@ -67,7 +66,10 @@ def daisy_list(request):
 
 def collection(request, slug):
     coll = get_object_or_404(Collection, slug=slug)
 
 def collection(request, slug):
     coll = get_object_or_404(Collection, slug=slug)
-    return render(request, 'catalogue/collection.html', {'collection': coll})
+    return render(request, 'catalogue/collection.html', {
+        'collection': coll,
+        'active_menu_item': 'collections',
+    })
 
 
 def differentiate_tags(request, tags, ambiguous_slugs):
 
 
 def differentiate_tags(request, tags, ambiguous_slugs):
@@ -146,7 +148,7 @@ def object_list(request, objects, fragments=None, related_tags=None, tags=None,
 
 
 def literature(request):
 
 
 def literature(request):
-    books = Book.objects.filter(parent=None)
+    books = Book.objects.filter(parent=None, findable=True)
     return object_list(request, books, related_tags=get_top_level_related_tags([]))
 
 
     return object_list(request, books, related_tags=get_top_level_related_tags([]))
 
 
@@ -155,9 +157,9 @@ def gallery(request):
 
 
 def audiobooks(request):
 
 
 def audiobooks(request):
-    audiobooks = Book.objects.filter(media__type__in=('mp3', 'ogg')).distinct()
+    audiobooks = Book.objects.filter(findable=True, media__type__in=('mp3', 'ogg')).distinct()
     return object_list(request, audiobooks, list_type='audiobooks', extra={
     return object_list(request, audiobooks, list_type='audiobooks', extra={
-        'daisy': Book.objects.filter(media__type='daisy').distinct(),
+        'daisy': Book.objects.filter(findable=True, media__type='daisy').distinct(),
     })
 
 
     })
 
 
@@ -205,6 +207,8 @@ def theme_list(request, tags, list_type):
         # 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))
         # 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))
+    elif list_type == 'books':
+        fragments = fragments.filter(book__findable=True)
 
     if not fragments and len(tags) == 1 and list_type == 'books':
         if PictureArea.tagged.with_any(tags).exists() or Picture.tagged.with_any(tags).exists():
 
     if not fragments and len(tags) == 1 and list_type == 'books':
         if PictureArea.tagged.with_any(tags).exists() or Picture.tagged.with_any(tags).exists():
@@ -222,9 +226,6 @@ def tagged_object_list(request, tags, list_type):
     except ResponseInstead as e:
         return e.response
 
     except ResponseInstead as e:
         return e.response
 
-    if is_crawler(request) and len(tags) > 1:
-        return HttpResponseForbidden('address removed from crawling. check robots.txt')
-
     if list_type == 'gallery' and any(tag.category == 'set' for tag in tags):
         raise Http404
 
     if list_type == 'gallery' and any(tag.category == 'set' for tag in tags):
         raise Http404
 
@@ -237,15 +238,16 @@ def tagged_object_list(request, tags, list_type):
         if any(tag.category == 'set' for tag in tags):
             params = {'objects': books}
         else:
         if any(tag.category == 'set' for tag in tags):
             params = {'objects': books}
         else:
+            books = books.filter(findable=True)
             params = {
             params = {
-                'objects': Book.tagged_top_level(tags),
+                'objects': Book.tagged_top_level(tags).filter(findable=True),
                 'fragments': Fragment.objects.filter(book__in=books),
                 'related_tags': get_top_level_related_tags(tags),
             }
     elif list_type == 'gallery':
         params = {'objects': Picture.tagged.with_all(tags)}
     elif list_type == 'audiobooks':
                 'fragments': Fragment.objects.filter(book__in=books),
                 'related_tags': get_top_level_related_tags(tags),
             }
     elif list_type == 'gallery':
         params = {'objects': Picture.tagged.with_all(tags)}
     elif list_type == 'audiobooks':
-        audiobooks = Book.objects.filter(media__type__in=('mp3', 'ogg')).distinct()
+        audiobooks = Book.objects.filter(findable=True, media__type__in=('mp3', 'ogg')).distinct()
         params = {
             'objects': Book.tagged.with_all(tags, audiobooks),
             'extra': {
         params = {
             'objects': Book.tagged.with_all(tags, audiobooks),
             'extra': {
@@ -291,6 +293,8 @@ def book_detail(request, slug):
             'book': book,
             'book_children': book.children.all().order_by('parent_number', 'sort_key'),
             'active_menu_item': 'books',
             'book': book,
             'book_children': book.children.all().order_by('parent_number', 'sort_key'),
             'active_menu_item': 'books',
+            'club_form': ScheduleForm() if book.preview else None,
+            'club': Club.objects.first() if book.preview else None,
         })
 
 
         })
 
 
@@ -353,7 +357,7 @@ def import_book(request):
                 _("An error occurred: %(exception)s\n\n%(tb)s") % {
                     'exception': exception, 'tb': tb
                 },
                 _("An error occurred: %(exception)s\n\n%(tb)s") % {
                     'exception': exception, 'tb': tb
                 },
-                mimetype='text/plain'
+                content_type='text/plain'
             )
         return HttpResponse(_("Book imported successfully"))
     return HttpResponse(_("Error importing file: %r") % book_import_form.errors)
             )
         return HttpResponse(_("Book imported successfully"))
     return HttpResponse(_("Error importing file: %r") % book_import_form.errors)
@@ -451,16 +455,17 @@ def tag_catalogue(request, category):
 
 
 def collections(request):
 
 
 def collections(request):
-    objects = Collection.objects.all()
+    objects = Collection.objects.filter(listed=True)
 
     if len(objects) > 3:
 
     if len(objects) > 3:
-        best = random.sample(list(objects), 3)
+        best = random.sample(list(objects), 4)
     else:
         best = objects
 
     return render(request, 'catalogue/collections.html', {
         'objects': objects,
         'best': best,
     else:
         best = objects
 
     return render(request, 'catalogue/collections.html', {
         'objects': objects,
         'best': best,
+        'active_menu_item': 'collections'
     })
 
 
     })