| author | ymh <ymh.work@gmail.com> |
| Wed, 19 Jul 2017 16:20:29 +0200 | |
| changeset 120 | 892980a3af09 |
| parent 119 | 8ff8e2aee0f9 |
| child 128 | 34a75bd8d0b9 |
| 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 |
| 31 | 8 |
from notes.models import Note, Session |
|
117
9864fe2067cd
Add api endpoints for group management
ymh <ymh.work@gmail.com>
parents:
99
diff
changeset
|
9 |
from rest_framework import viewsets |
| 31 | 10 |
|
11 |
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
|
12 |
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
|
13 |
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
|
14 |
ListNoteSerializer, ListSessionSerializer, |
|
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 |
RootDetailNoteSerializer, |
|
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 |
RootListNoteSerializer, UpdateNoteSerializer) |
| 31 | 17 |
|
18 |
logger = logging.getLogger(__name__) |
|
19 |
||
20 |
||
21 |
class SessionViewSet(viewsets.ModelViewSet): |
|
22 |
""" |
|
23 |
API endpoint that allow sessions ro be viewed or edited |
|
24 |
""" |
|
25 |
serializer_class = ListSessionSerializer |
|
26 |
lookup_field = 'ext_id' |
|
27 |
||
28 |
serializers = { |
|
29 |
'list': ListSessionSerializer, |
|
30 |
'retrieve': DetailSessionSerializer, |
|
|
68
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
31 |
'create': CreateSessionSerializer, |
| 31 | 32 |
} |
33 |
||
34 |
permission_classes = (SessionPermission,) |
|
35 |
||
36 |
def get_serializer_class(self): |
|
37 |
return self.serializers.get(self.action, ListSessionSerializer) |
|
38 |
||
39 |
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
|
40 |
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
|
41 |
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
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
) |
|
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 |
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
|
48 |
return queryset |
| 31 | 49 |
|
50 |
||
51 |
class NoteViewSet(viewsets.ModelViewSet): |
|
52 |
||
53 |
serializers = { |
|
54 |
'list': ListNoteSerializer, |
|
55 |
'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
|
56 |
'create': CreateNoteSerializer, |
|
83
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
57 |
'update': UpdateNoteSerializer, |
| 31 | 58 |
} |
59 |
lookup_field = 'ext_id' |
|
60 |
||
61 |
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
|
62 |
pagination_class = None |
| 31 | 63 |
|
64 |
def get_serializer_class(self): |
|
65 |
return self.serializers.get(self.action, ListNoteSerializer) |
|
66 |
||
67 |
def get_queryset(self): |
|
68 |
return Note.objects.filter( |
|
69 |
session__ext_id=self.kwargs['session_ext_id'], |
|
70 |
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
|
71 |
|
|
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
|
72 |
|
|
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
|
73 |
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
|
74 |
|
|
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 |
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
|
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 |
permission_classes = (NotePermission,) |
|
120
892980a3af09
for notes api, the end point list all properties.
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
78 |
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
|
79 |
|
|
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 |
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
|
81 |
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
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
) |
|
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 = 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
|
89 |
return queryset |