1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from base64 import b64encode
7 from urlparse import urljoin
9 from django.contrib.syndication.views import Feed
10 from django.core.urlresolvers import reverse
11 from django.shortcuts import get_object_or_404
12 from django.utils.feedgenerator import Atom1Feed
13 from django.conf import settings
14 from django.http import Http404
15 from django.contrib.sites.models import Site
17 from basicauth import logged_in_or_basicauth, factory_decorator
18 from catalogue.models import Book, Tag
19 from catalogue.views import books_starting_with
21 from opds.utils import piwik_track
26 u"link": u"opds_user",
28 u"title": u"Moje półki",
29 u"description": u"Półki użytkownika dostępne po zalogowaniu"
32 u"category": u"author",
33 u"link": u"opds_by_category",
34 u"link_args": [u"author"],
36 u"description": u"Utwory wg autorów"
40 u"link": u"opds_by_category",
41 u"link_args": [u"kind"],
43 u"description": u"Utwory wg rodzajów"
46 u"category": u"genre",
47 u"link": u"opds_by_category",
48 u"link_args": [u"genre"],
50 u"description": u"Utwory wg gatunków"
53 u"category": u"epoch",
54 u"link": u"opds_by_category",
55 u"link_args": [u"epoch"],
57 u"description": u"Utwory wg epok"
63 return urljoin("http://%s" % Site.objects.get_current().domain, url)
66 class OPDSFeed(Atom1Feed):
67 link_rel = u"subsection"
68 link_type = u"application/atom+xml"
70 _book_parent_img = full_url(os.path.join(settings.STATIC_URL, "img/book-parent.png"))
72 _book_parent_img_size = unicode(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book-parent.png")))
74 _book_parent_img_size = ''
76 _book_img = full_url(os.path.join(settings.STATIC_URL, "img/book.png"))
78 _book_img_size = unicode(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book.png")))
83 def add_root_elements(self, handler):
84 super(OPDSFeed, self).add_root_elements(handler)
85 handler.addQuickElement(u"link", None,
86 {u"href": reverse("opds_authors"),
88 u"type": u"application/atom+xml"})
89 handler.addQuickElement(u"link", None,
90 {u"href": full_url(os.path.join(settings.STATIC_URL, "opensearch.xml")),
92 u"type": u"application/opensearchdescription+xml"})
95 def add_item_elements(self, handler, item):
96 """ modified from Atom1Feed.add_item_elements """
97 handler.addQuickElement(u"title", item['title'])
99 # add a OPDS Navigation link if there's no enclosure
100 if item['enclosure'] is None:
101 handler.addQuickElement(u"link", u"", {u"href": item['link'], u"rel": u"subsection", u"type": u"application/atom+xml"})
102 # add a "green book" icon
103 handler.addQuickElement(u"link", '',
104 {u"rel": u"http://opds-spec.org/thumbnail",
105 u"href": self._book_parent_img,
106 u"length": self._book_parent_img_size,
107 u"type": u"image/png"})
108 if item['pubdate'] is not None:
109 handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('utf-8'))
111 # Author information.
112 if item['author_name'] is not None:
113 handler.startElement(u"author", {})
114 handler.addQuickElement(u"name", item['author_name'])
115 if item['author_email'] is not None:
116 handler.addQuickElement(u"email", item['author_email'])
117 if item['author_link'] is not None:
118 handler.addQuickElement(u"uri", item['author_link'])
119 handler.endElement(u"author")
122 if item['unique_id'] is not None:
123 unique_id = item['unique_id']
125 unique_id = get_tag_uri(item['link'], item['pubdate'])
126 handler.addQuickElement(u"id", unique_id)
129 # OPDS needs type=text
130 if item['description'] is not None:
131 handler.addQuickElement(u"summary", item['description'], {u"type": u"text"})
133 # Enclosure as OPDS Acquisition Link
134 if item['enclosure'] is not None:
135 handler.addQuickElement(u"link", '',
136 {u"rel": u"http://opds-spec.org/acquisition",
137 u"href": item['enclosure'].url,
138 u"length": item['enclosure'].length,
139 u"type": item['enclosure'].mime_type})
140 # add a "red book" icon
141 handler.addQuickElement(u"link", '',
142 {u"rel": u"http://opds-spec.org/thumbnail",
143 u"href": self._book_img,
144 u"length": self._book_img_size,
145 u"type": u"image/png"})
148 for cat in item['categories']:
149 handler.addQuickElement(u"category", u"", {u"term": cat})
152 if item['item_copyright'] is not None:
153 handler.addQuickElement(u"rights", item['item_copyright'])
156 class AcquisitionFeed(Feed):
158 link = u'http://www.wolnelektury.pl/'
159 item_enclosure_mime_type = "application/epub+zip"
160 author_name = u"Wolne Lektury"
161 author_link = u"http://www.wolnelektury.pl/"
163 def item_title(self, book):
166 def item_description(self):
169 def item_link(self, book):
170 return book.get_absolute_url()
172 def item_author_name(self, book):
174 return book.tags.filter(category='author')[0].name
178 def item_author_link(self, book):
180 return book.tags.filter(category='author')[0].get_absolute_url()
184 def item_enclosure_url(self, book):
185 return full_url(book.root_ancestor.epub_file.url)
187 def item_enclosure_length(self, book):
188 return book.root_ancestor.epub_file.size
191 class RootFeed(Feed):
193 title = u'Wolne Lektury'
194 link = u'http://www.wolnelektury.pl/'
195 description = u"Spis utworów na stronie http://WolneLektury.pl"
196 author_name = u"Wolne Lektury"
197 author_link = u"http://www.wolnelektury.pl/"
202 def item_title(self, item):
205 def item_link(self, item):
206 return reverse(item['link'], args=item['link_args'])
208 def item_description(self, item):
209 return item['description']
212 class ByCategoryFeed(Feed):
214 link = u'http://www.wolnelektury.pl/'
215 description = u"Spis utworów na stronie http://WolneLektury.pl"
216 author_name = u"Wolne Lektury"
217 author_link = u"http://www.wolnelektury.pl/"
219 def get_object(self, request, category):
220 feed = [feed for feed in _root_feeds if feed['category']==category]
228 def title(self, feed):
231 def items(self, feed):
232 return (tag for tag in Tag.objects.filter(category=feed['category']) if tag.get_count() > 0)
234 def item_title(self, item):
237 def item_link(self, item):
238 return reverse("opds_by_tag", args=[item.category, item.slug])
240 def item_description(self):
244 class ByTagFeed(AcquisitionFeed):
246 return tag.get_absolute_url()
248 def title(self, tag):
251 def description(self, tag):
252 return u"Spis utworów na stronie http://WolneLektury.pl"
254 def get_object(self, request, category, slug):
255 return get_object_or_404(Tag, category=category, slug=slug)
257 def items(self, tag):
258 books = Book.tagged.with_any([tag])
259 l_tags = Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books])
260 descendants_keys = [book.pk for book in Book.tagged.with_any(l_tags)]
262 books = books.exclude(pk__in=descendants_keys)
267 @factory_decorator(logged_in_or_basicauth())
269 class UserFeed(Feed):
271 link = u'http://www.wolnelektury.pl/'
272 description = u"Półki użytkownika na stronie http://WolneLektury.pl"
273 author_name = u"Wolne Lektury"
274 author_link = u"http://www.wolnelektury.pl/"
276 def get_object(self, request):
279 def title(self, user):
280 return u"Półki użytkownika %s" % user.username
282 def items(self, user):
283 return (tag for tag in Tag.objects.filter(category='set', user=user) if tag.get_count() > 0)
285 def item_title(self, item):
288 def item_link(self, item):
289 return reverse("opds_user_set", args=[item.slug])
291 def item_description(self):
294 # no class decorators in python 2.5
295 #UserFeed = factory_decorator(logged_in_or_basicauth())(UserFeed)
298 @factory_decorator(logged_in_or_basicauth())
300 class UserSetFeed(AcquisitionFeed):
302 return tag.get_absolute_url()
304 def title(self, tag):
307 def description(self, tag):
308 return u"Spis utworów na stronie http://WolneLektury.pl"
310 def get_object(self, request, slug):
311 return get_object_or_404(Tag, category='set', slug=slug, user=request.user)
313 def items(self, tag):
314 return Book.tagged.with_any([tag])
316 # no class decorators in python 2.5
317 #UserSetFeed = factory_decorator(logged_in_or_basicauth())(UserSetFeed)
320 class SearchFeed(AcquisitionFeed):
321 description = u"Wyniki wyszukiwania na stronie WolneLektury.pl"
322 title = u"Wyniki wyszukiwania"
324 def get_object(self, request):
325 return request.GET.get('q', '')
327 def get_link(self, query):
328 return "%s?q=%s" % (reverse('search'), query)
330 def items(self, query):
332 return books_starting_with(query)