X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/357027375ff8867f42ca34bcbfb5a78b5b185fc3..78bbe25fbfdf841d1c7ed1e1059a26c16d4f8ee7:/src/opds/views.py diff --git a/src/opds/views.py b/src/opds/views.py index c529e0272..0c31cd200 100644 --- a/src/opds/views.py +++ b/src/opds/views.py @@ -22,10 +22,10 @@ import operator import logging import re -log = logging.getLogger('opds') - from stats.utils import piwik_track +log = logging.getLogger('opds') + _root_feeds = ( { u"category": u"", @@ -66,6 +66,8 @@ _root_feeds = ( current_domain = lazy(lambda: Site.objects.get_current().domain, str)() + + def full_url(url): return urljoin("http://%s" % current_domain, url) @@ -77,16 +79,15 @@ class OPDSFeed(Atom1Feed): _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: + except IOError: _book_parent_img_size = '' _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: + except IOError: _book_img_size = '' - def add_root_elements(self, handler): super(OPDSFeed, self).add_root_elements(handler) handler.addQuickElement(u"link", None, @@ -98,20 +99,23 @@ class OPDSFeed(Atom1Feed): u"rel": u"search", u"type": u"application/opensearchdescription+xml"}) - def add_item_elements(self, handler, item): """ modified from Atom1Feed.add_item_elements """ handler.addQuickElement(u"title", item['title']) # add a OPDS Navigation link if there's no enclosure if item['enclosure'] is None: - handler.addQuickElement(u"link", u"", {u"href": item['link'], u"rel": u"subsection", u"type": u"application/atom+xml"}) + handler.addQuickElement( + u"link", u"", {u"href": item['link'], u"rel": u"subsection", u"type": u"application/atom+xml"}) # add a "green book" icon - handler.addQuickElement(u"link", '', - {u"rel": u"http://opds-spec.org/thumbnail", - u"href": self._book_parent_img, - u"length": self._book_parent_img_size, - u"type": u"image/png"}) + handler.addQuickElement( + u"link", '', + { + u"rel": u"http://opds-spec.org/thumbnail", + u"href": self._book_parent_img, + 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')) @@ -141,17 +145,23 @@ class OPDSFeed(Atom1Feed): # Enclosure as OPDS Acquisition Link if item['enclosure'] is not None: - handler.addQuickElement(u"link", '', - {u"rel": u"http://opds-spec.org/acquisition", - u"href": item['enclosure'].url, - u"length": item['enclosure'].length, - u"type": item['enclosure'].mime_type}) + handler.addQuickElement( + u"link", '', + { + u"rel": u"http://opds-spec.org/acquisition", + u"href": item['enclosure'].url, + u"length": item['enclosure'].length, + u"type": item['enclosure'].mime_type, + }) # add a "red book" icon - handler.addQuickElement(u"link", '', - {u"rel": u"http://opds-spec.org/thumbnail", - u"href": self._book_img, - u"length": self._book_img_size, - u"type": u"image/png"}) + handler.addQuickElement( + u"link", '', + { + u"rel": u"http://opds-spec.org/thumbnail", + u"href": self._book_img, + u"length": self._book_img_size, + u"type": u"image/png", + }) # Categories. for cat in item['categories']: @@ -180,22 +190,23 @@ class AcquisitionFeed(Feed): def item_author_name(self, book): try: - return book.tags.filter(category='author')[0].name - except KeyError: + return book.authors().first().name + except AttributeError: return u'' def item_author_link(self, book): try: - return book.tags.filter(category='author')[0].get_absolute_url() - except KeyError: + return book.authors().first().get_absolute_url() + except AttributeError: return u'' def item_enclosure_url(self, book): - return full_url(book.epub_file.url) if book.epub_file else None + return full_url(book.epub_url()) if book.epub_file else None def item_enclosure_length(self, book): return book.epub_file.size if book.epub_file else None + @piwik_track class RootFeed(Feed): feed_type = OPDSFeed @@ -217,6 +228,7 @@ class RootFeed(Feed): def item_description(self, item): return item['description'] + @piwik_track class ByCategoryFeed(Feed): feed_type = OPDSFeed @@ -249,6 +261,7 @@ class ByCategoryFeed(Feed): def item_description(self): return u'' + @piwik_track class ByTagFeed(AcquisitionFeed): def link(self, tag): @@ -394,8 +407,9 @@ class SearchFeed(AcquisitionFeed): return '' return val - criteria = dict([(cn, remove_dump_data(request.GET.get(cn, ''))) - for cn in self.MATCHES.keys()]) + 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)) @@ -409,10 +423,10 @@ class SearchFeed(AcquisitionFeed): 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())) + 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())