/
serializers.py
62 lines (52 loc) · 1.59 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
from rest_framework import serializers
from .models import User
class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
"""Public user account view."""
class Meta:
model = User
fields = (
'url',
'username',
'is_active',
'date_joined',
'last_login',
'schedule_entries'
)
extra_kwargs = {
'url': {
'view_name': 'user-detail'
},
'is_active': {
'initial': True
},
'schedule_entries': {
'view_name': 'schedule-detail'
},
}
read_only_fields = (
'schedule_entries',
'date_joined',
'last_login'
)
class UserDetailsSerializer(UserProfileSerializer):
"""Private user account view."""
auth_token = serializers.SerializerMethodField()
has_usable_password = serializers.SerializerMethodField()
is_admin = serializers.SerializerMethodField()
def get_is_admin(self, obj):
return obj.is_staff
class Meta(UserProfileSerializer.Meta):
fields = UserProfileSerializer.Meta.fields + (
'email',
'server_url',
'auth_token',
'has_usable_password',
'is_admin'
)
read_only_fields = UserProfileSerializer.Meta.read_only_fields + (
'auth_token',
)
def get_auth_token(self, obj):
return obj.auth_token.key
def get_has_usable_password(self, obj):
return obj.has_usable_password()