| author | ymh <ymh.work@gmail.com> |
| Tue, 25 Jul 2017 19:11:26 +0200 | |
| changeset 128 | 34a75bd8d0b9 |
| parent 120 | 892980a3af09 |
| child 132 | 906a6c7c7943 |
| permissions | -rw-r--r-- |
|
117
9864fe2067cd
Add api endpoints for group management
ymh <ymh.work@gmail.com>
parents:
99
diff
changeset
|
1 |
""" |
|
9864fe2067cd
Add api endpoints for group management
ymh <ymh.work@gmail.com>
parents:
99
diff
changeset
|
2 |
Core viewsets |
|
9864fe2067cd
Add api endpoints for group management
ymh <ymh.work@gmail.com>
parents:
99
diff
changeset
|
3 |
""" |
|
119
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
4 |
import datetime |
| 31 | 5 |
import logging |
6 |
||
|
119
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
7 |
from django.utils import timezone |
|
128
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
120
diff
changeset
|
8 |
from django_filters.rest_framework import DjangoFilterBackend |
| 31 | 9 |
from notes.models import Note, Session |
|
117
9864fe2067cd
Add api endpoints for group management
ymh <ymh.work@gmail.com>
parents:
99
diff
changeset
|
10 |
from rest_framework import viewsets |
| 31 | 11 |
|
|
128
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
120
diff
changeset
|
12 |
from ..filters import NoteFilterSet, SessionFilterSet |
| 31 | 13 |
from ..permissions import NotePermission, SessionPermission |
|
119
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
14 |
from ..serializers.core import (CreateNoteSerializer, CreateSessionSerializer, |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
15 |
DetailNoteSerializer, DetailSessionSerializer, |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
16 |
ListNoteSerializer, ListSessionSerializer, |
|
128
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
120
diff
changeset
|
17 |
RootDetailNoteSerializer, UpdateNoteSerializer) |
| 31 | 18 |
|
19 |
logger = logging.getLogger(__name__) |
|
20 |
||
21 |
||
22 |
class SessionViewSet(viewsets.ModelViewSet): |
|
23 |
""" |
|
24 |
API endpoint that allow sessions ro be viewed or edited |
|
25 |
""" |
|
26 |
serializer_class = ListSessionSerializer |
|
27 |
lookup_field = 'ext_id' |
|
28 |
||
29 |
serializers = { |
|
30 |
'list': ListSessionSerializer, |
|
31 |
'retrieve': DetailSessionSerializer, |
|
|
68
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
32 |
'create': CreateSessionSerializer, |
| 31 | 33 |
} |
34 |
||
35 |
permission_classes = (SessionPermission,) |
|
36 |
||
|
128
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
120
diff
changeset
|
37 |
filter_backends = (DjangoFilterBackend,) |
|
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
120
diff
changeset
|
38 |
filter_class = SessionFilterSet |
|
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
120
diff
changeset
|
39 |
|
| 31 | 40 |
def get_serializer_class(self): |
41 |
return self.serializers.get(self.action, ListSessionSerializer) |
|
42 |
||
43 |
def get_queryset(self): |
|
|
119
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
44 |
queryset = Session.objects.filter(owner=self.request.user).order_by('created') |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
45 |
modified_since_str = self.request.query_params.get('modified_since', None) |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
46 |
if modified_since_str is not None: |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
47 |
modified_since = datetime.datetime.fromtimestamp( |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
48 |
float(modified_since_str), |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
49 |
timezone.utc |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
50 |
) |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
51 |
queryset = queryset.filter(updated__gte=modified_since) |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
52 |
return queryset |
| 31 | 53 |
|
54 |
||
55 |
class NoteViewSet(viewsets.ModelViewSet): |
|
56 |
||
57 |
serializers = { |
|
58 |
'list': ListNoteSerializer, |
|
59 |
'retrieve': DetailNoteSerializer, |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
60 |
'create': CreateNoteSerializer, |
|
83
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
61 |
'update': UpdateNoteSerializer, |
| 31 | 62 |
} |
63 |
lookup_field = 'ext_id' |
|
64 |
||
65 |
permission_classes = (NotePermission,) |
|
|
119
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
66 |
pagination_class = None |
| 31 | 67 |
|
68 |
def get_serializer_class(self): |
|
69 |
return self.serializers.get(self.action, ListNoteSerializer) |
|
70 |
||
71 |
def get_queryset(self): |
|
72 |
return Note.objects.filter( |
|
73 |
session__ext_id=self.kwargs['session_ext_id'], |
|
74 |
session__owner=self.request.user) |
|
|
119
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
75 |
|
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
76 |
|
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
77 |
class RootNoteViewSet(viewsets.ReadOnlyModelViewSet): |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
78 |
|
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
79 |
lookup_field = 'ext_id' |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
80 |
|
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
81 |
permission_classes = (NotePermission,) |
|
120
892980a3af09
for notes api, the end point list all properties.
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
82 |
serializer_class = RootDetailNoteSerializer |
|
119
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
83 |
|
|
128
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
120
diff
changeset
|
84 |
filter_backends = (DjangoFilterBackend,) |
|
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
120
diff
changeset
|
85 |
filter_class = NoteFilterSet |
|
34a75bd8d0b9
add filter on session and node list to recover specific objects
ymh <ymh.work@gmail.com>
parents:
120
diff
changeset
|
86 |
|
|
119
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
87 |
def get_queryset(self): |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
88 |
queryset = Note.objects.filter(session__owner=self.request.user).order_by('created') |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
89 |
modified_since_str = self.request.query_params.get('modified_since', None) |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
90 |
if modified_since_str is not None: |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
91 |
modified_since = datetime.datetime.fromtimestamp( |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
92 |
float(modified_since_str), |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
93 |
timezone.utc |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
94 |
) |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
95 |
queryset = queryset.filter(updated__gte=modified_since) |
|
8ff8e2aee0f9
add parameter to filter session and note by updated date. Add pagination on sessions and notes. add read only endpoint at root level to list notes
ymh <ymh.work@gmail.com>
parents:
117
diff
changeset
|
96 |
return queryset |