Fakultas Ilmu Komputer UI

accounts.py 2.78 KB
Newer Older
1
2
3
4
5
6
from django.contrib.auth.models import User
from rest_framework import serializers

from core.models.accounts import Supervisor, Company, Student


7
class BasicUserSerializer(serializers.HyperlinkedModelSerializer):
8
9
10
11
12
13
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff')


class StudentSerializer(serializers.ModelSerializer):
14
    user = BasicUserSerializer()
Zamil Majdy's avatar
Zamil Majdy committed
15
16
    name = serializers.ReadOnlyField()

17
18
    class Meta:
        model = Student
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
        fields = ['id', 'name', 'user', 'npm', 'resume', 'phone_number', 'birth_place', 'birth_date', 'major', 'batch', \
                  'show_transcript', 'photo']


class StudentUpdateSerializer(serializers.ModelSerializer):
    email = serializers.EmailField()

    def to_representation(self, instance):
        resume = None
        photo = None
        if instance.resume and hasattr(instance.resume, 'url'):
            resume = instance.resume.url
        if instance.photo and hasattr(instance.photo, 'url'):
            photo = instance.photo.url
        return {
            'resume' : resume,
            'email' : instance.user.email,
            'phone_number' : instance.phone_number,
            'photo' : photo,
            'show_transcript' : instance.show_transcript
        }

    def update(self, instance, validated_data):
        instance.resume = validated_data.get('resume', instance.resume)
        instance.show_transcript = validated_data.get('show_transcript', instance.show_transcript)
        instance.phone_number = validated_data.get('phone_number', instance.phone_number)
        instance.photo = validated_data.get('photo', instance.photo)
        instance.user.email = validated_data.get('email', instance.user.email)
        instance.save()
        instance.user.save()
        return instance

    class Meta:
        model = Student
        fields = ['resume', 'email', 'phone_number', 'photo', 'show_transcript']
54
55
56


class CompanySerializer(serializers.ModelSerializer):
57
    user = BasicUserSerializer()
Zamil Majdy's avatar
Zamil Majdy committed
58
59
    name = serializers.ReadOnlyField()

60
61
62
63
64
65
    class Meta:
        model = Company
        fields = '__all__'


class SupervisorSerializer(serializers.ModelSerializer):
66
    user = BasicUserSerializer()
Zamil Majdy's avatar
Zamil Majdy committed
67
68
    name = serializers.ReadOnlyField()

69
70
    class Meta:
        model = Supervisor
Zamil Majdy's avatar
Zamil Majdy committed
71
        fields = '__all__'
72
73


74
class UserSerializer(serializers.HyperlinkedModelSerializer):
75
76
77
78
79
80
81
82
    company = CompanySerializer()
    supervisor = SupervisorSerializer()
    student = StudentSerializer()

    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff', 'company', 'supervisor', 'student')

83

84
85
86
87
88
89
90
class RegisterSerializer(serializers.HyperlinkedModelSerializer):
    company = CompanySerializer()

    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff', 'company')