src/notes/api/serializers/core.py
author ymh <ymh.work@gmail.com>
Fri, 23 Jun 2017 17:58:21 +0200
changeset 83 76a4e4b11762
parent 74 043477fd5c5c
child 117 9864fe2067cd
permissions -rw-r--r--
add server request for note update and delete
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
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
class ListSessionSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    owner = serializers.SlugRelatedField(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        read_only=True, slug_field='username', default=serializers.CurrentUserDefault())
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        model = Session
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
    66
            'ext_id', 'version', 'date', 'created', 'updated',
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            'owner', 'title', 'description', 'protocol'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        read_only_fields = ('ext_id', 'version', 'created', 'updated', 'owner')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
class DetailSessionSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    owner = serializers.SlugRelatedField(read_only=True, slug_field='username')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    notes = DetailNoteSerializer(many=True, read_only=True)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        model = Session
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
    80
            'ext_id', 'version', 'date', 'created', 'updated',
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            'owner', 'title', 'description', 'protocol',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            'notes'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
        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
    85
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    86
class CreateSessionSerializer(serializers.ModelSerializer):
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    87
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    88
    owner = serializers.SlugRelatedField(
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    89
        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
    90
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    91
    class Meta:
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    92
        model = Session
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
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',
68
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    95
            'owner', 'title', 'description', 'protocol'
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    96
        )
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
    97
        read_only_fields = ('version', 'created', 'updated', 'owner')