media in api fix
[wolnelektury.git] / apps / catalogue / views.py
index 7addb7f..fd8a633 100644 (file)
@@ -17,7 +17,7 @@ from django.template import RequestContext
 from django.shortcuts import render_to_response, get_object_or_404
 from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect
 from django.core.urlresolvers import reverse
-from django.db.models import Q
+from django.db.models import Count, Sum, Q
 from django.contrib.auth.decorators import login_required, user_passes_test
 from django.utils.datastructures import SortedDict
 from django.views.decorators.http import require_POST
@@ -28,6 +28,7 @@ from django.utils.functional import Promise
 from django.utils.encoding import force_unicode
 from django.utils.http import urlquote_plus
 from django.views.decorators import cache
+from django.utils import translation
 from django.utils.translation import ugettext as _
 from django.views.generic.list_detail import object_list
 
@@ -83,7 +84,7 @@ def book_list(request, filter=None, template_name='catalogue/book_list.html'):
     form = forms.SearchForm()
 
     books_by_parent = {}
-    books = models.Book.objects.all().order_by('parent_number', 'title').only('title', 'parent', 'slug')
+    books = models.Book.objects.all().order_by('parent_number', 'sort_key').only('title', 'parent', 'slug')
     if filter:
         books = books.filter(filter).distinct()
         book_ids = set((book.pk for book in books))
@@ -119,15 +120,36 @@ def book_list(request, filter=None, template_name='catalogue/book_list.html'):
 
 
 def audiobook_list(request):
-    return book_list(request, Q(medias__type='mp3') | Q(medias__type='ogg'),
+    return book_list(request, Q(media__type='mp3') | Q(media__type='ogg'),
                      template_name='catalogue/audiobook_list.html')
 
 
 def daisy_list(request):
-    return book_list(request, Q(medias__type='daisy'),
+    return book_list(request, Q(media__type='daisy'),
                      template_name='catalogue/daisy_list.html')
 
 
+def counters(request):
+    form = forms.SearchForm()
+
+    books = models.Book.objects.count()
+    books_nonempty = models.Book.objects.exclude(html_file='').count()
+    books_empty = models.Book.objects.filter(html_file='').count()
+    books_root = models.Book.objects.filter(parent=None).count()
+
+    media = models.BookMedia.objects.count()
+    media_types = models.BookMedia.objects.values('type').\
+            annotate(count=Count('type')).\
+            order_by('type')
+    for mt in media_types:
+        mt['size'] = sum(b.file.size for b in models.BookMedia.objects.filter(type=mt['type']))
+        mt['deprecated'] = models.BookMedia.objects.filter(
+            type=mt['type'], source_sha1=None).count() if mt['type'] in ('mp3', 'ogg') else '-'
+
+    return render_to_response('catalogue/counters.html',
+                locals(), context_instance=RequestContext(request))
+
+
 def differentiate_tags(request, tags, ambiguous_slugs):
     beginning = '/'.join(tag.url_chunk for tag in tags)
     unparsed = '/'.join(ambiguous_slugs[1:])
@@ -196,14 +218,10 @@ def tagged_object_list(request, tags=''):
 
             objects = fragments
     else:
-        # get relevant books and their tags
-        objects = models.Book.tagged.with_all(tags)
-        if not shelf_is_set:
-            # eliminate descendants
-            l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in objects])
-            descendants_keys = [book.pk for book in models.Book.tagged.with_any(l_tags)]
-            if descendants_keys:
-                objects = objects.exclude(pk__in=descendants_keys)
+        if shelf_is_set:
+            objects = models.Book.tagged.with_all(tags)
+        else:
+            objects = models.Book.tagged_top_level(tags)
 
         # get related tags from `tag_counter` and `theme_counter`
         related_counts = {}
@@ -235,7 +253,6 @@ def tagged_object_list(request, tags=''):
             'only_author': only_author,
             'only_my_shelf': only_my_shelf,
             'formats_form': forms.DownloadFormatsForm(),
-
             'tags': tags,
         }
     )
@@ -261,7 +278,7 @@ def book_detail(request, slug):
     book_tag = book.book_tag()
     tags = list(book.tags.filter(~Q(category='set')))
     categories = split_tags(tags)
-    book_children = book.children.all().order_by('parent_number', 'title')
+    book_children = book.children.all().order_by('parent_number', 'sort_key')
     
     _book = book
     parents = []
@@ -277,6 +294,18 @@ def book_detail(request, slug):
 
     extra_info = book.get_extra_info_value()
 
+    projects = set()
+    for m in book.media.filter(type='mp3'):
+        # ogg files are always from the same project
+        meta = m.get_extra_info_value()
+        project = meta.get('project')
+        if not project:
+            # temporary fallback
+            project = u'CzytamySłuchając'
+
+        projects.add((project, meta.get('funded_by', '')))
+    projects = sorted(projects)
+
     form = forms.SearchForm()
     return render_to_response('catalogue/book_detail.html', locals(),
         context_instance=RequestContext(request))
@@ -770,3 +799,18 @@ def xmls(request):
     temp.seek(0)
     response.write(temp.read())
     return response
+
+
+
+# info views for API
+
+def book_info(request, id, lang='pl'):
+    book = get_object_or_404(models.Book, id=id)
+    # set language by hand
+    translation.activate(lang)
+    return render_to_response('catalogue/book_info.html', locals(),
+        context_instance=RequestContext(request))
+
+def tag_info(request, id):
+    tag = get_object_or_404(models.Tag, id=id)
+    return HttpResponse(tag.description)