Search widget with JSONP support. Sample included.
[wolnelektury.git] / apps / catalogue / views.py
index 0e146fd..e7df9bc 100644 (file)
@@ -8,6 +8,8 @@ import sys
 import pprint
 import traceback
 import re
+import itertools
+from operator import itemgetter
 
 from django.conf import settings
 from django.template import RequestContext
@@ -43,16 +45,27 @@ class LazyEncoder(simplejson.JSONEncoder):
             return force_unicode(obj)
         return obj
 
+# shortcut for JSON reponses
+class JSONResponse(HttpResponse):
+    def __init__(self, data={}, callback=None, **kwargs):
+        # get rid of mimetype
+        kwargs.pop('mimetype', None)
+        data = simplejson.dumps(data)
+        if callback:
+            data = callback + "(" + data + ");" 
+        super(JSONResponse, self).__init__(data, mimetype="application/json", **kwargs)
+
 
 def main_page(request):
     if request.user.is_authenticated():
         shelves = models.Tag.objects.filter(category='set', user=request.user)
         new_set_form = forms.NewSetForm()
-    extra_where = "NOT catalogue_tag.category = 'set'"
-    tags = models.Tag.objects.usage_for_model(models.Book, counts=True, extra={'where': [extra_where]})
-    fragment_tags = models.Tag.objects.usage_for_model(models.Fragment, counts=True,
-        extra={'where': ["catalogue_tag.category = 'theme'"] + [extra_where]})
+
+    tags = models.Tag.objects.exclude(category__in=('set', 'book'))
+    for tag in tags:
+        tag.count = tag.get_count()
     categories = split_tags(tags)
+    fragment_tags = categories.get('theme', [])
 
     form = forms.SearchForm()
     return render_to_response('catalogue/main_page.html', locals(),
@@ -71,24 +84,44 @@ def book_list(request):
         context_instance=RequestContext(request))
 
 
-def tagged_object_list(request, tags=''):
-    # Prevent DoS attacks on our database
-    if len(tags.split('/')) > 6:
-        raise Http404
+def differentiate_tags(request, tags, ambiguous_slugs):
+    beginning = '/'.join(tag.url_chunk for tag in tags)
+    unparsed = '/'.join(ambiguous_slugs[1:])
+    options = []
+    for tag in models.Tag.objects.exclude(category='book').filter(slug=ambiguous_slugs[0]):
+        options.append({
+            'url_args': '/'.join((beginning, tag.url_chunk, unparsed)).strip('/'),
+            'tags': [tag]
+        })
+    return render_to_response('catalogue/differentiate_tags.html',
+                {'tags': tags, 'options': options, 'unparsed': ambiguous_slugs[1:]},
+                context_instance=RequestContext(request))
 
+
+def tagged_object_list(request, tags=''):
     try:
         tags = models.Tag.get_tag_list(tags)
     except models.Tag.DoesNotExist:
         raise Http404
+    except models.Tag.MultipleObjectsReturned, e:
+        return differentiate_tags(request, e.tags, e.ambiguous_slugs)
+
+    try:
+        if len(tags) > settings.MAX_TAG_LIST:
+            raise Http404
+    except AttributeError:
+        pass
 
     if len([tag for tag in tags if tag.category == 'book']):
         raise Http404
 
     theme_is_set = [tag for tag in tags if tag.category == 'theme']
-    shelf_is_set = len(tags) == 1 and tags[0].category == 'set'
-    my_shelf_is_set = shelf_is_set and request.user.is_authenticated() and request.user == tags[0].user
+    shelf_is_set = [tag for tag in tags if tag.category == 'set']
+    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
 
-    objects = only_author = pd_counter = categories = None
+    objects = only_author = pd_counter = None
+    categories = {}
 
     if theme_is_set:
         shelf_tags = [tag for tag in tags if tag.category == 'set']
@@ -97,11 +130,11 @@ def tagged_object_list(request, tags=''):
 
         if shelf_tags:
             books = models.Book.tagged.with_all(shelf_tags).order_by()
