src/notes/api/serializers/core.py
author ymh <ymh.work@gmail.com>
Wed, 19 Jul 2017 15:57:13 +0200
changeset 119 8ff8e2aee0f9
parent 117 9864fe2067cd
child 131 adad5563603c
permissions -rw-r--r--
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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
"""
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
Serializers for model core classes
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
     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
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from rest_framework import serializers
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
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
logger = logging.getLogger(__name__)
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
class DetailNoteSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
        model = Note
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
        fields = (
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
            'ext_id', 'version', 'created', 'updated',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
            'plain', 'html', 'raw',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
            'categorization', 'margin_note', 'tc_start', 'tc_end'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
        read_only_fields = ('ext_id', 'version', 'created', 'updated')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
83
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    22
class UpdateNoteSerializer(serializers.ModelSerializer):
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    23
    class Meta:
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    24
        model = Note
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    25
        fields = (
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    26
            'ext_id', 'version', 'created', 'updated',
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    27
            'plain', 'html', 'raw',
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    28
            '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
    29
        )
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    30
        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
    31
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
    32
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
    33
    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
    34
        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
    35
        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
    36
            '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
    37
            '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
    38
            '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
    39
        )
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
        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
    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
    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
    43
        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
    44
        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
    45
            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
    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
        return super_data
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
class ListNoteSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        model = Note
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        fields = (
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            'ext_id', 'tc_start', 'tc_end'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        read_only_fields = ('ext_id', )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
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
    57
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
    58
    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
    59
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
    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
    61
        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
    62
        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
    63
            '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
    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
        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
    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
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
    68
    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
    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
    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
    71
        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
    72
        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
    73
            '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
    74
            '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
    75
            '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
    76
            '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
    77
        )
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
        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
    79
            '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
    80
            '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
    81
            '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
    82
            '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
    83
        )
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
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
class ListSessionSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    owner = serializers.SlugRelatedField(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        read_only=True, slug_field='username', default=serializers.CurrentUserDefault())
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        model = Session
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
    94
            'ext_id', 'version', 'date', 'created', 'updated',
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
            'owner', 'title', 'description', 'protocol'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        read_only_fields = ('ext_id', 'version', 'created', 'updated', 'owner')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
class DetailSessionSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    owner = serializers.SlugRelatedField(read_only=True, slug_field='username')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
    notes = DetailNoteSerializer(many=True, read_only=True)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        model = Session
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   108
            'ext_id', 'version', 'date', 'created', 'updated',
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
            'owner', 'title', 'description', 'protocol',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
            'notes'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        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
   113
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   114
class CreateSessionSerializer(serializers.ModelSerializer):
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   115
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   116
    owner = serializers.SlugRelatedField(
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   117
        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
   118
117
9864fe2067cd Add api endpoints for group management
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
   119
68
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   120
    class Meta:
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   121
        model = Session
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   122
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   123
            '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
   124
            'owner', 'title', 'description', 'protocol'
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   125
        )
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   126
        read_only_fields = ('version', 'created', 'updated', 'owner')