From: Radek Czajka Date: Wed, 22 Sep 2010 10:37:43 +0000 (+0200) Subject: opds: added search, moved to separate app X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/cf9abb6f419c407ad53036d24c36b50105adeeff?ds=inline;hp=1606d4db6e8222f87b335c317ddbf24a745e8c37 opds: added search, moved to separate app --- diff --git a/apps/catalogue/feeds.py b/apps/catalogue/feeds.py deleted file mode 100644 index 886966836..000000000 --- a/apps/catalogue/feeds.py +++ /dev/null @@ -1,430 +0,0 @@ -# -*- 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: http://djangosnippets.org/snippets/243/ - -import base64 - -from django.http import HttpResponse -from django.contrib.auth import authenticate, login - -# -def view_or_basicauth(view, request, test_func, realm = "", *args, **kwargs): - """ - This is a helper function used by 'logged_in_or_basicauth' and - 'has_perm_or_basicauth' (deleted) that does the nitty of determining if they - are already logged in or if they have provided proper http-authorization - and returning the view if all goes well, otherwise responding with a 401. - """ - if test_func(request.user): - # Already logged in, just return the view. - # - return view(request, *args, **kwargs) - - # They are not logged in. See if they provided login credentials - # - if 'HTTP_AUTHORIZATION' in request.META: - auth = request.META['HTTP_AUTHORIZATION'].split() - if len(auth) == 2: - # NOTE: We are only support basic authentication for now. - # - if auth[0].lower() == "basic": - uname, passwd = base64.b64decode(auth[1]).split(':') - user = authenticate(username=uname, password=passwd) - if user is not None: - if user.is_active: - login(request, user) - request.user = user - return view(request, *args, **kwargs) - - # Either they did not provide an authorization header or - # something in the authorization attempt failed. Send a 401 - # back to them to ask them to authenticate. - # - response = HttpResponse() - response.status_code = 401 - response['WWW-Authenticate'] = 'Basic realm="%s"' % realm - return response - - -# -def logged_in_or_basicauth(realm = ""): - """ - A simple decorator that requires a user to be logged in. If they are not - logged in the request is examined for a 'authorization' header. - - If the header is present it is tested for basic authentication and - the user is logged in with the provided credentials. - - If the header is not present a http 401 is sent back to the - requestor to provide credentials. - - The purpose of this is that in several django projects I have needed - several specific views that need to support basic authentication, yet the - web site as a whole used django's provided authentication. - - The uses for this are for urls that are access programmatically such as - by rss feed readers, yet the view requires a user to be logged in. Many rss - readers support supplying the authentication credentials via http basic - auth (and they do NOT support a redirect to a form where they post a - username/password.) - - Use is simple: - - @logged_in_or_basicauth - def your_view: - ... - - You can provide the name of the realm to ask for authentication within. - """ - def view_decorator(func): - def wrapper(request, *args, **kwargs): - return view_or_basicauth(func, request, - lambda u: u.is_authenticated(), - realm, *args, **kwargs) - return wrapper - return view_decorator - - -############################################################################# - - -from base64 import b64encode -import os.path - -from django.contrib.syndication.views import Feed -from django.core.urlresolvers import reverse -from django.shortcuts import get_object_or_404 -from django.utils.feedgenerator import Atom1Feed -from django.conf import settings -from django.http import Http404 -from django.contrib.sites.models import Site - -from catalogue.models import Book, Tag - - -_root_feeds = ( - { - u"category": u"", - u"link": u"opds_user", - u"link_args": [], - u"title": u"Moje półki", - u"description": u"Półki użytkownika dostępne po zalogowaniu" - }, - { - u"category": u"author", - u"link": u"opds_by_category", - u"link_args": [u"author"], - u"title": u"Autorzy", - u"description": u"Utwory wg autorów" - }, - { - u"category": u"kind", - u"link": u"opds_by_category", - u"link_args": [u"kind"], - u"title": u"Rodzaje", - u"description": u"Utwory wg rodzajów" - }, - { - u"category": u"genre", - u"link": u"opds_by_category", - u"link_args": [u"genre"], - u"title": u"Gatunki", - u"description": u"Utwory wg gatunków" - }, - { - u"category": u"epoch", - u"link": u"opds_by_category", - u"link_args": [u"epoch"], - u"title": u"Epoki", - u"description": u"Utwory wg epok" - }, -) - - -def factory_decorator(decorator): - """ generates a decorator for a function factory class - if A(*) == f, factory_decorator(D)(A)(*) == D(f) - """ - def fac_dec(func): - def wrapper(*args, **kwargs): - return decorator(func(*args, **kwargs)) - return wrapper - return fac_dec - - -class OPDSFeed(Atom1Feed): - link_rel = u"subsection" - link_type = u"application/atom+xml" - - _book_parent_img = "http://%s%s" % (Site.objects.get_current().domain, os.path.join(settings.STATIC_URL, "img/book-parent.png")) - 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 = "http://%s%s" % (Site.objects.get_current().domain, os.path.join(settings.STATIC_URL, "img/book.png")) - try: - _book_img_size = unicode(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book.png"))) - except: - _book_img_size = '' - - def add_root_elements(self, handler): - super(OPDSFeed, self).add_root_elements(handler) - handler.addQuickElement(u"link", u"", {u"href": reverse("opds_authors"), u"rel": u"start", u"type": u"application/atom+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"}) - # 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"}) - if item['pubdate'] is not None: - handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('utf-8')) - - # Author information. - if item['author_name'] is not None: - handler.startElement(u"author", {}) - handler.addQuickElement(u"name", item['author_name']) - if item['author_email'] is not None: - handler.addQuickElement(u"email", item['author_email']) - if item['author_link'] is not None: - handler.addQuickElement(u"uri", item['author_link']) - handler.endElement(u"author") - - # Unique ID. - if item['unique_id'] is not None: - unique_id = item['unique_id'] - else: - unique_id = get_tag_uri(item['link'], item['pubdate']) - handler.addQuickElement(u"id", unique_id) - - # Summary. - # OPDS needs type=text - if item['description'] is not None: - 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}) - # 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"}) - - # Categories. - for cat in item['categories']: - handler.addQuickElement(u"category", u"", {u"term": cat}) - - # Rights. - if item['item_copyright'] is not None: - handler.addQuickElement(u"rights", item['item_copyright']) - - -class RootFeed(Feed): - feed_type = OPDSFeed - title = u'Wolne Lektury' - link = u'http://www.wolnelektury.pl/' - description = u"Spis utworów na stronie http://WolneLektury.pl" - author_name = u"Wolne Lektury" - author_link = u"http://www.wolnelektury.pl/" - - def items(self): - return _root_feeds - - def item_title(self, item): - return item['title'] - - def item_link(self, item): - return reverse(item['link'], args=item['link_args']) - - def item_description(self, item): - return item['description'] - - -class ByCategoryFeed(Feed): - feed_type = OPDSFeed - link = u'http://www.wolnelektury.pl/' - description = u"Spis utworów na stronie http://WolneLektury.pl" - author_name = u"Wolne Lektury" - author_link = u"http://www.wolnelektury.pl/" - - def get_object(self, request, category): - feed = [feed for feed in _root_feeds if feed['category']==category] - if feed: - feed = feed[0] - else: - raise Http404 - - return feed - - def title(self, feed): - return feed['title'] - - def items(self, feed): - return (tag for tag in Tag.objects.filter(category=feed['category']) if tag.get_count() > 0) - - def item_title(self, item): - return item.name - - def item_link(self, item): - return reverse("opds_by_tag", args=[item.category, item.slug]) - - def item_description(self): - return u'' - - -class ByTagFeed(Feed): - feed_type = OPDSFeed - link = u'http://www.wolnelektury.pl/' - item_enclosure_mime_type = "application/epub+zip" - author_name = u"Wolne Lektury" - author_link = u"http://www.wolnelektury.pl/" - - def link(self, tag): - return tag.get_absolute_url() - - def title(self, tag): - return tag.name - - def description(self, tag): - return u"Spis utworów na stronie http://WolneLektury.pl" - - def get_object(self, request, category, slug): - 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 - - def item_title(self, book): - return book.title - - def item_description(self): - return u'' - - def item_link(self, book): - return book.get_absolute_url() - - def item_author_name(self, book): - try: - return book.tags.filter(category='author')[0].name - except KeyError: - return u'' - - def item_author_link(self, book): - try: - return book.tags.filter(category='author')[0].get_absolute_url() - except KeyError: - return u'' - - def item_enclosure_url(self, book): - return "http://%s%s" % (Site.objects.get_current().domain, book.root_ancestor.epub_file.url) - - def item_enclosure_length(self, book): - return book.root_ancestor.epub_file.size - - -@factory_decorator(logged_in_or_basicauth()) -class UserFeed(Feed): - feed_type = OPDSFeed - 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/" - - def get_object(self, request): - return request.user - - def title(self, user): - return u"Półki użytkownika %s" % user.username - - def items(self, user): - return (tag for tag in Tag.objects.filter(category='set', user=user) if tag.get_count() > 0) - - def item_title(self, item): - return item.name - - def item_link(self, item): - return reverse("opds_user_set", args=[item.slug]) - - def item_description(self): - return u'' - - -@factory_decorator(logged_in_or_basicauth()) -class UserSetFeed(Feed): - feed_type = OPDSFeed - link = u'http://www.wolnelektury.pl/' - item_enclosure_mime_type = "application/epub+zip" - author_name = u"Wolne Lektury" - author_link = u"http://www.wolnelektury.pl/" - - def link(self, tag): - return tag.get_absolute_url() - - def title(self, tag): - return tag.name - - def description(self, tag): - return u"Spis utworów na stronie http://WolneLektury.pl" - - def get_object(self, request, slug): - return get_object_or_404(Tag, category='set', slug=slug, user=request.user) - - def items(self, tag): - return Book.tagged.with_any([tag]) - - def item_title(self, book): - return book.title - - def item_description(self): - return u'' - - def item_link(self, book): - return book.get_absolute_url() - - def item_author_name(self, book): - try: - return book.tags.filter(category='author')[0].name - except KeyError: - return u'' - - def item_author_link(self, book): - try: - return book.tags.filter(category='author')[0].get_absolute_url() - except KeyError: - return u'' - - def item_enclosure_url(self, book): - return "http://%s%s" % (Site.objects.get_current().domain, book.root_ancestor.epub_file.url) - - def item_enclosure_length(self, book): - return book.root_ancestor.epub_file.size - -@logged_in_or_basicauth() -def user_set_feed(request): - return UserSetFeed()(request) - diff --git a/apps/catalogue/urls.py b/apps/catalogue/urls.py index 53a70c3de..70d5fd8e4 100644 --- a/apps/catalogue/urls.py +++ b/apps/catalogue/urls.py @@ -3,7 +3,6 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.conf.urls.defaults import * -from catalogue.feeds import RootFeed, ByCategoryFeed, ByTagFeed, UserFeed, UserSetFeed urlpatterns = patterns('catalogue.views', @@ -23,13 +22,6 @@ urlpatterns = patterns('catalogue.views', # tools url(r'^zegar', 'clock', name='clock'), - # OPDS interface - url(r'^opds/$', RootFeed(), name="opds_authors"), - url(r'^opds/user/$', UserFeed(), name="opds_user"), - url(r'^opds/set/(?P[a-zA-Z0-9-]+)/$', UserSetFeed(), name="opds_user_set"), - url(r'^opds/(?P[a-zA-Z0-9-]+)/$', ByCategoryFeed(), name="opds_by_category"), - url(r'^opds/(?P[a-zA-Z0-9-]+)/(?P[a-zA-Z0-9-]+)/$', ByTagFeed(), name="opds_by_tag"), - # Public interface. Do not change this URLs. url(r'^lektura/(?P[a-zA-Z0-9-]+)\.html$', 'book_text', name='book_text'), url(r'^lektura/(?P[a-zA-Z0-9-]+)/$', 'book_detail', name='book_detail'), diff --git a/apps/catalogue/views.py b/apps/catalogue/views.py index d74ca7544..1180ab2fd 100644 --- a/apps/catalogue/views.py +++ b/apps/catalogue/views.py @@ -359,6 +359,10 @@ def _get_result_type(match): return type +def books_starting_with(prefix): + prefix = prefix.lower() + return models.Book.objects.filter(_word_starts_with('title', prefix)) + def find_best_matches(query, user=None): """ Finds a Book, Tag or Bookstub best matching a query. diff --git a/apps/opds/__init__.py b/apps/opds/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/apps/opds/urls.py b/apps/opds/urls.py new file mode 100644 index 000000000..1a316b6be --- /dev/null +++ b/apps/opds/urls.py @@ -0,0 +1,16 @@ +# -*- 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 django.conf.urls.defaults import * +from opds.views import RootFeed, ByCategoryFeed, ByTagFeed, UserFeed, UserSetFeed, SearchFeed + + +urlpatterns = patterns('opds.views', + url(r'^$', RootFeed(), name="opds_authors"), + url(r'^search/$', SearchFeed(), name="opds_search"), + url(r'^user/$', UserFeed(), name="opds_user"), + url(r'^set/(?P[a-zA-Z0-9-]+)/$', UserSetFeed(), name="opds_user_set"), + url(r'^(?P[a-zA-Z0-9-]+)/$', ByCategoryFeed(), name="opds_by_category"), + url(r'^(?P[a-zA-Z0-9-]+)/(?P[a-zA-Z0-9-]+)/$', ByTagFeed(), name="opds_by_tag"), +) diff --git a/apps/opds/views.py b/apps/opds/views.py new file mode 100644 index 000000000..a51b5b187 --- /dev/null +++ b/apps/opds/views.py @@ -0,0 +1,325 @@ +# -*- 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 base64 import b64encode +import os.path + +from django.contrib.syndication.views import Feed +from django.core.urlresolvers import reverse +from django.shortcuts import get_object_or_404 +from django.utils.feedgenerator import Atom1Feed +from django.conf import settings +from django.http import Http404 +from django.contrib.sites.models import Site + +from basicauth import logged_in_or_basicauth, factory_decorator +from catalogue.models import Book, Tag +from catalogue.views import books_starting_with + + +_root_feeds = ( + { + u"category": u"", + u"link": u"opds_user", + u"link_args": [], + u"title": u"Moje półki", + u"description": u"Półki użytkownika dostępne po zalogowaniu" + }, + { + u"category": u"author", + u"link": u"opds_by_category", + u"link_args": [u"author"], + u"title": u"Autorzy", + u"description": u"Utwory wg autorów" + }, + { + u"category": u"kind", + u"link": u"opds_by_category", + u"link_args": [u"kind"], + u"title": u"Rodzaje", + u"description": u"Utwory wg rodzajów" + }, + { + u"category": u"genre", + u"link": u"opds_by_category", + u"link_args": [u"genre"], + u"title": u"Gatunki", + u"description": u"Utwory wg gatunków" + }, + { + u"category": u"epoch", + u"link": u"opds_by_category", + u"link_args": [u"epoch"], + u"title": u"Epoki", + u"description": u"Utwory wg epok" + }, +) + + +def full_url(url): + return "http://%s%s" % (Site.objects.get_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")) + 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")) + try: + _book_img_size = unicode(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book.png"))) + except: + _book_img_size = '' + + + def add_root_elements(self, handler): + super(OPDSFeed, self).add_root_elements(handler) + handler.addQuickElement(u"link", None, + {u"href": reverse("opds_authors"), + u"rel": u"start", + u"type": u"application/atom+xml"}) + 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"}) + + + 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"}) + # 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"}) + if item['pubdate'] is not None: + handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('utf-8')) + + # Author information. + if item['author_name'] is not None: + handler.startElement(u"author", {}) + handler.addQuickElement(u"name", item['author_name']) + if item['author_email'] is not None: + handler.addQuickElement(u"email", item['author_email']) + if item['author_link'] is not None: + handler.addQuickElement(u"uri", item['author_link']) + handler.endElement(u"author") + + # Unique ID. + if item['unique_id'] is not None: + unique_id = item['unique_id'] + else: + unique_id = get_tag_uri(item['link'], item['pubdate']) + handler.addQuickElement(u"id", unique_id) + + # Summary. + # OPDS needs type=text + if item['description'] is not None: + 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}) + # 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"}) + + # Categories. + for cat in item['categories']: + handler.addQuickElement(u"category", u"", {u"term": cat}) + + # Rights. + if item['item_copyright'] is not None: + handler.addQuickElement(u"rights", item['item_copyright']) + + +class AcquisitionFeed(Feed): + feed_type = OPDSFeed + link = u'http://www.wolnelektury.pl/' + item_enclosure_mime_type = "application/epub+zip" + author_name = u"Wolne Lektury" + author_link = u"http://www.wolnelektury.pl/" + + def item_title(self, book): + return book.title + + def item_description(self): + return u'' + + def item_link(self, book): + return book.get_absolute_url() + + def item_author_name(self, book): + try: + return book.tags.filter(category='author')[0].name + except KeyError: + return u'' + + def item_author_link(self, book): + try: + return book.tags.filter(category='author')[0].get_absolute_url() + except KeyError: + return u'' + + def item_enclosure_url(self, book): + return full_url(book.root_ancestor.epub_file.url) + + def item_enclosure_length(self, book): + return book.root_ancestor.epub_file.size + + +class RootFeed(Feed): + feed_type = OPDSFeed + title = u'Wolne Lektury' + link = u'http://www.wolnelektury.pl/' + description = u"Spis utworów na stronie http://WolneLektury.pl" + author_name = u"Wolne Lektury" + author_link = u"http://www.wolnelektury.pl/" + + def items(self): + return _root_feeds + + def item_title(self, item): + return item['title'] + + def item_link(self, item): + return reverse(item['link'], args=item['link_args']) + + def item_description(self, item): + return item['description'] + + +class ByCategoryFeed(Feed): + feed_type = OPDSFeed + link = u'http://www.wolnelektury.pl/' + description = u"Spis utworów na stronie http://WolneLektury.pl" + author_name = u"Wolne Lektury" + author_link = u"http://www.wolnelektury.pl/" + + def get_object(self, request, category): + feed = [feed for feed in _root_feeds if feed['category']==category] + if feed: + feed = feed[0] + else: + raise Http404 + + return feed + + def title(self, feed): + return feed['title'] + + def items(self, feed): + return (tag for tag in Tag.objects.filter(category=feed['category']) if tag.get_count() > 0) + + def item_title(self, item): + return item.name + + def item_link(self, item): + return reverse("opds_by_tag", args=[item.category, item.slug]) + + def item_description(self): + return u'' + + +class ByTagFeed(AcquisitionFeed): + def link(self, tag): + return tag.get_absolute_url() + + def title(self, tag): + return tag.name + + def description(self, tag): + return u"Spis utworów na stronie http://WolneLektury.pl" + + def get_object(self, request, category, slug): + 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 + + +@factory_decorator(logged_in_or_basicauth()) +class UserFeed(Feed): + feed_type = OPDSFeed + 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/" + + def get_object(self, request): + return request.user + + def title(self, user): + return u"Półki użytkownika %s" % user.username + + def items(self, user): + return (tag for tag in Tag.objects.filter(category='set', user=user) if tag.get_count() > 0) + + def item_title(self, item): + return item.name + + def item_link(self, item): + return reverse("opds_user_set", args=[item.slug]) + + def item_description(self): + return u'' + + +@factory_decorator(logged_in_or_basicauth()) +class UserSetFeed(AcquisitionFeed): + def link(self, tag): + return tag.get_absolute_url() + + def title(self, tag): + return tag.name + + def description(self, tag): + return u"Spis utworów na stronie http://WolneLektury.pl" + + def get_object(self, request, slug): + return get_object_or_404(Tag, category='set', slug=slug, user=request.user) + + def items(self, tag): + return Book.tagged.with_any([tag]) + + +class SearchFeed(AcquisitionFeed): + description = u"Wyniki wyszukiwania na stronie WolneLektury.pl" + title = u"Wyniki wyszukiwania" + + def get_object(self, request): + return request.GET.get('q', '') + + def get_link(self, query): + return "%s?q=%s" % (reverse('search'), query) + + def items(self, query): + try: + return books_starting_with(query) + except ValueError: + # too short a query + return [] diff --git a/lib/basicauth.py b/lib/basicauth.py new file mode 100644 index 000000000..befcc6fe7 --- /dev/null +++ b/lib/basicauth.py @@ -0,0 +1,100 @@ +############################################################################# +# from http://djangosnippets.org/snippets/243/ + +from functools import wraps +import base64 + +from django.http import HttpResponse +from django.contrib.auth import authenticate, login + +# +def view_or_basicauth(view, request, test_func, realm = "", *args, **kwargs): + """ + This is a helper function used by 'logged_in_or_basicauth' and + 'has_perm_or_basicauth' (deleted) that does the nitty of determining if they + are already logged in or if they have provided proper http-authorization + and returning the view if all goes well, otherwise responding with a 401. + """ + if test_func(request.user): + # Already logged in, just return the view. + # + return view(request, *args, **kwargs) + + # They are not logged in. See if they provided login credentials + # + if 'HTTP_AUTHORIZATION' in request.META: + auth = request.META['HTTP_AUTHORIZATION'].split() + if len(auth) == 2: + # NOTE: We are only support basic authentication for now. + # + if auth[0].lower() == "basic": + uname, passwd = base64.b64decode(auth[1]).split(':') + user = authenticate(username=uname, password=passwd) + if user is not None: + if user.is_active: + login(request, user) + request.user = user + return view(request, *args, **kwargs) + + # Either they did not provide an authorization header or + # something in the authorization attempt failed. Send a 401 + # back to them to ask them to authenticate. + # + response = HttpResponse() + response.status_code = 401 + response['WWW-Authenticate'] = 'Basic realm="%s"' % realm + return response + + +# +def logged_in_or_basicauth(realm = ""): + """ + A simple decorator that requires a user to be logged in. If they are not + logged in the request is examined for a 'authorization' header. + + If the header is present it is tested for basic authentication and + the user is logged in with the provided credentials. + + If the header is not present a http 401 is sent back to the + requestor to provide credentials. + + The purpose of this is that in several django projects I have needed + several specific views that need to support basic authentication, yet the + web site as a whole used django's provided authentication. + + The uses for this are for urls that are access programmatically such as + by rss feed readers, yet the view requires a user to be logged in. Many rss + readers support supplying the authentication credentials via http basic + auth (and they do NOT support a redirect to a form where they post a + username/password.) + + Use is simple: + + @logged_in_or_basicauth + def your_view: + ... + + You can provide the name of the realm to ask for authentication within. + """ + def view_decorator(func): + def wrapper(request, *args, **kwargs): + return view_or_basicauth(func, request, + lambda u: u.is_authenticated(), + realm, *args, **kwargs) + return wrapper + return view_decorator + + +############################################################################# + + +def factory_decorator(decorator): + """ generates a decorator for a function factory class + if A(*) == f, factory_decorator(D)(A)(*) == D(f) + """ + def fac_dec(func): + @wraps(func) + def wrapper(*args, **kwargs): + return decorator(func(*args, **kwargs)) + return wrapper + return fac_dec diff --git a/scripts/fbreader.xml b/scripts/fbreader.xml new file mode 100644 index 000000000..69652b335 --- /dev/null +++ b/scripts/fbreader.xml @@ -0,0 +1,65 @@ + + + + + WolneLektury.pl + Wolne Lektury + Szkolna biblioteka internetowa + http://www.wolnelektury.pl/opds/ + http://www.wolnelektury.pl/opds/search/?q=%s + http://www.wolnelektury.pl/opds/user/ + http://www.wolnelektury.pl/uzytkownicy/wyloguj/?next=/opds/ + + http://www.wolnelektury.pl/opds/user/ + + + data:image/png;base64, +iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAADAFBMVEX////////////////////+ +///+///+//////7///////////////////7+///+///////+///+///+///9/v79/v79/v79/v3+ +/v3+/vv+/vv9/v3+/vz9/v39/fz9/fz9/fn9/fr9/fz+/f3+/v3+/f39/fz8/Pz7/Pv7/Pv7+/v6 ++/r5+vr4+vn2+fj2+fj7+fD3+Pb39/X59/L69/L6+PH7+O/7+O35+Oj4+OX299719tj19dP088j1 +8sb18MHz78Hz68Hy5sfi5ufe5ufn5+bc5KTb5KDr5eLx48vf45jr44/u493a4+Xb45rv44rb46bj +45Ta453y4dXz4YLz4oX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mPjy5 +FFLI58OPHzs3pdfy0YvEhVzl7RoaSx9DNQtQmPzzw9MLIY8OpjyQemUufZTAxMNvqbk6PASvnUU1 +2weFqfe9ihaR0hscfK9TkD86hKvuDNBPJP5cDyZulc6Kjxnznz3h4KYtZVACkPzYAXj2xpc4fKNM +64fUhKW3CTyjJQxNXsZc3XqDBKCePHbmYnu6iZ72rOC4C4ev5c+Iz4XnP3XSFrQ5zoHOyTPgweDY +AJZdH55aMwr3Uc1UFkAtmXjphazIDYU33yzW0YdHF+LgWHAqBFt4/exUBIPjkjf3dA20qjzgfX0T +lVPhkfQ+PEiPzRyUnRS3taSwM5NEN6d5oDwwOdlrL5zg2DAGbw0SwLADt6fX2qeYg3vw3F8d4Pbk +0pbzkpRe+mh9kcuB20uJU5luHkAOrrqfIDCzI9x7s1ivOLOD2v30fKfmBm5sf9BOnBJlJA8Sn8i7 +NoR6JhwCvC/HwRtKpp59fDeezIccPR3kpunB77h09OGtKyQkXfvBGwt1Eg4Fobfjl1+8z7ybOBei +MyjXTp+ClCWC6AruGriafnh1MNGUd4SSyAF0zMUzmcm3E6neJXRK5UxbA7NXqE54Xn3i4o1H6bGR +hwndCYcLQheSS1RW5TNjMi36bDehrP7YwMh9W1p07NKyAO6mvOhgBpgyN5iTZe8MTCh7QufV28qi +mXblamMDyB2D2esFzFwyuL4OqOxdxz6B6EetOjS48V8XGcTpvfQ3xOgk7scU/ZYAAAAASUVORK5C +YII= + + \ No newline at end of file diff --git a/wolnelektury/settings.py b/wolnelektury/settings.py index 0efb14531..a8bb82dd2 100644 --- a/wolnelektury/settings.py +++ b/wolnelektury/settings.py @@ -137,6 +137,7 @@ INSTALLED_APPS = [ 'infopages', 'suggest', 'lesmianator', + 'opds', ] CACHE_BACKEND = 'locmem:///?max_entries=3000' diff --git a/wolnelektury/static/opensearch.xml b/wolnelektury/static/opensearch.xml new file mode 100644 index 000000000..7a319f13b --- /dev/null +++ b/wolnelektury/static/opensearch.xml @@ -0,0 +1,12 @@ + + + Wolne Lektury + WolneLektury.pl, szkolna biblioteka internetowa. + lektury + Fundacja Nowoczesna Polska + pl + fundacja@nowoczesnapolska.org.pl + + + diff --git a/wolnelektury/urls.py b/wolnelektury/urls.py index a0b57b394..e3704f439 100644 --- a/wolnelektury/urls.py +++ b/wolnelektury/urls.py @@ -15,6 +15,7 @@ admin.autodiscover() urlpatterns = patterns('', url(r'^katalog/', include('catalogue.urls')), url(r'^materialy/', include('lessons.urls')), + url(r'^opds/', include('opds.urls')), url(r'^sugestia/', include('suggest.urls')), url(r'^lesmianator/?$', 'lesmianator.views.poem', name='lesmianator'),