-            l_tags = [models.Tag.objects.get(slug='l-' + book.slug) for book in books]
+            l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books])
             fragments = models.Fragment.tagged.with_any(l_tags, fragments)
 
         # newtagging goes crazy if we just try:
-        #related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True, 
+        #related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True,
         #                    extra={'where': ["catalogue_tag.category != 'book'"]})
         fragment_keys = [fragment.pk for fragment in fragments]
         if fragment_keys:
@@ -113,26 +146,30 @@ def tagged_object_list(request, tags=''):
 
             objects = fragments
     else:
-        books = models.Book.tagged.with_all(tags).order_by()
-        l_tags = [models.Tag.objects.get(slug='l-' + book.slug) for book in books]
-        book_keys = [book.pk for book in books]
-        # newtagging goes crazy if we just try:
-        #related_tags = models.Tag.objects.usage_for_queryset(books, counts=True, 
-        #                    extra={'where': ["catalogue_tag.category NOT IN ('set', 'book', 'theme')"]})
-        if book_keys:
-            related_tags = models.Book.tags.usage(counts=True,
-                                filters={'pk__in': book_keys},
-                                extra={'where': ["catalogue_tag.category NOT IN ('set', 'book', 'theme')"]})
-            categories = split_tags(related_tags)
-
-            fragment_keys = [fragment.pk for fragment in models.Fragment.tagged.with_any(l_tags)]
-            if fragment_keys:
-                categories['theme'] = models.Fragment.tags.usage(counts=True,
-                                    filters={'pk__in': fragment_keys},
-                                    extra={'where': ["catalogue_tag.category = 'theme'"]})
-
-            books = books.exclude(parent__in=book_keys)
-            objects = books
+        # get relevant books and their tags
+        objects = models.Book.tagged.with_all(tags).order_by()
+        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)
+
+        # get related tags from `tag_counter` and `theme_counter`
+        related_counts = {}
+        tags_pks = [tag.pk for tag in tags]
+        for book in objects:
+            for tag_pk, value in itertools.chain(book.tag_counter.iteritems(), book.theme_counter.iteritems()):
+                if tag_pk in tags_pks:
+                    continue
+                related_counts[tag_pk] = related_counts.get(tag_pk, 0) + value
+        related_tags = models.Tag.objects.filter(pk__in=related_counts.keys())
+        related_tags = [tag for tag in related_tags if tag not in tags]
+        for tag in related_tags:
+            tag.count = related_counts[tag.pk]
+
+        categories = split_tags(related_tags)
+        del related_tags
 
     if not objects:
         only_author = len(tags) == 1 and tags[0].category == 'author'
