move fragment to book detail
[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 patterns, 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
9 from piston.resource import Resource
10 from ssify import ssi_included
11 from api import handlers
12 from api.helpers import CsrfExemptResource
13
14 auth = OAuthAuthentication(realm="Wolne Lektury")
15
16 book_list_resource = CsrfExemptResource(handler=handlers.BooksHandler, authentication=auth)
17 ebook_list_resource = Resource(handler=handlers.EBooksHandler)
18 # book_list_resource = Resource(handler=handlers.BooksHandler)
19 book_resource = Resource(handler=handlers.BookDetailHandler)
20 filter_book_resource = Resource(handler=handlers.FilterBooksHandler)
21
22 collection_resource = Resource(handler=handlers.CollectionDetailHandler)
23 collection_list_resource = Resource(handler=handlers.CollectionsHandler)
24
25 tag_list_resource = Resource(handler=handlers.TagsHandler)
26 tag_resource = Resource(handler=handlers.TagDetailHandler)
27
28 fragment_resource = Resource(handler=handlers.FragmentDetailHandler)
29 fragment_list_resource = Resource(handler=handlers.FragmentsHandler)
30
31 picture_resource = CsrfExemptResource(handler=handlers.PictureHandler, authentication=auth)
32
33
34 tags_re = r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})'
35 paginate_re = r'(?:before/(?P<before>[a-z0-9-]+)/)?(?:after/(?P<after>[a-z0-9-]+)/)?(?:count/(?P<count>[0-9]+)/)?$'
36
37
38 @ssi_included
39 def incl(request, model, pk, emitter_format):
40     resource = {
41         'book': book_list_resource,
42         'fragment': fragment_list_resource,
43         'tag': tag_list_resource,
44         }[model]
45     request.piwik_track = False
46     resp = resource(request, pk=pk, emitter_format=emitter_format)
47     if emitter_format == 'xml':
48         # Ugly, but quick way of stripping <?xml?> header and <response> tags.
49         resp.content = resp.content[49:-11]
50     return resp
51
52
53 urlpatterns = patterns(
54     'piston.authentication',
55     url(r'^oauth/request_token/$', 'oauth_request_token'),
56     url(r'^oauth/authorize/$', 'oauth_user_auth'),
57     url(r'^oauth/access_token/$', csrf_exempt(oauth_access_token)),
58
59 ) + patterns(
60     '',
61     url(r'^$', TemplateView.as_view(template_name='api/main.html'), name='api'),
62     url(r'^include/(?P<model>book|fragment|tag)/(?P<pk>\d+)\.(?P<lang>.+)\.(?P<emitter_format>xml|json)$',
63         incl, name='api_include'),
64
65     # info boxes (used by mobile app)
66     url(r'book/(?P<book_id>\d*?)/info\.html$', 'catalogue.views.book_info'),
67     url(r'tag/(?P<tag_id>\d*?)/info\.html$', 'catalogue.views.tag_info'),
68
69     # books by collections
70     url(r'^collections/$', collection_list_resource, name="api_collections"),
71     url(r'^collections/(?P<slug>[^/]+)/$', collection_resource, name="api_collection"),
72
73     # objects details
74     url(r'^books/(?P<book>[a-z0-9-]+)/$', book_resource, name="api_book"),
75     url(r'^(?P<category>[a-z0-9-]+)/(?P<slug>[a-z0-9-]+)/$',
76         tag_resource, name="api_tag"),
77     url(r'^books/(?P<book>[a-z0-9-]+)/fragments/(?P<anchor>[a-z0-9-]+)/$',
78         fragment_resource, name="api_fragment"),
79
80     # books by tags
81     url(tags_re + r'books/' + paginate_re,
82         book_list_resource, name='api_book_list'),
83     url(tags_re + r'ebooks/' + paginate_re,
84         ebook_list_resource, name='api_ebook_list'),
85     url(tags_re + r'parent_books/' + paginate_re,
86         book_list_resource, {"top_level": True}, name='api_parent_book_list'),
87     url(tags_re + r'parent_ebooks/' + paginate_re,
88         ebook_list_resource, {"top_level": True}, name='api_parent_ebook_list'),
89     url(tags_re + r'audiobooks/' + paginate_re,
90         book_list_resource, {"audiobooks": True}, name='api_audiobook_list'),
91     url(tags_re + r'daisy/' + paginate_re,
92         book_list_resource, {"daisy": True}, name='api_daisy_list'),
93
94     url(r'^recommended/' + paginate_re, book_list_resource, {"recommended": True}, name='api_recommended_list'),
95     url(r'^newest/', book_list_resource, {"newest": True, "count": 20}, name='api_newest_list'),
96     url(r'^filter-books/(?:title-part/(?P<title_part>[^/]+)/)?(?:author-part/(?P<author_part>[^/]+)/)?'
97         r'(?:lektura/(?P<is_lektura>(?:true|false))/)?(?:audiobook/(?P<is_audiobook>(?:true|false))/)?'
98         + paginate_re, filter_book_resource, name='api_filter_books'),
99
100     url(r'^pictures/$', picture_resource),
101
102     # fragments by book, tags, themes
103     # this should be paged
104     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', fragment_list_resource),
105
106     # tags by category
107     url(r'^(?P<category>[a-z0-9-]+)/$', tag_list_resource, name='api_tag_list'),
108 )