X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/075fb07f19c2f20cd3b6818b973e7aebebdacc9f..0e79f07515678e3e678c3637124a27cb81ee8a94:/apps/opds/views.py diff --git a/apps/opds/views.py b/apps/opds/views.py index c907fe198..c529e0272 100644 --- a/apps/opds/views.py +++ b/apps/opds/views.py @@ -2,10 +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 urllib2 import unquote from django.contrib.syndication.views import Feed from django.core.urlresolvers import reverse @@ -14,15 +12,18 @@ from django.utils.feedgenerator import Atom1Feed from django.conf import settings from django.http import Http404 from django.contrib.sites.models import Site +from django.utils.functional import lazy from basicauth import logged_in_or_basicauth, factory_decorator from catalogue.models import Book, Tag -from search import MultiSearch, SearchResult, JVM -from lucene import Term, QueryWrapperFilter, TermQuery - +from search.views import Search +import operator +import logging import re +log = logging.getLogger('opds') + from stats.utils import piwik_track _root_feeds = ( @@ -64,21 +65,22 @@ _root_feeds = ( ) +current_domain = lazy(lambda: Site.objects.get_current().domain, str)() def full_url(url): - return urljoin("http://%s" % Site.objects.get_current().domain, url) + return urljoin("http://%s" % current_domain, url) class OPDSFeed(Atom1Feed): link_rel = u"subsection" link_type = u"application/atom+xml" - _book_parent_img = full_url(os.path.join(settings.STATIC_URL, "img/book-parent.png")) + _book_parent_img = lazy(lambda: full_url(os.path.join(settings.STATIC_URL, "img/book-parent.png")), str)() try: _book_parent_img_size = unicode(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book-parent.png"))) except: _book_parent_img_size = '' - _book_img = full_url(os.path.join(settings.STATIC_URL, "img/book.png")) + _book_img = lazy(lambda: full_url(os.path.join(settings.STATIC_URL, "img/book.png")), str)() try: _book_img_size = unicode(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book.png"))) except: @@ -91,7 +93,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"}) @@ -111,6 +113,7 @@ class OPDSFeed(Atom1Feed): u"length": self._book_parent_img_size, u"type": u"image/png"}) if item['pubdate'] is not None: + # FIXME: rfc3339_date is undefined, is this ever run? handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('utf-8')) # Author information. @@ -127,6 +130,7 @@ class OPDSFeed(Atom1Feed): if item['unique_id'] is not None: unique_id = item['unique_id'] else: + # FIXME: get_tag_uri is undefined, is this ever run? unique_id = get_tag_uri(item['link'], item['pubdate']) handler.addQuickElement(u"id", unique_id) @@ -187,19 +191,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 @@ -216,13 +220,13 @@ class RootFeed(Feed): @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] + feed = [feed for feed in _root_feeds if feed['category'] == category] if feed: feed = feed[0] else: @@ -234,7 +238,7 @@ class ByCategoryFeed(Feed): return feed['title'] def items(self, feed): - return Tag.objects.filter(category=feed['category']).exclude(book_count=0) + return Tag.objects.filter(category=feed['category']).exclude(items=None) def item_title(self, item): return item.name @@ -260,13 +264,7 @@ class ByTagFeed(AcquisitionFeed): return get_object_or_404(Tag, category=category, slug=slug) 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]) - descendants_keys = [book.pk for book in Book.tagged.with_any(l_tags)] - if descendants_keys: - books = books.exclude(pk__in=descendants_keys) - - return books + return Book.tagged_top_level([tag]) @factory_decorator(logged_in_or_basicauth()) @@ -276,7 +274,7 @@ class UserFeed(Feed): 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 @@ -285,7 +283,7 @@ class UserFeed(Feed): return u"Półki użytkownika %s" % user.username def items(self, user): - return Tag.objects.filter(category='set', user=user).exclude(book_count=0) + return Tag.objects.filter(category='set', user=user).exclude(items=None) def item_title(self, item): return item.name @@ -296,9 +294,6 @@ 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 @@ -318,17 +313,41 @@ 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" - INLINE_QUERY_RE = re.compile(r"(author:(?P[^ ]+)|title:(?P[^ ]+)|categories:(?P<categories>[^ ]+)|description:(?P<description>[^ ]+))") - + 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): """ For OPDS 1.1 We should handle a query for search terms @@ -336,7 +355,7 @@ class SearchFeed(AcquisitionFeed): 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). @@ -344,94 +363,67 @@ class SearchFeed(AcquisitionFeed): (perhaps for is_book=True) """ - JVM.attachCurrentThread() query = request.GET.get('q', '') inline_criteria = re.findall(self.INLINE_QUERY_RE, query) if inline_criteria: - def get_criteria(criteria, name, position): - e = filter(lambda el: el[0][0:len(name)] == name, criteria) - print e - if not e: - return None - c = e[0][position] - print c - if c[0] == '"' and c[-1] == '"': - c = c[1:-1] - c = c.replace('+', ' ') - return c - - #import pdb; pdb.set_trace() - author = get_criteria(inline_criteria, 'author', 1) - title = get_criteria(inline_criteria, 'title', 2) - translator = None - categories = get_criteria(inline_criteria, 'categories', 3) - query = get_criteria(inline_criteria, 'description', 4) + 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: - author = request.GET.get('author', '') - title = request.GET.get('title', '') - translator = request.GET.get('translator', '') - categories = None - fuzzy = False + 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() - srch = MultiSearch() - hint = srch.hint() + 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]] - # Scenario 1: full search terms provided. - # Use auxiliarry information to narrow it and make it better. if query: - filters = [] - - if author: - print "narrow to author %s" % author - hint.tags(srch.search_tags(author, filter=srch.term_filter(Term('tag_category', 'author')))) - - if translator: - print "filter by translator %s" % translator - filters.append(QueryWrapperFilter( - srch.make_phrase(srch.get_tokens(translator, field='translators'), - field='translators'))) - - if categories: - filters.append(QueryWrapperFilter( - srch.make_phrase(srch.get_tokens(categories, field="tag_name_pl"), - field='tag_name_pl'))) - - flt = srch.chain_filters(filters) - if title: - print "hint by book title %s" % title - q = srch.make_phrase(srch.get_tokens(title, field='title'), field='title') - hint.books(*srch.search_books(q, filter=flt)) - - toks = srch.get_tokens(query) - print "tokens: %s" % toks - # import pdb; pdb.set_trace() - results = SearchResult.aggregate(srch.search_perfect_book(toks, fuzzy=fuzzy, hint=hint), - srch.search_perfect_parts(toks, fuzzy=fuzzy, hint=hint), - srch.search_everywhere(toks, fuzzy=fuzzy, hint=hint)) - results.sort(reverse=True) - return [r.book for r in results] + 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: - # Scenario 2: since we no longer have to figure out what the query term means to the user, - # we can just use filters and not the Hint class. - filters = [] - - fields = { - 'author': author, - 'translators': translator, - 'title': title - } - - for fld, q in fields.items(): - if q: - filters.append(QueryWrapperFilter( - srch.make_phrase(srch.get_tokens(q, field=fld), field=fld))) - - flt = srch.chain_filters(filters) - books = srch.search_books(TermQuery(Term('is_book', 'true')), filter=flt) - return books + 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)