App compatibility fix + some error handling.
[wolnelektury.git] / src / api / views.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.http import Http404
6 from django.views.generic.base import View
7 from oauthlib.common import urlencode
8 from oauthlib.oauth1 import RequestTokenEndpoint, AccessTokenEndpoint
9 from piston.models import KEY_SIZE, SECRET_SIZE
10 from rest_framework.permissions import IsAuthenticated
11 from rest_framework.response import Response
12 from rest_framework.views import APIView
13 from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404
14 from migdal.models import Entry
15 from catalogue.models import Book
16 from .models import BookUserData
17 from . import serializers
18 from .request_validator import PistonRequestValidator
19 from .utils import oauthlib_request, oauthlib_response
20
21
22 class OAuth1RequestTokenEndpoint(RequestTokenEndpoint):
23     def _create_request(self, *args, **kwargs):
24         r = super(OAuth1RequestTokenEndpoint, self)._create_request(*args, **kwargs)
25         r.redirect_uri = 'oob'
26         return r
27
28     def create_request_token(self, request, credentials):
29         token = {
30             'oauth_token': self.token_generator()[:KEY_SIZE],
31             'oauth_token_secret': self.token_generator()[:SECRET_SIZE],
32         }
33         token.update(credentials)
34         self.request_validator.save_request_token(token, request)
35         return urlencode(token.items())
36
37
38 class OAuth1RequestTokenView(View):
39     def __init__(self):
40         self.endpoint = OAuth1RequestTokenEndpoint(PistonRequestValidator())
41
42     def dispatch(self, request):
43         return oauthlib_response(
44             self.endpoint.create_request_token_response(
45                 **oauthlib_request(request)
46             )
47         )
48
49
50 class OAuth1AccessTokenEndpoint(AccessTokenEndpoint):
51     def _create_request(self, *args, **kwargs):
52         r = super(OAuth1AccessTokenEndpoint, self)._create_request(*args, **kwargs)
53         r.verifier = 'x' * 20
54         return r
55
56     def create_access_token(self, request, credentials):
57         request.realms = self.request_validator.get_realms(
58             request.resource_owner_key, request)
59         token = {
60             'oauth_token': self.token_generator()[:KEY_SIZE],
61             'oauth_token_secret': self.token_generator()[:SECRET_SIZE],
62             'oauth_authorized_realms': ' '.join(request.realms)
63         }
64         token.update(credentials)
65         self.request_validator.save_access_token(token, request)
66         return urlencode(token.items())
67
68
69 class OAuth1AccessTokenView(View):
70     def __init__(self):
71         self.endpoint = OAuth1AccessTokenEndpoint(PistonRequestValidator())
72
73     def dispatch(self, request):
74         return oauthlib_response(
75             self.endpoint.create_access_token_response(
76                 **oauthlib_request(request)
77             )
78         )
79
80
81 class UserView(RetrieveAPIView):
82     permission_classes = [IsAuthenticated]
83     serializer_class = serializers.UserSerializer
84
85     def get_object(self):
86         return self.request.user
87
88
89 class BookUserDataView(RetrieveAPIView):
90     permission_classes = [IsAuthenticated]
91     serializer_class = serializers.BookUserDataSerializer
92     lookup_field = 'book__slug'
93     lookup_url_kwarg = 'slug'
94
95     def get_queryset(self):
96         return BookUserData.objects.filter(user=self.request.user)
97
98     def get(self, *args, **kwargs):
99         try:
100             return super(BookUserDataView, self).get(*args, **kwargs)
101         except Http404:
102             return Response({"state": "not_started"})
103
104     def post(self, request, slug, state):
105         if state not in ('reading', 'complete'):
106             raise Http404
107
108         book = get_object_or_404(Book, slug=slug)
109         instance = BookUserData.update(book, request.user, state)
110         serializer = self.get_serializer(instance)
111         return Response(serializer.data)
112
113
114 class BlogView(ListAPIView):
115     serializer_class = serializers.BlogSerializer
116
117     def get_queryset(self):
118         after = self.request.query_params.get('after')
119         count = int(self.request.query_params.get('count', 20))
120         entries = Entry.published_objects.filter(in_stream=True).order_by('-first_published_at')
121         if after:
122             entries = entries.filter(first_published_at__lt=after)
123         if count:
124             entries = entries[:count]
125         return entries