9cec9b872afc09db50c91b2c36944d6a8b73e0a5
[wolnelektury.git] / src / api / urls.py
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.
4 #
5 from django.conf.urls import url
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 from ssify import ssi_included
11 import catalogue.views
12 from api import handlers
13 from api.helpers import CsrfExemptResource
14 from api.piston_patch import oauth_user_auth
15
16 auth = OAuthAuthentication(realm="Wolne Lektury")
17
18
19 class DjangoAuthentication(object):
20     """
21     Authentication handler that always returns
22     True, so no authentication is needed, nor
23     initiated (`challenge` is missing.)
24     """
25     def is_authenticated(self, request):
26         return request.user.is_authenticated()
27
28     def challenge(self):
29         from django.http import HttpResponse
30         resp = HttpResponse("Authorization Required")
31         resp.status_code = 401
32         return resp
33
34
35 def auth_resource(handler):
36     from django.conf import settings
37     if settings.DEBUG:
38         django_auth = DjangoAuthentication()
39         return CsrfExemptResource(handler=handler, authentication=django_auth)
40     return CsrfExemptResource(handler=handler, authentication=auth)
41
42
43 book_list_resource = auth_resource(handler=handlers.BooksHandler)
44 ebook_list_resource = Resource(handler=handlers.EBooksHandler)
45 # book_list_resource = Resource(handler=handlers.BooksHandler)
46 book_resource = Resource(handler=handlers.BookDetailHandler)
47 filter_book_resource = auth_resource(handler=handlers.FilterBooksHandler)
48 epub_resource = auth_resource(handler=handlers.EpubHandler)
49
50 preview_resource = Resource(handler=handlers.BookPreviewHandler)
51
52 reading_resource = auth_resource(handler=handlers.UserDataHandler)
53 shelf_resource = auth_resource(handler=handlers.UserShelfHandler)
54
55 like_resource = auth_resource(handler=handlers.UserLikeHandler)
56
57 collection_resource = Resource(handler=handlers.CollectionDetailHandler)
58 collection_list_resource = Resource(handler=handlers.CollectionsHandler)
59
60 tag_list_resource = Resource(handler=handlers.TagsHandler)
61 tag_resource = Resource(handler=handlers.TagDetailHandler)
62
63 fragment_resource = Resource(handler=handlers.FragmentDetailHandler)
64 fragment_list_resource = Resource(handler=handlers.FragmentsHandler)
65
66 picture_resource = auth_resource(handler=handlers.PictureHandler)
67
68 blog_resource = Resource(handler=handlers.BlogEntryHandler)
69
70
71 tags_re = r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})'
72 paginate_re = r'(?:before/(?P<before>[a-z0-9-]+)/)?(?:after/(?P<after>[a-z0-9-]+)/)?(?:count/(?P<count>[0-9]+)/)?$'
73
74
75 @ssi_included
76 def incl(request, model, pk, emitter_format):
77     resource = {
78         'book': book_list_resource,
79         'fragment': fragment_list_resource,
80         'tag': tag_list_resource,
81         }[model]
82     request.piwik_track = False
83     resp = resource(request, pk=pk, emitter_format=emitter_format)
84     if emitter_format == 'xml':
85         # Ugly, but quick way of stripping <?xml?> header and <response> tags.
86         resp.content = resp.content[49:-11]
87     return resp
88
89
90 urlpatterns = [
91     url(r'^oauth/request_token/$', oauth_request_token),
92     url(r'^oauth/authorize/$', oauth_user_auth, name='oauth_user_auth'),
93     url(r'^oauth/access_token/$', csrf_exempt(oauth_access_token)),
94
95     url(r'^$', TemplateView.as_view(template_name='api/main.html'), name='api'),
96     url(r'^include/(?P<model>book|fragment|tag)/(?P<pk>\d+)\.(?P<lang>.+)\.(?P<emitter_format>xml|json)$',
97         incl, name='api_include'),
98
99     # info boxes (used by mobile app)
100     url(r'book/(?P<book_id>\d*?)/info\.html$', catalogue.views.book_info),
101     url(r'tag/(?P<tag_id>\d*?)/info\.html$', catalogue.views.tag_info),
102
103     # books by collections
104     url(r'^collections/$', collection_list_resource, name="api_collections"),
105     url(r'^collections/(?P<slug>[^/]+)/$', collection_resource, name="api_collection"),
106
107     # epub preview
108     url(r'^epub/(?P<slug>[a-z0-9-]+)/$', epub_resource, name='api_epub'),
109
110     # reading data
111     url(r'^reading/(?P<slug>[a-z0-9-]+)/$', reading_resource, name='api_reading'),
112     url(r'^reading/(?P<slug>[a-z0-9-]+)/(?P<state>[a-z]+)/$', reading_resource, name='api_reading'),
113     url(r'^shelf/(?P<state>[a-z]+)/$', shelf_resource, name='api_shelf'),
114     url(r'^username/$', reading_resource, name='api_username'),
115
116     url(r'^like/(?P<slug>[a-z0-9-]+)/$', like_resource, name='api_like'),
117
118     # objects details
119     url(r'^books/(?P<book>[a-z0-9-]+)/$', book_resource, name="api_book"),
120     url(r'^(?P<category>[a-z0-9-]+)/(?P<slug>[a-z0-9-]+)/$',
121         tag_resource, name="api_tag"),
122     url(r'^books/(?P<book>[a-z0-9-]+)/fragments/(?P<anchor>[a-z0-9-]+)/$',
123         fragment_resource, name="api_fragment"),
124
125     # books by tags
126     url(tags_re + r'books/' + paginate_re,
127         book_list_resource, name='api_book_list'),
128     url(tags_re + r'ebooks/' + paginate_re,
129         ebook_list_resource, name='api_ebook_list'),
130     url(tags_re + r'parent_books/' + paginate_re,
131         book_list_resource, {"top_level": True}, name='api_parent_book_list'),
132     url(tags_re + r'parent_ebooks/' + paginate_re,
133         ebook_list_resource, {"top_level": True}, name='api_parent_ebook_list'),
134     url(tags_re + r'audiobooks/' + paginate_re,
135         book_list_resource, {"audiobooks": True}, name='api_audiobook_list'),
136     url(tags_re + r'daisy/' + paginate_re,
137         book_list_resource, {"daisy": True}, name='api_daisy_list'),
138
139     url(r'^recommended/' + paginate_re, book_list_resource, {"recommended": True}, name='api_recommended_list'),
140     url(r'^newest/$', book_list_resource, {"newest": True, "top_level": True, "count": 20}, name='api_newest_list'),
141     url(r'^filter-books/$', filter_book_resource, name='api_filter_books'),
142
143     url(r'^preview/$', preview_resource, name='api_preview'),
144
145     url(r'^pictures/$', picture_resource),
146
147     url(r'^blog/$', blog_resource),
148
149     # fragments by book, tags, themes
150     # this should be paged
151     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', fragment_list_resource),
152
153     # tags by category
154     url(r'^(?P<category>[a-z0-9-]+)/$', tag_list_resource, name='api_tag_list'),
155 ]