-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializers.py
76 lines (56 loc) · 1.97 KB
/
serializers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from django.contrib.auth.models import Group
from rest_framework import serializers
from .models import User, Organization
from .constants import USER_INFO_FIELDS, ORG_INFO_FIELDS, USER_CREATE_FIELDS
class GroupSerializer(serializers.ModelSerializer):
permissions = serializers.SlugRelatedField(
many=True,
read_only=True,
slug_field='codename'
)
class Meta:
model = Group
exclude = []
class OrganizationSerializer(serializers.ModelSerializer):
class Meta:
model = Organization
fields = ORG_INFO_FIELDS
class OrganizationRelatedField(serializers.ModelSerializer):
class Meta:
model = Organization
fields = ['id', 'name']
class UserOrgSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['id', 'name']
class UserDefaultSerializer(serializers.ModelSerializer):
organization = OrganizationRelatedField(read_only=True)
groups = serializers.SlugRelatedField(
many=True,
read_only=True,
slug_field='name'
)
class Meta:
model = User
exclude = []
class UserInfoSerializer(serializers.ModelSerializer):
organization = OrganizationRelatedField(read_only=True)
class Meta:
model = User
fields = USER_INFO_FIELDS
class UserCreateSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = USER_CREATE_FIELDS
extra_kwargs = {'password': {'write_only': True}}
def create(self, validated_data):
user = User(name=validated_data.get('name'),
phone=validated_data.get('phone'),
email=validated_data.get('email'),
birthdate=validated_data.get('birthdate'))
user.organization = validated_data.get('org')
user.set_password(validated_data.get('password'))
user.save()
groups = validated_data.get('groups', [])
user.groups.set(groups)
return user