| author | ymh <ymh.work@gmail.com> |
| Mon, 31 Jul 2017 23:18:38 +0200 | |
| changeset 131 | adad5563603c |
| parent 119 | 8ff8e2aee0f9 |
| child 133 | 6f3078f7fd47 |
| permissions | -rw-r--r-- |
| 31 | 1 |
""" |
2 |
Serializers for model core classes |
|
3 |
""" |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
4 |
import logging |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
5 |
|
|
131
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
6 |
from django.contrib.auth.models import Group |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
7 |
from rest_framework import serializers |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
8 |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
9 |
from notes.models import Note, Session |
| 31 | 10 |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
11 |
logger = logging.getLogger(__name__) |
| 31 | 12 |
|
13 |
||
14 |
class DetailNoteSerializer(serializers.ModelSerializer): |
|
15 |
class Meta: |
|
16 |
model = Note |
|
17 |
fields = ( |
|
18 |
'ext_id', 'version', 'created', 'updated', |
|
19 |
'plain', 'html', 'raw', |
|
20 |
'categorization', 'margin_note', 'tc_start', 'tc_end' |
|
21 |
) |
|
22 |
read_only_fields = ('ext_id', 'version', 'created', 'updated') |
|
23 |
||
|
83
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
24 |
class UpdateNoteSerializer(serializers.ModelSerializer): |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
25 |
class Meta: |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
26 |
model = Note |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
27 |
fields = ( |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
28 |
'ext_id', 'version', 'created', 'updated', |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
29 |
'plain', 'html', 'raw', |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
30 |
'categorization', 'margin_note', 'tc_start', 'tc_end' |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
31 |
) |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
32 |
read_only_fields = ('ext_id', 'version', 'created', 'updated', 'tc_start', 'tc_end') |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
33 |
|
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
34 |
class CreateNoteSerializer(serializers.ModelSerializer): |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
35 |
class Meta: |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
36 |
model = Note |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
37 |
fields = ( |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
38 |
'ext_id', 'version', 'created', 'updated', |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
39 |
'plain', 'html', 'raw', |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
40 |
'categorization', 'margin_note', 'tc_start', 'tc_end' |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
41 |
) |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
42 |
read_only_fields = ('version', 'created', 'updated') |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
43 |
|
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
44 |
def to_internal_value(self, data): |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
45 |
super_data = super().to_internal_value(data) |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
46 |
super_data['session'] = Session.objects.get( |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
47 |
ext_id=self.context['view'].kwargs['session_ext_id'] |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
48 |
) |
|
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
49 |
return super_data |
| 31 | 50 |
|
51 |
class ListNoteSerializer(serializers.ModelSerializer): |
|
52 |
class Meta: |
|
53 |
model = Note |
|
54 |
fields = ( |
|
55 |
'ext_id', 'tc_start', 'tc_end' |
|
56 |
) |
|
57 |
read_only_fields = ('ext_id', ) |
|
58 |
||
|
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
|
59 |
class RootListNoteSerializer(serializers.ModelSerializer): |
|
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
|
60 |
session = serializers.SlugRelatedField(read_only=True, slug_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
|
61 |
|
|
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 |
class Meta: |
|
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
|
63 |
model = Note |
|
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
|
64 |
fields = ( |
|
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
|
65 |
'ext_id', 'tc_start', 'tc_end', 'session' |
|
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 |
) |
|
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
|
67 |
read_only_fields = ('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
|
68 |
|
|
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
|
69 |
class RootDetailNoteSerializer(serializers.ModelSerializer): |
|
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
|
70 |
session = serializers.SlugRelatedField(read_only=True, slug_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
|
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 |
class Meta: |
|
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 |
model = Note |
|
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 |
fields = ( |
|
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 |
'ext_id', 'version', 'created', 'updated', |
|
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 |
'plain', 'html', 'raw', |
|
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 |
'categorization', 'margin_note', 'tc_start', 'tc_end', |
|
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 |
'session' |
|
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 |
read_only_fields = ( |
|
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 |
'ext_id', 'version', 'created', 'updated', |
|
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 |
'plain', 'html', 'raw', |
|
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 |
'categorization', 'margin_note', 'tc_start', 'tc_end', |
|
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 |
'session' |
|
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 |
) |
|
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 |
|
| 31 | 87 |
|
88 |
class ListSessionSerializer(serializers.ModelSerializer): |
|
89 |
||
90 |
owner = serializers.SlugRelatedField( |
|
91 |
read_only=True, slug_field='username', default=serializers.CurrentUserDefault()) |
|
|
131
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
92 |
group = serializers.SlugRelatedField(read_only=True, slug_field='name') |
| 31 | 93 |
|
94 |
class Meta: |
|
95 |
model = Session |
|
96 |
fields = ( |
|
| 71 | 97 |
'ext_id', 'version', 'date', 'created', 'updated', |
|
131
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
98 |
'owner', 'title', 'description', 'protocol', 'group' |
| 31 | 99 |
) |
|
131
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
100 |
read_only_fields = ('ext_id', 'version', 'created', 'updated', 'owner', 'group') |
| 31 | 101 |
|
102 |
||
103 |
class DetailSessionSerializer(serializers.ModelSerializer): |
|
104 |
||
105 |
owner = serializers.SlugRelatedField(read_only=True, slug_field='username') |
|
106 |
notes = DetailNoteSerializer(many=True, read_only=True) |
|
|
131
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
107 |
group = serializers.SlugRelatedField(slug_field='name', read_only=True) |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
108 |
|
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
109 |
class Meta: |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
110 |
model = Session |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
111 |
fields = ( |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
112 |
'ext_id', 'version', 'date', 'created', 'updated', |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
113 |
'owner', 'title', 'description', 'protocol', 'group', |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
114 |
'notes' |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
115 |
) |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
116 |
read_only_fields = ('ext_id', 'version', 'created', 'updated', 'owner', 'group') |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
117 |
|
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
118 |
class CreateSessionSerializer(serializers.ModelSerializer): |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
119 |
|
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
120 |
owner = serializers.SlugRelatedField( |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
121 |
read_only=True, slug_field='username', default=serializers.CurrentUserDefault()) |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
122 |
group = serializers.SlugRelatedField(slug_field='name', queryset=Group.objects.all(), required=False) |
| 31 | 123 |
|
124 |
class Meta: |
|
125 |
model = Session |
|
126 |
fields = ( |
|
| 71 | 127 |
'ext_id', 'version', 'date', 'created', 'updated', |
|
131
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
128 |
'owner', 'title', 'description', 'protocol', 'group' |
|
68
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
129 |
) |
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
130 |
read_only_fields = ('version', 'created', 'updated', 'owner') |
|
131
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
131 |
|
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
132 |
def validate(self, data): |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
133 |
data = super().validate(data) |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
134 |
|
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
135 |
group = data.get('group') |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
136 |
owner = data.get('owner') |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
137 |
|
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
138 |
if group is None: |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
139 |
if owner and owner.profile and owner.profile.default_group: |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
140 |
group = owner.profile.default_group |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
141 |
if group is None and owner: |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
142 |
group = Group.objects.filter(profile__owner_personal=owner).first() |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
143 |
|
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
144 |
if group is None: |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
145 |
raise serializers.ValidationError("group field is required or default group or personal group could not be found for owner") |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
146 |
elif not owner in group.user_set.all(): |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
147 |
raise serializers.ValidationError("Owner must be in group") |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
148 |
|
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
149 |
data['group'] = group |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
150 |
return data |
|
adad5563603c
add personal group, default_group to users and add group info to session on backend
ymh <ymh.work@gmail.com>
parents:
119
diff
changeset
|
151 |