f9f9c2279b064e8167894c930ad2bd81c63b85da
[wolnelektury.git] / apps / api / urls.py
1 # -*- coding: utf-8 -*-
2 from django.conf.urls.defaults import *
3 from django.views.decorators.csrf import csrf_exempt
4 from piston.authentication import OAuthAuthentication, oauth_access_token 
5 from piston.resource import Resource
6
7 from api import handlers
8 from api.helpers import CsrfExemptResource
9
10 auth = OAuthAuthentication(realm="Wolne Lektury")
11
12 book_changes_resource = Resource(handler=handlers.BookChangesHandler)
13 tag_changes_resource = Resource(handler=handlers.TagChangesHandler)
14 changes_resource = Resource(handler=handlers.ChangesHandler)
15
16 book_list_resource = CsrfExemptResource(handler=handlers.BooksHandler, authentication=auth)
17 #book_list_resource = Resource(handler=handlers.BooksHandler)
18 book_resource = Resource(handler=handlers.BookDetailHandler)
19
20 tag_list_resource = Resource(handler=handlers.TagsHandler)
21 tag_resource = Resource(handler=handlers.TagDetailHandler)
22
23 fragment_resource = Resource(handler=handlers.FragmentDetailHandler)
24 fragment_list_resource = Resource(handler=handlers.FragmentsHandler)
25
26 picture_resource = CsrfExemptResource(handler=handlers.PictureHandler, authentication=auth)
27
28 urlpatterns = patterns(
29     'piston.authentication',
30     url(r'^oauth/request_token/$', 'oauth_request_token'),
31     url(r'^oauth/authorize/$', 'oauth_user_auth'),
32     url(r'^oauth/access_token/$', csrf_exempt(oauth_access_token)),
33
34 ) + patterns('',
35     url(r'^$', 'django.views.generic.simple.direct_to_template',
36             {'template': 'api/main.html'}, name='api'),
37
38
39     # changes handlers
40     url(r'^book_changes/(?P<since>\d*?)\.(?P<emitter_format>xml|json|yaml)$', book_changes_resource),
41     url(r'^tag_changes/(?P<since>\d*?)\.(?P<emitter_format>xml|json|yaml)$', tag_changes_resource),
42     # used by mobile app
43     url(r'^changes/(?P<since>\d*?)\.(?P<emitter_format>xml|json|yaml)$', changes_resource),
44
45     # info boxes (used by mobile app)
46     url(r'book/(?P<id>\d*?)/info\.html$', 'catalogue.views.book_info'),
47     url(r'tag/(?P<id>\d*?)/info\.html$', 'catalogue.views.tag_info'),
48
49
50     # objects details
51     url(r'^books/(?P<book>[a-z0-9-]+)/$', book_resource, name="api_book"),
52     url(r'^(?P<category>[a-z0-9-]+)/(?P<slug>[a-z0-9-]+)/$',
53         tag_resource, name="api_tag"),
54     url(r'^books/(?P<book>[a-z0-9-]+)/fragments/(?P<anchor>[a-z0-9-]+)/$',
55         fragment_resource, name="api_fragment"),
56
57     # books by tags
58     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})books/$',
59         book_list_resource, name='api_book_list'),
60     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})parent_books/$',
61         book_list_resource, {"top_level": True}, name='api_parent_book_list'),
62     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})audiobooks/$',
63         book_list_resource, {"audiobooks": True}, name='api_audiobook_list'),
64     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})daisy/$',
65         book_list_resource, {"daisy": True}, name='api_daisy_list'),
66
67     url(r'^pictures/$', picture_resource),
68
69     # fragments by book, tags, themes
70     # this should be paged
71     url(r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){1,6})fragments/$', fragment_list_resource),
72
73     # tags by category
74     url(r'^(?P<category>[a-z0-9-]+)/$', tag_list_resource, name='api_tag_list'),
75 )