@@ -145,10 +182,10 @@ def tagged_object_list(request, tags=''):
         template_name='catalogue/tagged_object_list.html',
         extra_context={
             'categories': categories,
-            'shelf_is_set': shelf_is_set,
+            'only_shelf': only_shelf,
             'only_author': only_author,
             'pd_counter': pd_counter,
-            'user_is_owner': my_shelf_is_set,
+            'only_my_shelf': only_my_shelf,
             'formats_form': forms.DownloadFormatsForm(),
 
             'tags': tags,
@@ -158,8 +195,8 @@ def tagged_object_list(request, tags=''):
 
 def book_fragments(request, book_slug, theme_slug):
     book = get_object_or_404(models.Book, slug=book_slug)
-    book_tag = get_object_or_404(models.Tag, slug='l-' + book_slug)
-    theme = get_object_or_404(models.Tag, slug=theme_slug)
+    book_tag = get_object_or_404(models.Tag, slug='l-' + book_slug, category='book')
+    theme = get_object_or_404(models.Tag, slug=theme_slug, category='theme')
     fragments = models.Fragment.tagged.with_all([book_tag, theme])
 
     form = forms.SearchForm()
@@ -173,12 +210,16 @@ def book_detail(request, slug):
     except models.Book.DoesNotExist:
         return book_stub_detail(request, slug)
 
-    book_tag = get_object_or_404(models.Tag, slug='l-' + 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')
-    extra_where = "catalogue_tag.category = 'theme'"
-    book_themes = models.Tag.objects.related_for_model(book_tag, models.Fragment, counts=True, extra={'where': [extra_where]})
+
+    theme_counter = book.theme_counter
+    book_themes = models.Tag.objects.filter(pk__in=theme_counter.keys())
+    for tag in book_themes:
+        tag.count = theme_counter[tag.pk]
+
     extra_info = book.get_extra_info_value()
 
     form = forms.SearchForm()
@@ -214,7 +255,7 @@ def book_text(request, slug):
 
 def _no_diacritics_regexp(query):
     """ returns a regexp for searching for a query without diacritics
-    
+
     should be locale-aware """
     names = {
         u'a':u'aąĄ', u'c':u'cćĆ', u'e':u'eęĘ', u'l': u'lłŁ', u'n':u'nńŃ', u'o':u'oóÓ', u's':u'sśŚ', u'z':u'zźżŹŻ',
@@ -232,16 +273,16 @@ def unicode_re_escape(query):
 def _word_starts_with(name, prefix):
     """returns a Q object getting models having `name` contain a word
     starting with `prefix`
-    
+
     We define word characters as alphanumeric and underscore, like in JS.
-    
+
     Works for MySQL, PostgreSQL, Oracle.
     For SQLite, _sqlite* version is substituted for this.
     """
     kwargs = {}
 
     prefix = _no_diacritics_regexp(unicode_re_escape(prefix))
-    # can't use [[:<:]] (word start), 
+    # can't use [[:<:]] (word start),
     # but we want both `xy` and `(xy` to catch `(xyz)`
     kwargs['%s__iregex' % name] = u"(^|[^[:alnum:]_])%s" % prefix
 
@@ -249,8 +290,8 @@ def _word_starts_with(name, prefix):
 
 
 def _sqlite_word_starts_with(name, prefix):
-    """ version of _word_starts_with for SQLite 
-    
+    """ version of _word_starts_with for SQLite
+
     SQLite in Django uses Python re module
     """
     kwargs = {}
@@ -273,7 +314,6 @@ def _tags_starting_with(prefix, user=None):
         tags = tags.filter(~Q(category='book') & (~Q(category='set') | Q(user=user)))
     else:
         tags = tags.filter(~Q(category='book') & ~Q(category='set'))
-
     return list(books) + list(tags) + list(book_stubs)
 
 
@@ -282,7 +322,7 @@ def _get_result_link(match, tag_list):
         return match.get_absolute_url()
     else:
         return reverse('catalogue.views.tagged_object_list',
-            kwargs={'tags': '/'.join(tag.slug for tag in tag_list + [match])}
+            kwargs={'tags': '/'.join(tag.url_chunk for tag in tag_list + [match])}
         )
 
 def _get_result_type(match):
@@ -290,18 +330,18 @@ def _get_result_type(match):
         type = 'book'
     else:
         type = match.category
-    return dict(models.TAG_CATEGORIES)[type]
+    return type
 
 
 
 def find_best_matches(query, user=None):
     """ Finds a Book, Tag or Bookstub best matching a query.
-    
+
     Returns a with:
       - zero elements when nothing is found,
       - one element when a best result is found,
       - more then one element on multiple exact matches
-    
+
     Raises a ValueError on too short a query.
     """
 
@@ -348,8 +388,29 @@ def tags_starting_with(request):
     # Prefix must have at least 2 characters
     if len(prefix) < 2:
         return HttpResponse('')
-
-    return HttpResponse('\n'.join(tag.name for tag in _tags_starting_with(prefix, request.user)))
+    tags_list = []
+    result = ""   
+    for tag in _tags_starting_with(prefix, request.user):
+        if not tag.name in tags_list:
+            result += "\n" + tag.name
+            tags_list.append(tag.name)
+    return HttpResponse(result)
+
+def json_tags_starting_with(request, callback=None):
+    # Callback for JSONP
+    prefix = request.GET.get('q', '')
+    callback = request.GET.get('callback', '')
+    # Prefix must have at least 2 characters
+    if len(prefix) < 2:
+        return HttpResponse('')
+    tags_list = []
+    result = ""   
+    for tag in _tags_starting_with(prefix, request.user):
+        if not tag.name in tags_list:
+            result += "\n" + tag.name
+            tags_list.append(tag.name)
+    dict_result = {"matches": tags_list}
+    return JSONResponse(dict_result, callback)
 
 # ====================
 # = Shelf management =
@@ -378,11 +439,11 @@ def book_sets(request, slug):
             new_shelves = [models.Tag.objects.get(pk=id) for id in form.cleaned_data['set_ids']]
 
             for shelf in [shelf for shelf in old_shelves if shelf not in new_shelves]:
-                shelf.book_count -= 1
+                shelf.book_count = None
                 shelf.save()
 
             for shelf in [shelf for shelf in new_shelves if shelf not in old_shelves]:
-                shelf.book_count += 1
+                shelf.book_count = None
                 shelf.save()
 
             book.tags = new_shelves + list(book.tags.filter(~Q(category='set') | ~Q(user=request.user)))
@@ -408,7 +469,7 @@ def remove_from_shelf(request, shelf, book):
     if shelf in book.tags:
         models.Tag.objects.remove_tag(book, shelf)
 
-        shelf.book_count -= 1
+        shelf.book_count = None
         shelf.save()
 
         return HttpResponse(_('Book was successfully removed from the shelf'))
@@ -434,7 +495,7 @@ def download_shelf(request, slug):
     """"
     Create a ZIP archive on disk and transmit it in chunks of 8KB,
     without loading the whole file into memory. A similar approach can
-    be used for large dynamic PDF files.                                        
+    be used for large dynamic PDF files.
     """
     shelf = get_object_or_404(models.Tag, slug=slug, category='set')
 
@@ -443,7 +504,7 @@ def download_shelf(request, slug):
     if form.is_valid():
         formats = form.cleaned_data['formats']
     if len(formats) == 0:
-        formats = ['pdf', 'odt', 'txt', 'mp3', 'ogg']
+        formats = ['pdf', 'epub', 'odt', 'txt', 'mp3', 'ogg']
 
     # Create a ZIP archive
     temp = tempfile.TemporaryFile()
@@ -453,6 +514,9 @@ def download_shelf(request, slug):
         if 'pdf' in formats and book.pdf_file:
             filename = book.pdf_file.path
             archive.write(filename, str('%s.pdf' % book.slug))
+        if 'epub' in formats and book.epub_file:
+            filename = book.epub_file.path
+            archive.write(filename, str('%s.epub' % book.slug))
         if 'odt' in formats and book.odt_file:
             filename = book.odt_file.path
             archive.write(filename, str('%s.odt' % book.slug))
@@ -483,11 +547,13 @@ def shelf_book_formats(request, shelf):
     """
     shelf = get_object_or_404(models.Tag, slug=shelf, category='set')
 
-    formats = {'pdf': False, 'odt': False, 'txt': False, 'mp3': False, 'ogg': False}
+    formats = {'pdf': False, 'epub': False, 'odt': False, 'txt': False, 'mp3': False, 'ogg': False}
 
     for book in collect_books(models.Book.tagged.with_all(shelf)):
         if book.pdf_file:
             formats['pdf'] = True
+        if book.epub_file:
+            formats['epub'] = True
         if book.odt_file:
             formats['odt'] = True
         if book.txt_file:
@@ -583,7 +649,6 @@ def import_book(request):
             info = sys.exc_info()
             exception = pprint.pformat(info[1])
             tb = '\n'.join(traceback.format_tb(info[2]))
-            _('Today is %(month)s, %(day)s.') % {'month': m, 'day': d}
             return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
         return HttpResponse(_("Book imported successfully"))
     else: