src/notes/api/serializers/core.py
author ymh <ymh.work@gmail.com>
Wed, 18 Jul 2018 17:32:09 +0200
changeset 142 56850f5c73f6
parent 133 6f3078f7fd47
permissions -rw-r--r--
- upgrade libraries - python 3.7 compatibility - First protocol management implementation
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
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
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
     7
from django.db import transaction
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
     8
from notes.api.fields.category import ProtocolField
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
     9
from notes import constants
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    10
from notes.models import Note, Session
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    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
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
class DetailNoteSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        model = Note
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
        fields = (
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
            'ext_id', 'version', 'created', 'updated',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
            'plain', 'html', 'raw',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
            'categorization', 'margin_note', 'tc_start', 'tc_end'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        read_only_fields = ('ext_id', 'version', 'created', 'updated')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    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
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
class ListNoteSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        model = Note
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        fields = (
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            'ext_id', 'tc_start', 'tc_end'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        read_only_fields = ('ext_id', )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    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
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 ListSessionSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
    owner = serializers.SlugRelatedField(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    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
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
    96
    protocol = ProtocolField(required=False, read_only=True)
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        model = Session
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   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
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   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
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
class DetailSessionSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
    owner = serializers.SlugRelatedField(read_only=True, slug_field='username')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   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
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   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
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   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
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   128
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   129
    @transaction.atomic()
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   130
    def save(self,**kwargs):
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   131
        return super().save(**kwargs)
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   132
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   133
    def create(self, validated_data):
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   134
        return super().create(validated_data)
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   135
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   136
    def update(self, instance, validated_data):
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   137
        return super().update(instance, validated_data)
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   138
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   139
    def validate(self, data):
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   140
        data = super().validate(data)
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   141
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   142
        group = data.get('group')
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   143
        owner = data.get('owner')
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   144
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   145
        if owner is None:
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   146
            owner = self.fields['owner'].get_default()
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   147
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   148
        if group is None:
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   149
            if owner.profile and owner.profile.default_group:
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   150
                group = owner.profile.default_group
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   151
            if group is None and owner:
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   152
                group = Group.objects.filter(profile__owner_personal=owner).first()
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   153
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   154
        if group is None:
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   155
            raise serializers.ValidationError("group field is required or default group or personal group could not be found for owner")
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   156
        elif not owner in group.user_set.all():
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   157
            raise serializers.ValidationError("Owner must be in group")
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   158
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   159
        # Be careful: we update the data in the "validate". This is a side effect.
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   160
        data['group'] = group
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   161
        data['owner'] = owner
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   162
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   163
        protocol = data.get('protocol')
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   164
        if protocol is None:
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   165
            data['protocol'] = group.profile.protocol
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   166
        elif isinstance(protocol, dict) and 'owner' not in protocol:
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   167
            data['protocol']['owner'] = group.name
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   168
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   169
        return data
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   170
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   171
class UpdateSessionSerializer(BaseSessionSerializer):
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   172
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   173
    protocol = ProtocolField(required=False, allow_null=True, read_only=True)
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   174
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   175
    class Meta:
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   176
        model = Session
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   177
        fields = (
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   178
            'ext_id', 'version', 'date', 'created', 'updated',
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   179
            'owner', 'title', 'description', 'protocol', 'group'
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   180
        )
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   181
        read_only_fields = ('ext_id', 'protocol', 'version', 'created', 'updated', 'owner')
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   182
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   183
    @transaction.atomic()
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   184
    def save(self,**kwargs):
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   185
        return super().save(**kwargs)
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   186
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   187
class CreateSessionSerializer(BaseSessionSerializer):
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   188
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   189
    protocol = ProtocolField(required=False, allow_null=True)
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
        model = Session
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   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
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   199
    @transaction.atomic()
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   200
    def save(self,**kwargs):
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   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
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   203
    def create(self, validated_data):
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   204
        protocol = validated_data.pop('protocol')
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   205
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   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
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   208
            protocol_revision = Protocol.objects.create_new_revision(protocol.get('id'), protocol, None)
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   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
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   211
        elif isinstance(protocol, str) and protocol.startswith(constants.PROTOCOL_URN_PREFIX):
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   212
            validated_data['protocol'] = protocol
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   213
        else:
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   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
56850f5c73f6 - upgrade libraries
ymh <ymh.work@gmail.com>
parents: 133
diff changeset
   216
        return super().create(validated_data)