Fakultas Ilmu Komputer UI

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

from core.models.accounts import Supervisor, Company, Student
5
from core.models.vacancies import Application
6

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
    name = serializers.ReadOnlyField()
16
    accepted_no = serializers.SerializerMethodField()
Zamil Majdy's avatar
Zamil Majdy committed
17

18
19
    class Meta:
        model = Student
20
        fields = ['id', 'name', 'user', 'npm', 'resume', 'phone_number', 'birth_place', 'birth_date', 'major', 'batch', \
21
22
23
24
25
26
                  'show_transcript', 'photo', 'accepted_no']

    def get_accepted_no(self, obj):
        apps = Application.objects.filter(student=obj, status=4)
        companies = apps.values('vacancy__company').distinct()
        return companies.count()
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


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']
60
61
62


class CompanySerializer(serializers.ModelSerializer):
63
    user = BasicUserSerializer()
Zamil Majdy's avatar
Zamil Majdy committed
64
65
    name = serializers.ReadOnlyField()

66
67
68
69
70
71
    class Meta:
        model = Company
        fields = '__all__'


class SupervisorSerializer(serializers.ModelSerializer):
72
    user = BasicUserSerializer()
Zamil Majdy's avatar
Zamil Majdy committed
73
74
    name = serializers.ReadOnlyField()

75
76
    class Meta:
        model = Supervisor
Zamil Majdy's avatar
Zamil Majdy committed
77
        fields = '__all__'
78
79


80
class UserSerializer(serializers.HyperlinkedModelSerializer):
81
82
83
84
85
86
87
88
    company = CompanySerializer()
    supervisor = SupervisorSerializer()
    student = StudentSerializer()

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

89

90
91
92
93
94
95
96
class RegisterSerializer(serializers.HyperlinkedModelSerializer):
    company = CompanySerializer()

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