changes from server
[wolnelektury.git] / apps / 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
21 collection_resource = Resource(handler=handlers.CollectionDetailHandler)
22 collection_list_resource = Resource(handler=handlers.CollectionsHandler)
23
24 tag_list_resource = Resource(handler=handlers.TagsHandler)
25 tag_resource = Resource(handler=handlers.TagDetailHandler)
26
27 fragment_resource = Resource(handler=handlers.FragmentDetailHandler)
28 fragment_list_resource = Resource(handler=handlers.FragmentsHandler)
29
30 picture_resource = CsrfExemptResource(handler=handlers.PictureHandler, authentication=auth)
31
32
33 @ssi_included
34 def incl(request, model, pk, emitter_format):
35     resource = {
36         'book': book_list_resource,
37         'fragment': fragment_list_resource,
38         'tag': tag_list_resource,
39         }[model]
40     request.piwik_track = False
41     resp = resource(request, pk=pk, emitter_format=emitter_format)
42     if emitter_format == 'xml':
43         # Ugly, but quick way of stripping <?xml?> header and <response> tags.
44         resp.content = resp.content[49:-11]
45     return resp
46
47
48 urlpatterns = patterns(
49     'piston.authentication',
50     url(r'^oauth/request_token/$', 'oauth_request_token'),
51     url(r'^oauth/authorize/$', 'oauth_user_auth'),
52     url(r'^oauth/access_token/$', csrf_exempt(oauth_access_token)),
53
54 ) + patterns('',
55     url(r'^$', TemplateView.as_view(template_name='api/main.html'), name='api'),
56     url(r'^include/(?P<model>book|fragment|tag)/(?P<pk>\d+)\.(?P<lang>.+)\.(?P<emitter_format>xml|json)$',
57         incl, name='api_include'),
58
59     # info boxes (used by mobile app)
60     url(r'book/(?P<id>\d*?)/info\.html$', 'catalogue.views.book_info'),
61     url(r'tag/(?P<id>\d*?)/info\.html$', 'catalogue.views.tag_info'),
62
63     # books by collections
64     url(r'^collections/$', collection_list_resource, name="api_collections"),
65     url(r'^collections/(?P<slug>[^/]+)/$', collection_resource, name="api_collection"),
66
67     # objects details
68     url(r'^books/(?P<book>[a-z0-9-]+)/$', book_resource, name="api_book"),
69     url(r'^(?P<category>[a-z0-9-]+)/(?P<slug>[a-z0-9-]+)/$',
70         tag_resource, name="api_tag"),
71     url(r'^books/(?P<book>[a-z0-9-]+)/fragments/(?P<anchor>[a-z0-9-]+)/$',
72         fragment_resource, name="api_fragment"),
73
74     # books by tags
75     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})books/$',
76         book_list_resource, name='api_book_list'),
77     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})ebooks/$',
78         ebook_list_resource, name='api_ebook_list'),
79     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})parent_books/$',
80         book_list_resource, {"top_level": True}, name='api_parent_book_list'),
81     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})parent_ebooks/$',
82         ebook_list_resource, {"top_level": True}, name='api_parent_ebook_list'),
83     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})audiobooks/$',
84         book_list_resource, {"audiobooks": True}, name='api_audiobook_list'),
85     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})daisy/$',
86         book_list_resource, {"daisy": True}, name='api_daisy_list'),
87
88     url(r'^pictures/$', picture_resource),
89
90     # fragments by book, tags, themes
91     # this should be paged
92     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', fragment_list_resource),
93
94     # tags by category
95     url(r'^(?P<category>[a-z0-9-]+)/$', tag_list_resource, name='api_tag_list'),
96 )