X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/357027375ff8867f42ca34bcbfb5a78b5b185fc3..084f20588e607aa9ae3ed280b6a2f0349b6a3200:/src/opds/views.py diff --git a/src/opds/views.py b/src/opds/views.py index c529e0272..c71822fa8 100644 --- a/src/opds/views.py +++ b/src/opds/views.py @@ -1,13 +1,13 @@ -# -*- coding: utf-8 -*- # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from functools import reduce import os.path -from urlparse import urljoin +from urllib.parse import urljoin from django.contrib.syndication.views import Feed -from django.core.urlresolvers import reverse from django.shortcuts import get_object_or_404 +from django.urls import reverse from django.utils.feedgenerator import Atom1Feed from django.conf import settings from django.http import Http404 @@ -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) @@ -76,17 +78,16 @@ 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: + _book_parent_img_size = str(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book-parent.png"))) + except OSError: _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: + _book_img_size = str(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book.png"))) + except OSError: _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"}) + if not item.get('enclosures') is None: + 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')) @@ -140,18 +144,24 @@ class OPDSFeed(Atom1Feed): handler.addQuickElement(u"summary", item['description'], {u"type": u"text"}) # 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}) + for enc in item.get('enclosures', []): + handler.addQuickElement( + u"link", '', + { + u"rel": u"http://opds-spec.org/acquisition", + u"href": enc.url, + u"length": enc.length, + u"type": enc.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())