Merge commit 'afb3cc28'
[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, 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
14 from . import views
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 filter_book_resource = auth_resource(handler=handlers.FilterBooksHandler)
46
47 picture_resource = auth_resource(handler=handlers.PictureHandler)
48
49 blog_resource = Resource(handler=handlers.BlogEntryHandler)
50
51
52 tags_re = r'^(?P<tags>(?:(?:[a-z0-9-]+/){2}){0,6})'
53 paginate_re = r'(?:after/(?P<after>[a-z0-9-]+)/)?(?:count/(?P<count>[0-9]+)/)?$'
54
55
56 urlpatterns = [
57     url(r'^oauth/request_token/$', oauth_request_token),
58     url(r'^oauth/authorize/$', oauth_user_auth, name='oauth_user_auth'),
59     url(r'^oauth/access_token/$', csrf_exempt(oauth_access_token)),
60
61     url(r'^$', TemplateView.as_view(template_name='api/main.html'), name='api'),
62
63     # info boxes (used by mobile app)
64     url(r'book/(?P<book_id>\d*?)/info\.html$', catalogue.views.book_info),
65     url(r'tag/(?P<tag_id>\d*?)/info\.html$', catalogue.views.tag_info),
66
67     # reading data
68     url(r'^reading/(?P<slug>[a-z0-9-]+)/$', views.BookUserDataView.as_view(), name='api_reading'),
69     url(r'^reading/(?P<slug>[a-z0-9-]+)/(?P<state>[a-z]+)/$', views.BookUserDataView.as_view(), name='api_reading'),
70     url(r'^username/$', views.UserView.as_view(), name='api_username'),
71
72     # books by tags
73     url(tags_re + r'ebooks/' + paginate_re,
74         ebook_list_resource, name='api_ebook_list'),
75     url(tags_re + r'parent_ebooks/' + paginate_re,
76         ebook_list_resource, {"top_level": True}, name='api_parent_ebook_list'),
77
78     url(r'^filter-books/$', filter_book_resource, name='api_filter_books'),
79
80     url(r'^pictures/$', picture_resource),
81
82     url(r'^blog/$', blog_resource),
83
84     url(r'^', include('social.api.urls')),
85     url(r'^', include('catalogue.api.urls')),
86 ]