Filter menu items
[wolnelektury.git] / apps / opds / views.py
index a51b5b1..23c320e 100644 (file)
@@ -2,8 +2,8 @@
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
-from base64 import b64encode
 import os.path
+from urlparse import urljoin
 
 from django.contrib.syndication.views import Feed
 from django.core.urlresolvers import reverse
@@ -15,8 +15,15 @@ from django.contrib.sites.models import Site
 
 from basicauth import logged_in_or_basicauth, factory_decorator
 from catalogue.models import Book, Tag
-from catalogue.views import books_starting_with
 
+from search.views import Search, SearchResult
+import operator
+import logging
+import re
+
+log = logging.getLogger('opds')
+
+from stats.utils import piwik_track
 
 _root_feeds = (
     {
@@ -58,7 +65,7 @@ _root_feeds = (
 
 
 def full_url(url):
-    return "http://%s%s" % (Site.objects.get_current().domain, url)
+    return urljoin("http://%s" % Site.objects.get_current().domain, url)
 
 
 class OPDSFeed(Atom1Feed):
@@ -84,7 +91,7 @@ class OPDSFeed(Atom1Feed):
                                 {u"href": reverse("opds_authors"),
                                  u"rel": u"start",
                                  u"type": u"application/atom+xml"})
-        handler.addQuickElement(u"link", None, 
+        handler.addQuickElement(u"link", None,
                                 {u"href": full_url(os.path.join(settings.STATIC_URL, "opensearch.xml")),
                                  u"rel": u"search",
                                  u"type": u"application/opensearchdescription+xml"})
@@ -180,19 +187,19 @@ class AcquisitionFeed(Feed):
             return u''
 
     def item_enclosure_url(self, book):
-        return full_url(book.root_ancestor.epub_file.url)
+        return full_url(book.epub_file.url) if book.epub_file else None
 
     def item_enclosure_length(self, book):
-        return book.root_ancestor.epub_file.size
-
+        return book.epub_file.size if book.epub_file else None
 
+@piwik_track
 class RootFeed(Feed):
     feed_type = OPDSFeed
     title = u'Wolne Lektury'
-    link = u'http://www.wolnelektury.pl/'
+    link = u'http://wolnelektury.pl/'
     description = u"Spis utworów na stronie http://WolneLektury.pl"
     author_name = u"Wolne Lektury"
-    author_link = u"http://www.wolnelektury.pl/"
+    author_link = u"http://wolnelektury.pl/"
 
     def items(self):
         return _root_feeds
@@ -206,13 +213,13 @@ class RootFeed(Feed):
     def item_description(self, item):
         return item['description']
 
-
+@piwik_track
 class ByCategoryFeed(Feed):
     feed_type = OPDSFeed
-    link = u'http://www.wolnelektury.pl/'
+    link = u'http://wolnelektury.pl/'
     description = u"Spis utworów na stronie http://WolneLektury.pl"
     author_name = u"Wolne Lektury"
-    author_link = u"http://www.wolnelektury.pl/"
+    author_link = u"http://wolnelektury.pl/"
 
     def get_object(self, request, category):
         feed = [feed for feed in _root_feeds if feed['category']==category]
@@ -227,7 +234,7 @@ class ByCategoryFeed(Feed):
         return feed['title']
 
     def items(self, feed):
-        return (tag for tag in Tag.objects.filter(category=feed['category']) if tag.get_count() > 0)
+        return Tag.objects.filter(category=feed['category']).exclude(book_count=0)
 
     def item_title(self, item):
         return item.name
@@ -238,7 +245,7 @@ class ByCategoryFeed(Feed):
     def item_description(self):
         return u''
 
-
+@piwik_track
 class ByTagFeed(AcquisitionFeed):
     def link(self, tag):
         return tag.get_absolute_url()
@@ -254,7 +261,7 @@ class ByTagFeed(AcquisitionFeed):
 
     def items(self, tag):
         books = Book.tagged.with_any([tag])
-        l_tags = Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books])
+        l_tags = Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books.iterator()])
         descendants_keys = [book.pk for book in Book.tagged.with_any(l_tags)]
         if descendants_keys:
             books = books.exclude(pk__in=descendants_keys)
@@ -263,12 +270,13 @@ class ByTagFeed(AcquisitionFeed):
 
 
 @factory_decorator(logged_in_or_basicauth())
+@piwik_track
 class UserFeed(Feed):
     feed_type = OPDSFeed
     link = u'http://www.wolnelektury.pl/'
     description = u"Półki użytkownika na stronie http://WolneLektury.pl"
     author_name = u"Wolne Lektury"
-    author_link = u"http://www.wolnelektury.pl/"
+    author_link = u"http://wolnelektury.pl/"
 
     def get_object(self, request):
         return request.user
@@ -277,7 +285,7 @@ class UserFeed(Feed):
         return u"Półki użytkownika %s" % user.username
 
     def items(self, user):
-        return (tag for tag in Tag.objects.filter(category='set', user=user) if tag.get_count() > 0)
+        return Tag.objects.filter(category='set', user=user).exclude(book_count=0)
 
     def item_title(self, item):
         return item.name
@@ -288,8 +296,12 @@ class UserFeed(Feed):
     def item_description(self):
         return u''
 
