| author | ymh <ymh.work@gmail.com> |
| Thu, 22 Jun 2017 12:09:48 +0200 | |
| changeset 74 | 043477fd5c5c |
| parent 71 | 75dc1e794cf4 |
| child 83 | 76a4e4b11762 |
| 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 |
|
|
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
|
6 |
from notes.models import Note, Session |
| 31 | 7 |
from rest_framework import serializers |
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 |
logger = logging.getLogger(__name__) |
| 31 | 10 |
|
11 |
||
12 |
class DetailNoteSerializer(serializers.ModelSerializer): |
|
13 |
class Meta: |
|
14 |
model = Note |
|
15 |
fields = ( |
|
16 |
'ext_id', 'version', 'created', 'updated', |
|
17 |
'plain', 'html', 'raw', |
|
18 |
'categorization', 'margin_note', 'tc_start', 'tc_end' |
|
19 |
) |
|
20 |
read_only_fields = ('ext_id', 'version', 'created', 'updated') |
|
21 |
||
|
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
|
22 |
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
|
23 |
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
|
24 |
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
|
25 |
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
|
26 |
'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
|
27 |
'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
|
28 |
'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
|
29 |
) |
|
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
|
30 |
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
|
31 |
|
|
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
|
32 |
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
|
33 |
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
|
34 |
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
|
35 |
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
|
36 |
) |
|
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 |
return super_data |
| 31 | 38 |
|
39 |
class ListNoteSerializer(serializers.ModelSerializer): |
|
40 |
class Meta: |
|
41 |
model = Note |
|
42 |
fields = ( |
|
43 |
'ext_id', 'tc_start', 'tc_end' |
|
44 |
) |
|
45 |
read_only_fields = ('ext_id', ) |
|
46 |
||
47 |
||
48 |
class ListSessionSerializer(serializers.ModelSerializer): |
|
49 |
||
50 |
owner = serializers.SlugRelatedField( |
|
51 |
read_only=True, slug_field='username', default=serializers.CurrentUserDefault()) |
|
52 |
||
53 |
class Meta: |
|
54 |
model = Session |
|
55 |
fields = ( |
|
| 71 | 56 |
'ext_id', 'version', 'date', 'created', 'updated', |
| 31 | 57 |
'owner', 'title', 'description', 'protocol' |
58 |
) |
|
59 |
read_only_fields = ('ext_id', 'version', 'created', 'updated', 'owner') |
|
60 |
||
61 |
||
62 |
class DetailSessionSerializer(serializers.ModelSerializer): |
|
63 |
||
64 |
owner = serializers.SlugRelatedField(read_only=True, slug_field='username') |
|
65 |
notes = DetailNoteSerializer(many=True, read_only=True) |
|
66 |
||
67 |
class Meta: |
|
68 |
model = Session |
|
69 |
fields = ( |
|
| 71 | 70 |
'ext_id', 'version', 'date', 'created', 'updated', |
| 31 | 71 |
'owner', 'title', 'description', 'protocol', |
72 |
'notes' |
|
73 |
) |
|
74 |
read_only_fields = ('ext_id', 'version', 'created', 'updated', 'owner') |
|
|
68
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
75 |
|
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
76 |
class CreateSessionSerializer(serializers.ModelSerializer): |
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
77 |
|
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
78 |
owner = serializers.SlugRelatedField( |
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
79 |
read_only=True, slug_field='username', default=serializers.CurrentUserDefault()) |
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
80 |
|
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
81 |
class Meta: |
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
82 |
model = Session |
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
83 |
fields = ( |
| 71 | 84 |
'ext_id', 'version', 'date', 'created', 'updated', |
|
68
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
85 |
'owner', 'title', 'description', 'protocol' |
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
86 |
) |
|
6e18b31b0ad5
Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents:
31
diff
changeset
|
87 |
read_only_fields = ('version', 'created', 'updated', 'owner') |