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 django.conf.urls import url, include
6 from django.views.decorators.csrf import csrf_exempt
7 from django.views.generic import TemplateView
8 from piston.authentication import OAuthAuthentication, oauth_access_token, oauth_request_token
9 from piston.resource import Resource
10 import catalogue.views
11 from api import handlers
12 from api.helpers import CsrfExemptResource
13 from api.piston_patch import oauth_user_auth
15 auth = OAuthAuthentication(realm="Wolne Lektury")
18 class DjangoAuthentication(object):
20 Authentication handler that always returns
21 True, so no authentication is needed, nor
22 initiated (`challenge` is missing.)
24 def is_authenticated(self, request):
25 return request.user.is_authenticated()
28 from django.http import HttpResponse
29 resp = HttpResponse("Authorization Required")
30 resp.status_code = 401
34 def auth_resource(handler):
35 from django.conf import settings
37 django_auth = DjangoAuthentication()
38 return CsrfExemptResource(handler=handler, authentication=django_auth)
39 return CsrfExemptResource(handler=handler, authentication=auth)
42 book_list_resource = auth_resource(handler=handlers.BooksHandler)
43 ebook_list_resource = Resource(handler=handlers.EBooksHandler)
44 # book_list_resource = Resource(handler=handlers.BooksHandler)
45 filter_book_resource = auth_resource(handler=handlers.FilterBooksHandler)
46 epub_resource = auth_resource(handler=handlers.EpubHandler)
48 preview_resource = Resource(handler=handlers.BookPreviewHandler)
50 reading_resource = auth_resource(handler=handlers.UserDataHandler)
51 shelf_resource = auth_resource(handler=handlers.UserShelfHandler)
53 like_resource = auth_resource(handler=handlers.UserLikeHandler)
55 tag_list_resource = Resource(handler=handlers.TagsHandler)
56 tag_resource = Resource(handler=handlers.TagDetailHandler)
58 fragment_resource = Resource(handler=handlers.FragmentDetailHandler)
59 fragment_list_resource = Resource(handler=handlers.FragmentsHandler)
61 picture_resource = auth_resource(handler=handlers.PictureHandler)
63 blog_resource = Resource(handler=handlers.BlogEntryHandler)
66 tags_re = r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})'
67 paginate_re = r'(?:after/(?P<after>[a-z0-9-]+)/)?(?:count/(?P<count>[0-9]+)/)?$'
71 url(r'^oauth/request_token/$', oauth_request_token),
72 url(r'^oauth/authorize/$', oauth_user_auth, name='oauth_user_auth'),
73 url(r'^oauth/access_token/$', csrf_exempt(oauth_access_token)),
75 url(r'^$', TemplateView.as_view(template_name='api/main.html'), name='api'),
77 # These are the new ones.
78 url(r'^', include('catalogue.api.urls')),
80 # info boxes (used by mobile app)
81 url(r'book/(?P<book_id>\d*?)/info\.html$', catalogue.views.book_info),
82 url(r'tag/(?P<tag_id>\d*?)/info\.html$', catalogue.views.tag_info),
85 url(r'^epub/(?P<slug>[a-z0-9-]+)/$', epub_resource, name='api_epub'),
88 url(r'^reading/(?P<slug>[a-z0-9-]+)/$', reading_resource, name='api_reading'),
89 url(r'^reading/(?P<slug>[a-z0-9-]+)/(?P<state>[a-z]+)/$', reading_resource, name='api_reading'),
90 url(r'^shelf/(?P<state>[a-z]+)/$', shelf_resource, name='api_shelf'),
91 url(r'^username/$', reading_resource, name='api_username'),
93 url(r'^like/(?P<slug>[a-z0-9-]+)/$', like_resource, name='api_like'),
96 url(r'^(?P<category>[a-z0-9-]+)/(?P<slug>[a-z0-9-]+)/$',
97 tag_resource, name="api_tag"),
98 url(r'^books/(?P<book>[a-z0-9-]+)/fragments/(?P<anchor>[a-z0-9-]+)/$',
99 fragment_resource, name="api_fragment"),
102 url(tags_re + r'books/' + paginate_re,
103 book_list_resource, name='api_book_list'),
104 url(tags_re + r'ebooks/' + paginate_re,
105 ebook_list_resource, name='api_ebook_list'),
106 url(tags_re + r'parent_books/' + paginate_re,
107 book_list_resource, {"top_level": True}, name='api_parent_book_list'),
108 url(tags_re + r'parent_ebooks/' + paginate_re,
109 ebook_list_resource, {"top_level": True}, name='api_parent_ebook_list'),
110 url(tags_re + r'audiobooks/' + paginate_re,
111 book_list_resource, {"audiobooks": True}, name='api_audiobook_list'),
112 url(tags_re + r'daisy/' + paginate_re,
113 book_list_resource, {"daisy": True}, name='api_daisy_list'),
115 url(r'^recommended/' + paginate_re, book_list_resource, {"recommended": True}, name='api_recommended_list'),
116 url(r'^newest/$', book_list_resource, {"newest": True, "top_level": True, "count": 20}, name='api_newest_list'),
117 url(r'^filter-books/$', filter_book_resource, name='api_filter_books'),
119 url(r'^preview/$', preview_resource, name='api_preview'),
121 url(r'^pictures/$', picture_resource),
123 url(r'^blog/$', blog_resource),
125 # fragments by book, tags, themes
126 # this should be paged
127 url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', fragment_list_resource),
130 url(r'^(?P<category>[a-z0-9-]+)/$', tag_list_resource, name='api_tag_list'),