| author | ymh <ymh.work@gmail.com> |
| Wed, 18 Jul 2018 17:32:09 +0200 | |
| changeset 142 | 56850f5c73f6 |
| parent 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 |
| 142 | 7 |
from django.db import transaction |
8 |
from notes.api.fields.category import ProtocolField |
|
9 |
from notes import constants |
|
10 |
from notes.models import Note, Session |
|
11 |
from protocols.models import Protocol |
|
|
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
|
12 |
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
|
13 |
|
|
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
|
14 |
logger = logging.getLogger(__name__) |
| 31 | 15 |
|
16 |
||
17 |
class DetailNoteSerializer(serializers.ModelSerializer): |
|
18 |
class Meta: |
|
19 |
model = Note |
|
20 |
fields = ( |
|
21 |
'ext_id', 'version', 'created', 'updated', |
|
22 |
'plain', 'html', 'raw', |
|
23 |
'categorization', 'margin_note', 'tc_start', 'tc_end' |
|
24 |
) |
|
25 |
read_only_fields = ('ext_id', 'version', 'created', 'updated') |
|
26 |
||
|
83
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
27 |
class UpdateNoteSerializer(serializers.ModelSerializer): |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
28 |
class Meta: |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
29 |
model = Note |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
30 |
fields = ( |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
31 |
'ext_id', 'version', 'created', 'updated', |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
32 |
'plain', 'html', 'raw', |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
33 |
'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
|
34 |
) |
|
76a4e4b11762
add server request for note update and delete
ymh <ymh.work@gmail.com>
parents:
74
diff
changeset
|
35 |
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
|
36 |
|
|
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
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
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
|
41 |
'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
|
42 |
'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
|
43 |
'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
|
44 |
) |
|
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 |
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
|
46 |
|
|
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 |
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
|
48 |
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
|
49 |
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
|
50 |
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
|
51 |
) |
|
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
|
52 |
return super_data |
| 31 | 53 |
|
54 |
class ListNoteSerializer(serializers.ModelSerializer): |
|
55 |
class Meta: |
|
56 |
model = Note |
|
57 |
fields = ( |
|
58 |
'ext_id', 'tc_start', 'tc_end' |
|
59 |
) |
|
60 |
read_only_fields = ('ext_id', ) |
|
61 |
||
|
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 |
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
|
63 |
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
|
64 |
|
|
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 |
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
|
66 |
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
|
67 |
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
|
68 |
'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
|
69 |
) |
|
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 |
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
|
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 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
|
73 |
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
|
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 |
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
|
76 |
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
|
77 |
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
|
78 |
'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
|
79 |
'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
|
80 |
'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
|
81 |
'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
|
82 |
) |
|
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 |
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
|
84 |
'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
|
85 |
'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
|
86 |
'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
|
87 |
'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
|
88 |
) |
|
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 |
|
| 31 | 90 |
|
91 |
class ListSessionSerializer(serializers.ModelSerializer): |
|
92 |
||
93 |
owner = serializers.SlugRelatedField( |
|
94 |
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
|
95 |
group = serializers.SlugRelatedField(read_only=True, slug_field='name') |
| 142 | 96 |
protocol = ProtocolField(required=False, read_only=True) |
| 31 | 97 |
|
98 |
class Meta: |
|
99 |
model = Session |
|
100 |
fields = ( |
|
| 71 | 101 |
'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
|
102 |
'owner', 'title', 'description', 'protocol', 'group' |
| 31 | 103 |
) |
|
133
6f3078f7fd47
Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents:
131
diff
changeset
|
104 |
read_only_fields = ('ext_id', 'version', 'created', 'updated', 'owner', 'group', 'protocol') |
| 31 | 105 |
|
106 |
||
107 |
class DetailSessionSerializer(serializers.ModelSerializer): |
|
108 |
||
109 |
owner = serializers.SlugRelatedField(read_only=True, slug_field='username') |
|
110 |
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
|
111 |
group = serializers.SlugRelatedField(slug_field='name', read_only=True) |
| 142 | 112 |
protocol = ProtocolField(required=False, 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
|
113 |
|
|
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 |
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
|
115 |
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
|
116 |
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
|
117 |
'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
|
118 |
'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
|
119 |
'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
|
120 |
) |
|
133
6f3078f7fd47
Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents:
131
diff
changeset
|
121 |
read_only_fields = ('ext_id', 'version', 'created', 'updated', 'owner', 'group', 'protocol') |
|
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
|
122 |
|
| 142 | 123 |
class BaseSessionSerializer(serializers.ModelSerializer): |
|
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
|
124 |
|
|
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
|
125 |
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
|
126 |
read_only=True, slug_field='username', default=serializers.CurrentUserDefault()) |
|
133
6f3078f7fd47
Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents:
131
diff
changeset
|
127 |
group = serializers.SlugRelatedField(slug_field='name', queryset=Group.objects.all(), required=False, allow_null=True) |
| 142 | 128 |
|
129 |
@transaction.atomic() |
|
130 |
def save(self,**kwargs): |
|
131 |
return super().save(**kwargs) |
|
132 |
||
133 |
def create(self, validated_data): |
|
134 |
return super().create(validated_data) |
|
135 |
||
136 |
def update(self, instance, validated_data): |
|
137 |
return super().update(instance, validated_data) |
|
138 |
||
139 |
def validate(self, data): |
|
140 |
data = super().validate(data) |
|
141 |
||
142 |
group = data.get('group') |
|
143 |
owner = data.get('owner') |
|
144 |
||
145 |
if owner is None: |
|
146 |
owner = self.fields['owner'].get_default() |
|
147 |
||
148 |
if group is None: |
|
149 |
if owner.profile and owner.profile.default_group: |
|
150 |
group = owner.profile.default_group |
|
151 |
if group is None and owner: |
|
152 |
group = Group.objects.filter(profile__owner_personal=owner).first() |
|
153 |
||
154 |
if group is None: |
|
155 |
raise serializers.ValidationError("group field is required or default group or personal group could not be found for owner") |
|
156 |
elif not owner in group.user_set.all(): |
|
157 |
raise serializers.ValidationError("Owner must be in group") |
|
158 |
||
159 |
# Be careful: we update the data in the "validate". This is a side effect. |
|
160 |
data['group'] = group |
|
161 |
data['owner'] = owner |
|
162 |
||
163 |
protocol = data.get('protocol') |
|
164 |
if protocol is None: |
|
165 |
data['protocol'] = group.profile.protocol |
|
166 |
elif isinstance(protocol, dict) and 'owner' not in protocol: |
|
167 |
data['protocol']['owner'] = group.name |
|
168 |
||
169 |
return data |
|
170 |
||
171 |
class UpdateSessionSerializer(BaseSessionSerializer): |
|
172 |
||
173 |
protocol = ProtocolField(required=False, allow_null=True, read_only=True) |
|
174 |
||
175 |
class Meta: |
|
176 |
model = Session |
|
177 |
fields = ( |
|
178 |
'ext_id', 'version', 'date', 'created', 'updated', |
|
179 |
'owner', 'title', 'description', 'protocol', 'group' |
|
180 |
) |
|
181 |
read_only_fields = ('ext_id', 'protocol', 'version', 'created', 'updated', 'owner') |
|
182 |
||
183 |
@transaction.atomic() |
|
184 |
def save(self,**kwargs): |
|
185 |
return super().save(**kwargs) |
|
186 |
||
187 |
class CreateSessionSerializer(BaseSessionSerializer): |
|
188 |
||
189 |
protocol = ProtocolField(required=False, allow_null=True) |
|
| 31 | 190 |
|
191 |
class Meta: |
|
192 |
model = Session |
|
193 |
fields = ( |
|
| 71 | 194 |
'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
|
195 |
'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
|
196 |
) |
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
197 |
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
|
198 |
|
| 142 | 199 |
@transaction.atomic() |
200 |
def save(self,**kwargs): |
|
201 |
return super().save(**kwargs) |
|
|
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
|
202 |
|
| 142 | 203 |
def create(self, validated_data): |
204 |
protocol = validated_data.pop('protocol') |
|
205 |
||
206 |
if protocol is None or isinstance(protocol, dict): |
|
|
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
|
207 |
|
| 142 | 208 |
protocol_revision = Protocol.objects.create_new_revision(protocol.get('id'), protocol, None) |
209 |
validated_data['protocol'] = "%s%s.%s" % (constants.PROTOCOL_URN_PREFIX, protocol_revision.protocol.ext_id, protocol_revision.version) |
|
|
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
|
210 |
|
| 142 | 211 |
elif isinstance(protocol, str) and protocol.startswith(constants.PROTOCOL_URN_PREFIX): |
212 |
validated_data['protocol'] = protocol |
|
213 |
else: |
|
214 |
raise Exception("Bad format for protocol") |
|
|
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
|
215 |
|
| 142 | 216 |
return super().create(validated_data) |