+# no class decorators in python 2.5
+#UserFeed = factory_decorator(logged_in_or_basicauth())(UserFeed)
+
 
 @factory_decorator(logged_in_or_basicauth())
+@piwik_track
 class UserSetFeed(AcquisitionFeed):
     def link(self, tag):
         return tag.get_absolute_url()
@@ -306,20 +318,127 @@ class UserSetFeed(AcquisitionFeed):
     def items(self, tag):
         return Book.tagged.with_any([tag])
 
+# no class decorators in python 2.5
+#UserSetFeed = factory_decorator(logged_in_or_basicauth())(UserSetFeed)
+
 
+@piwik_track
 class SearchFeed(AcquisitionFeed):
     description = u"Wyniki wyszukiwania na stronie WolneLektury.pl"
     title = u"Wyniki wyszukiwania"
-    
+
+    QUOTE_OR_NOT = r'(?:(?=["])"([^"]+)"|([^ ]+))'
+    INLINE_QUERY_RE = re.compile(
+        r"author:" + QUOTE_OR_NOT +
+        "|translator:" + QUOTE_OR_NOT +
+        "|title:" + QUOTE_OR_NOT +
+        "|categories:" + QUOTE_OR_NOT +
+        "|description:" + QUOTE_OR_NOT +
+        "|text:" + QUOTE_OR_NOT
+        )
+    MATCHES = {
+        'author': (0, 1),
+        'translator': (2, 3),
+        'title': (4, 5),
+        'categories': (6, 7),
+        'description': (8, 9),
+        'text': (10, 11),
+        }
+
+    PARAMS_TO_FIELDS = {
+        'author': 'authors',
+        'translator': 'translators',
+        #        'title': 'title',
+        'categories': 'tag_name_pl',
+        'description': 'text',
+        #        'text': 'text',
+        }
+
+    ATOM_PLACEHOLDER = re.compile(r"^{(atom|opds):\w+}$")
+
     def get_object(self, request):
-        return request.GET.get('q', '')
+        """
+        For OPDS 1.1 We should handle a query for search terms
+        and criteria provided either as opensearch or 'inline' query.
+        OpenSearch defines fields: atom:author, atom:contributor (treated as translator),
+        atom:title. Inline query provides author, title, categories (treated as book tags),
+        description (treated as content search terms).
+
+        if search terms are provided, we shall search for books
+        according to Hint information (from author & contributror & title).
+
+        but if search terms are empty, we should do a different search
+        (perhaps for is_book=True)
+
+        """
+
+        query = request.GET.get('q', '')
+
+        inline_criteria = re.findall(self.INLINE_QUERY_RE, query)
+        if inline_criteria:
+            remains = re.sub(self.INLINE_QUERY_RE, '', query)
+            remains = re.sub(r'[ \t]+', ' ', remains)
+
+            def get_criteria(criteria, name):
+                for c in criteria:
+                    for p in self.MATCHES[name]:
+                        if c[p]:
+                            if p % 2 == 0:
+                                return c[p].replace('+', ' ')
+                            return c[p]
+                return None
+
+            criteria = dict(map(
+                lambda cn: (cn, get_criteria(inline_criteria, cn)),
+                ['author', 'translator', 'title', 'categories',
+                 'description', 'text']))
+            query = remains
+            # empty query and text set case?
+            log.debug("Inline query = [%s], criteria: %s" % (query, criteria))
+        else:
+            def remove_dump_data(val):
+                """Some clients don't get opds placeholders and just send them."""
+                if self.ATOM_PLACEHOLDER.match(val):
+                    return ''
+                return val
+
+            criteria = dict([(cn, remove_dump_data(request.GET.get(cn, '')))
+                        for cn in self.MATCHES.keys()])
+            # query is set above.
+            log.debug("Inline query = [%s], criteria: %s" % (query, criteria))
+
+        srch = Search()
+
+        book_hit_filter = srch.index.Q(book_id__any=True)
+        filters = [book_hit_filter] + [srch.index.Q(
+            **{self.PARAMS_TO_FIELDS.get(cn, cn): criteria[cn]}
+            ) for cn in self.MATCHES.keys() if cn in criteria
+            if criteria[cn]]
+
+        if query:
+            q = srch.index.query(
+                reduce(operator.or_,
+                       [srch.index.Q(**{self.PARAMS_TO_FIELDS.get(cn, cn): query})
+                        for cn in self.MATCHES.keys()],
+                srch.index.Q()))
+        else:
+            q = srch.index.query(srch.index.Q())
+
+        q = srch.apply_filters(q, filters).field_limit(score=True, fields=['book_id'])
+        results = q.execute()
+
+        book_scores = dict([(r['book_id'], r['score']) for r in results])
+        books = Book.objects.filter(id__in=set([r['book_id'] for r in results]))
+        books = list(books)
+        books.sort(reverse=True, key=lambda book: book_scores[book.id])
+        return books
 
     def get_link(self, query):
-        return "%s?q=%s" % (reverse('search'), query) 
+        return "%s?q=%s" % (reverse('search'), query)
 
-    def items(self, query):
+    def items(self, books):
         try:
-            return books_starting_with(query)
+            return books
         except ValueError:
             # too short a query
             return []