src/notes/api/serializers/core.py
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--
add api call to save notes. internally use ts for time data for notes and session
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
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
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
class ListNoteSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        model = Note
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        fields = (
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            'ext_id', 'tc_start', 'tc_end'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        read_only_fields = ('ext_id', )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
class ListSessionSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    owner = serializers.SlugRelatedField(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        read_only=True, slug_field='username', default=serializers.CurrentUserDefault())
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        model = Session
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
    56
            'ext_id', 'version', 'date', 'created', 'updated',
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            'owner', 'title', 'description', 'protocol'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        read_only_fields = ('ext_id', 'version', 'created', 'updated', 'owner')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
class DetailSessionSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    owner = serializers.SlugRelatedField(read_only=True, slug_field='username')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    notes = DetailNoteSerializer(many=True, read_only=True)
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        model = Session
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
    70
            'ext_id', 'version', 'date', 'created', 'updated',
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
            'owner', 'title', 'description', 'protocol',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
            'notes'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    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
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
    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')