src/notes/api/serializers/core.py
author ymh <ymh.work@gmail.com>
Thu, 03 Aug 2017 09:44:37 +0200
changeset 133 6f3078f7fd47
parent 131 adad5563603c
child 142 56850f5c73f6
permissions -rw-r--r--
Work on correct protocol propagation
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
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
     7
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
     8
133
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
     9
from notes.api.fields.category import ProtocolField
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
    10
from notes.models import Note, Session
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
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
    12
logger = logging.getLogger(__name__)
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
class DetailNoteSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        model = Note
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        fields = (
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
            'ext_id', 'version', 'created', 'updated',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
            'plain', 'html', 'raw',
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
            'categorization', 'margin_note', 'tc_start', 'tc_end'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        read_only_fields = ('ext_id', 'version', 'created', 'updated')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
83
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    25
class UpdateNoteSerializer(serializers.ModelSerializer):
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    26
    class Meta:
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    27
        model = Note
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    28
        fields = (
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    29
            'ext_id', 'version', 'created', 'updated',
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    30
            'plain', 'html', 'raw',
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    31
            '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
    32
        )
76a4e4b11762 add server request for note update and delete
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    33
        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
    34
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
    35
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
    36
    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
    37
        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
    38
        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
    39
            '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
    40
            '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
    41
            '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
    42
        )
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
        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
    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
    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
    46
        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
    47
        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
    48
            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
    49
        )
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
        return super_data
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
class ListNoteSerializer(serializers.ModelSerializer):
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 = Note
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        fields = (
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            'ext_id', 'tc_start', 'tc_end'
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        read_only_fields = ('ext_id', )
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
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
    60
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
    61
    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
    62
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
    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
    64
        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
    65
        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
    66
            '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
    67
        )
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
        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
    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 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
    71
    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
    72
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
    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
    74
        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
    75
        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
    76
            '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
    77
            '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
    78
            '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
    79
            '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
    80
        )
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
        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
    82
            '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
    83
            '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
    84
            '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
    85
            '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
    86
        )
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
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
class ListSessionSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
    owner = serializers.SlugRelatedField(
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        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
    93
    group = serializers.SlugRelatedField(read_only=True, slug_field='name')
133
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
    94
    protocol = ProtocolField(required=False)
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        model = Session
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
    99
            '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
   100
            'owner', 'title', 'description', 'protocol', 'group'
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
        )
133
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   102
        read_only_fields = ('ext_id', 'version', 'created', 'updated', 'owner', 'group', 'protocol')
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
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 DetailSessionSerializer(serializers.ModelSerializer):
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    owner = serializers.SlugRelatedField(read_only=True, slug_field='username')
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
    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
   109
    group = serializers.SlugRelatedField(slug_field='name', read_only=True)
133
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   110
    protocol = ProtocolField(required=False)
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
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
   112
    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
   113
        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
   114
        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
   115
            '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
   116
            '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
   117
            '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
   118
        )
133
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   119
        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
   120
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
   121
class CreateSessionSerializer(serializers.ModelSerializer):
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
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
   123
    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
   124
        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
   125
    group = serializers.SlugRelatedField(slug_field='name', queryset=Group.objects.all(), required=False, allow_null=True)
6f3078f7fd47 Work on correct protocol propagation
ymh <ymh.work@gmail.com>
parents: 131
diff changeset
   126
    protocol = ProtocolField(required=False)
31
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
    class Meta:
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
        model = Session
63be3ce389f7 improve api
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
        fields = (
71
75dc1e794cf4 add date field on session object
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
   131
            '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
   132
            '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
   133
        )
6e18b31b0ad5 Correct session creation and add offline to the persisted state
ymh <ymh.work@gmail.com>
parents: 31
diff changeset
   134
        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
   135
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
   136
    def validate(self, data):
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
   137
        data = super().validate(data)
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
   138
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
   139
        group = data.get('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
   140
        owner = data.get('owner')
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
   141
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
   142
        if group is None:
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
   143
            if owner and owner.profile and owner.profile.default_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
   144
                group = owner.profile.default_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
   145
            if group is None and owner:
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
   146
                group = Group.objects.filter(profile__owner_personal=owner).first()
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
   147
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
   148
        if group is None:
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
   149
            raise serializers.ValidationError("group field is required or default group or personal group could not be found for owner")
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
   150
        elif not owner in group.user_set.all():
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
   151
            raise serializers.ValidationError("Owner must be in 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
   152
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
   153
        data['group'] = 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
   154
        return data