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
8 from django.contrib.syndication.views import Feed
9 from django.core.urlresolvers import reverse
10 from django.shortcuts import get_object_or_404
11 from django.utils.feedgenerator import Atom1Feed
12 from django.conf import settings
13 from django.http import Http404
14 from django.contrib.sites.models import Site
16 from basicauth import logged_in_or_basicauth, factory_decorator
17 from catalogue.models import Book, Tag
18 from catalogue.views import books_starting_with
24 u"link": u"opds_user",
26 u"title": u"Moje półki",
27 u"description": u"Półki użytkownika dostępne po zalogowaniu"
30 u"category": u"author",
31 u"link": u"opds_by_category",
32 u"link_args": [u"author"],
34 u"description": u"Utwory wg autorów"
38 u"link": u"opds_by_category",
39 u"link_args": [u"kind"],
41 u"description": u"Utwory wg rodzajów"
44 u"category": u"genre",
45 u"link": u"opds_by_category",
46 u"link_args": [u"genre"],
48 u"description": u"Utwory wg gatunków"
51 u"category": u"epoch",
52 u"link": u"opds_by_category",
53 u"link_args": [u"epoch"],
55 u"description": u"Utwory wg epok"
61 return "http://%s%s" % (Site.objects.get_current().domain, url)
64 class OPDSFeed(Atom1Feed):
65 link_rel = u"subsection"
66 link_type = u"application/atom+xml"
68 _book_parent_img = full_url(os.path.join(settings.STATIC_URL, "img/book-parent.png"))
70 _book_parent_img_size = unicode(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book-parent.png")))
72 _book_parent_img_size = ''
74 _book_img = full_url(os.path.join(settings.STATIC_URL, "img/book.png"))
76 _book_img_size = unicode(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book.png")))
81 def add_root_elements(self, handler):
82 super(OPDSFeed, self).add_root_elements(handler)
83 handler.addQuickElement(u"link", None,
84 {u"href": reverse("opds_authors"),
86 u"type": u"application/atom+xml"})
87 handler.addQuickElement(u"link", None,
88 {u"href": full_url(os.path.join(settings.STATIC_URL, "opensearch.xml")),
90 u"type": u"application/opensearchdescription+xml"})
93 def add_item_elements(self, handler, item):
94 """ modified from Atom1Feed.add_item_elements """
95 handler.addQuickElement(u"title", item['title'])
97 # add a OPDS Navigation link if there's no enclosure
98 if item['enclosure'] is None:
99 handler.addQuickElement(u"link", u"", {u"href": item['link'], u"rel": u"subsection", u"type": u"application/atom+xml"})
100 # add a "green book" icon
101 handler.addQuickElement(u"link", '',
102 {u"rel": u"http://opds-spec.org/thumbnail",
103 u"href": self._book_parent_img,
104 u"length": self._book_parent_img_size,
105 u"type": u"image/png"})
106 if item['pubdate'] is not None:
107 handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('utf-8'))
109 # Author information.
110 if item['author_name'] is not None:
111 handler.startElement(u"author", {})
112 handler.addQuickElement(u"name", item['author_name'])
113 if item['author_email'] is not None:
114 handler.addQuickElement(u"email", item['author_email'])
115 if item['author_link'] is not None:
116 handler.addQuickElement(u"uri", item['author_link'])
117 handler.endElement(u"author")
120 if item['unique_id'] is not None:
121 unique_id = item['unique_id']
123 unique_id = get_tag_uri(item['link'], item['pubdate'])
124 handler.addQuickElement(u"id", unique_id)
127 # OPDS needs type=text
128 if item['description'] is not None:
129 handler.addQuickElement(u"summary", item['description'], {u"type": u"text"})
131 # Enclosure as OPDS Acquisition Link
132 if item['enclosure'] is not None:
133 handler.addQuickElement(u"link", '',
134 {u"rel": u"http://opds-spec.org/acquisition",
135 u"href": item['enclosure'].url,
136 u"length": item['enclosure'].length,
137 u"type": item['enclosure'].mime_type})
138 # add a "red book" icon
139 handler.addQuickElement(u"link", '',
140 {u"rel": u"http://opds-spec.org/thumbnail",
141 u"href": self._book_img,
142 u"length": self._book_img_size,
143 u"type": u"image/png"})
146 for cat in item['categories']:
147 handler.addQuickElement(u"category", u"", {u"term": cat})
150 if item['item_copyright'] is not None:
151 handler.addQuickElement(u"rights", item['item_copyright'])
154 class AcquisitionFeed(Feed):
156 link = u'http://www.wolnelektury.pl/'
157 item_enclosure_mime_type = "application/epub+zip"
158 author_name = u"Wolne Lektury"
159 author_link = u"http://www.wolnelektury.pl/"
161 def item_title(self, book):
164 def item_description(self):
167 def item_link(self, book):
168 return book.get_absolute_url()
170 def item_author_name(self, book):
172 return book.tags.filter(category='author')[0].name
176 def item_author_link(self, book):
178 return book.tags.filter(category='author')[0].get_absolute_url()
182 def item_enclosure_url(self, book):
183 return full_url(book.root_ancestor.epub_file.url)
185 def item_enclosure_length(self, book):
186 return book.root_ancestor.epub_file.size
189 class RootFeed(Feed):
191 title = u'Wolne Lektury'
192 link = u'http://www.wolnelektury.pl/'
193 description = u"Spis utworów na stronie http://WolneLektury.pl"
194 author_name = u"Wolne Lektury"
195 author_link = u"http://www.wolnelektury.pl/"
200 def item_title(self, item):
203 def item_link(self, item):
204 return reverse(item['link'], args=item['link_args'])
206 def item_description(self, item):
207 return item['description']
210 class ByCategoryFeed(Feed):
212 link = u'http://www.wolnelektury.pl/'
213 description = u"Spis utworów na stronie http://WolneLektury.pl"
214 author_name = u"Wolne Lektury"
215 author_link = u"http://www.wolnelektury.pl/"
217 def get_object(self, request, category):
218 feed = [feed for feed in _root_feeds if feed['category']==category]
226 def title(self, feed):
229 def items(self, feed):
230 return (tag for tag in Tag.objects.filter(category=feed['category']) if tag.get_count() > 0)
232 def item_title(self, item):
235 def item_link(self, item):
236 return reverse("opds_by_tag", args=[item.category, item.slug])
238 def item_description(self):
242 class ByTagFeed(AcquisitionFeed):
244 return tag.get_absolute_url()
246 def title(self, tag):
249 def description(self, tag):
250 return u"Spis utworów na stronie http://WolneLektury.pl"
252 def get_object(self, request, category, slug):
253 return get_object_or_404(Tag, category=category, slug=slug)
255 def items(self, tag):
256 books = Book.tagged.with_any([tag])
257 l_tags = Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books])
258 descendants_keys = [book.pk for book in Book.tagged.with_any(l_tags)]
260 books = books.exclude(pk__in=descendants_keys)
265 #@factory_decorator(logged_in_or_basicauth())
266 class UserFeed(Feed):
268 link = u'http://www.wolnelektury.pl/'
269 description = u"Półki użytkownika na stronie http://WolneLektury.pl"
270 author_name = u"Wolne Lektury"
271 author_link = u"http://www.wolnelektury.pl/"
273 def get_object(self, request):
276 def title(self, user):
277 return u"Półki użytkownika %s" % user.username
279 def items(self, user):
280 return (tag for tag in Tag.objects.filter(category='set', user=user) if tag.get_count() > 0)
282 def item_title(self, item):
285 def item_link(self, item):
286 return reverse("opds_user_set", args=[item.slug])
288 def item_description(self):
291 # no class decorators in python 2.5
292 UserFeed = factory_decorator(logged_in_or_basicauth())(UserFeed)
295 #@factory_decorator(logged_in_or_basicauth())
296 class UserSetFeed(AcquisitionFeed):
298 return tag.get_absolute_url()
300 def title(self, tag):
303 def description(self, tag):
304 return u"Spis utworów na stronie http://WolneLektury.pl"
306 def get_object(self, request, slug):
307 return get_object_or_404(Tag, category='set', slug=slug, user=request.user)
309 def items(self, tag):
310 return Book.tagged.with_any([tag])
312 # no class decorators in python 2.5
313 UserSetFeed = factory_decorator(logged_in_or_basicauth())(UserSetFeed)
316 class SearchFeed(AcquisitionFeed):
317 description = u"Wyniki wyszukiwania na stronie WolneLektury.pl"
318 title = u"Wyniki wyszukiwania"
320 def get_object(self, request):
321 return request.GET.get('q', '')
323 def get_link(self, query):
324 return "%s?q=%s" % (reverse('search'), query)
326 def items(self, query):
328 return books_starting_with(query)