Fakultas Ilmu Komputer UI

accounts.py 10.6 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

Zamil Majdy's avatar
Zamil Majdy committed
7

8
class BasicUserSerializer(serializers.HyperlinkedModelSerializer):
9
10
    class Meta:
        model = User
Zulia Putri's avatar
bug fix    
Zulia Putri committed
11
        fields = ('url', 'username', 'email', 'is_staff', 'last_login')
12
13
14


class StudentSerializer(serializers.ModelSerializer):
15
    user = BasicUserSerializer()
Zamil Majdy's avatar
Zamil Majdy committed
16
    name = serializers.ReadOnlyField()
17
    accepted_no = serializers.SerializerMethodField()
18
    read_no = serializers.SerializerMethodField()
Zamil Majdy's avatar
Zamil Majdy committed
19

20
21
    class Meta:
        model = Student
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
22
23
24
25
26
27
28
        fields = ['id', 'name', 'full_name', 'user', 'npm', 'resume', 'phone_number', 'gender', 'birth_place',
                  'birth_date', 'major', 'batch',
                  'show_transcript', 'photo', 'accepted_no', 'linkedin_url', 'hackerrank_url', 'region', 'intro',
                  'portfolio_link', 'website_url', 'student_gpa', 'age',
                  'work_experience', 'latest_work', 'latest_work_desc', 'read_no', 'volunteer', 'job_seeking_status',
                  'skills', 'expected_salary',
                  'self_description', 'github_url', 'gitlab_url', 'awards', 'certification', 'languages', 'seminar',
Dwi Nanda Susanto's avatar
Dwi Nanda Susanto committed
29
                  'interests', 'alamat', 'projects', 'dependants', 'student_toefl', 'student_toefl_file', 'related_course', 'ielts']
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
30
31
32

    @staticmethod
    def get_accepted_no(obj):
33
34
        apps = Application.objects.filter(student=obj, status=4)
        companies = apps.values('vacancy__company').distinct()
35
36
        return companies.count()

Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
37
38
    @staticmethod
    def get_read_no(obj):
39
40
        apps = Application.objects.filter(student=obj, status=1)
        companies = apps.values('vacancy__company').distinct()
41
        return companies.count()
42
43
44
45
46
47
48
49


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

    def to_representation(self, instance):
        resume = None
        photo = None
Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
50
        student_toefl_file = None
51
52
53
54
        if instance.resume and hasattr(instance.resume, 'url'):
            resume = instance.resume.url
        if instance.photo and hasattr(instance.photo, 'url'):
            photo = instance.photo.url
Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
55
56
        if instance.student_toefl_file and hasattr(instance.student_toefl_file, 'url'):
            student_toefl_file = instance.student_toefl_file.url
57
        return {
Zamil Majdy's avatar
Zamil Majdy committed
58
59
60
            'resume': resume,
            'email': instance.user.email,
            'phone_number': instance.phone_number,
Zulia Putri's avatar
bug fix    
Zulia Putri committed
61
            'gender': instance.gender,
PRIAMBUDI LINTANG BAGASKARA's avatar
PRIAMBUDI LINTANG BAGASKARA committed
62
            'region': instance.region,
Zamil Majdy's avatar
Zamil Majdy committed
63
            'photo': photo,
64
            'show_transcript': instance.show_transcript,
Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
65
            'portfolio_link': instance.portfolio_link,
66
            'self_description': instance.self_description,
HANIF AGUNG PRAYOGA's avatar
HANIF AGUNG PRAYOGA committed
67
            'intro': instance.intro,
68
            'expected_salary': instance.expected_salary,
Zulia Putri's avatar
bug fix    
Zulia Putri committed
69
70
71
            'job_seeking_status': instance.job_seeking_status,
            'website_url': instance.website_url,
            'student_gpa': instance.student_gpa,
72
            'student_toefl':instance.student_toefl,
Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
73
            'student_toefl_file': student_toefl_file,
74
            'volunteer': instance.volunteer,
75
            'linkedin_url': instance.linkedin_url,
76
            'hackerrank_url': instance.hackerrank_url,
77
            'work_experience': instance.work_experience,
78
79
            'latest_work': instance.latest_work,
            'latest_work_desc': instance.latest_work_desc,
80
            'alamat': instance.alamat,
Tyagita Larasati's avatar
Tyagita Larasati committed
81
            'skills': instance.skills,
Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
82
            'github_url': instance.github_url,
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
83
84
85
86
87
88
89
90
            'gitlab_url': instance.gitlab_url,
            'awards': instance.awards,
            'projects': instance.projects,
            'certification': instance.certification,
            'languages': instance.languages,
            'seminar': instance.seminar,
            'interests': instance.interests,
            'dependants': instance.dependants,
Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
91
            'related_course':instance.related_course,
Dwi Nanda Susanto's avatar
Dwi Nanda Susanto committed
92
            'ielts':instance.ielts,
93
94
95
96
        }

    def update(self, instance, validated_data):
        instance.resume = validated_data.get('resume', instance.resume)
Zulia Putri's avatar
bug fix    
Zulia Putri committed
97
98
99
100
        instance.show_transcript = validated_data.get(
            'show_transcript', instance.show_transcript)
        instance.phone_number = validated_data.get(
            'phone_number', instance.phone_number)
Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
101
        instance.gender = validated_data.get('gender', instance.gender)
PRIAMBUDI LINTANG BAGASKARA's avatar
PRIAMBUDI LINTANG BAGASKARA committed
102
        instance.region = validated_data.get('region', instance.region)
103
104
        instance.photo = validated_data.get('photo', instance.photo)
        instance.user.email = validated_data.get('email', instance.user.email)
Zulia Putri's avatar
bug fix    
Zulia Putri committed
105
106
107
108
        instance.portfolio_link = validated_data.get(
            'portfolio_link', instance.portfolio_link)
        instance.self_description = validated_data.get(
            'self_description', instance.self_description)
HANIF AGUNG PRAYOGA's avatar
HANIF AGUNG PRAYOGA committed
109
        instance.intro = validated_data.get('intro', instance.intro)
110
        instance.volunteer = validated_data.get('volunteer', instance.volunteer)
111
        instance.linkedin_url = validated_data.get('linkedin_url', instance.linkedin_url)
Muhammad Fauzan Fikri's avatar
Muhammad Fauzan Fikri committed
112
        instance.website_url = validated_data.get('website_url', instance.website_url)
113
114
        instance.latest_work = validated_data.get('latest_work', instance.latest_work)
        instance.latest_work_desc = validated_data.get('latest_work_desc', instance.latest_work_desc)
115
        instance.alamat = validated_data.get('alamat', instance.alamat)
Zulia Putri's avatar
bug fix    
Zulia Putri committed
116
117
118
119
120
121
122
123
124
125
126
127
        instance.expected_salary = validated_data.get(
            'expected_salary', instance.expected_salary)
        instance.volunteer = validated_data.get(
            'volunteer', instance.volunteer)
        instance.linkedin_url = validated_data.get(
            'linkedin_url', instance.linkedin_url)
        instance.hackerrank_url = validated_data.get(
            'hackerrank_url', instance.hackerrank_url)
        instance.job_seeking_status = validated_data.get(
            'job_seeking_status', instance.job_seeking_status)
        instance.student_gpa = validated_data.get(
            'student_gpa', instance.student_gpa)
128
        instance.student_toefl = validated_data.get('student_toefl', instance.student_toefl)
Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
129
        instance.student_toefl_file = validated_data.get('student_toefl_file', instance.student_toefl_file)
Zulia Putri's avatar
bug fix    
Zulia Putri committed
130
131
        instance.website_url = validated_data.get(
            'website_url', instance.website_url)
Tyagita Larasati's avatar
Tyagita Larasati committed
132
        instance.skills = validated_data.get('skills', instance.skills)
Zulia Putri's avatar
bug fix    
Zulia Putri committed
133
134
135
136
137
138
139
140
        instance.work_experience = validated_data.get(
            'work_experience', instance.work_experience)
        instance.latest_work = validated_data.get(
            'latest_work', instance.latest_work)
        instance.latest_work_desc = validated_data.get(
            'latest_work_desc', instance.latest_work_desc)
        instance.github_url = validated_data.get(
            'github_url', instance.github_url)
Khoirul Khuluqi Abdulloh's avatar
Khoirul Khuluqi Abdulloh committed
141
142
        instance.gitlab_url = validated_data.get(
            'gitlab_url', instance.gitlab_url)
Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
143
144
        instance.awards = validated_data.get(
            'awards', instance.awards)
Aviliani Pramestya's avatar
Aviliani Pramestya committed
145
146
        instance.projects = validated_data.get(
            'projects', instance.projects)
Fannyah Dita Cahya's avatar
Fannyah Dita Cahya committed
147
148
        instance.certification = validated_data.get(
            'certification', instance.certification)
Tyagita Larasati's avatar
Tyagita Larasati committed
149
150
        instance.languages = validated_data.get(
            'languages', instance.languages)
Nixi Sendya Putri's avatar
Nixi Sendya Putri committed
151
152
        instance.seminar = validated_data.get(
            'seminar', instance.seminar)
Hema Mitta Kalyani's avatar
Hema Mitta Kalyani committed
153
154
        instance.interests = validated_data.get(
            'interests', instance.interests)
Dwi Nanda Susanto's avatar
Dwi Nanda Susanto committed
155
        instance.dependants = validated_data.get('dependants', instance.dependants)
Farah Alhaniy Efendi's avatar
Farah Alhaniy Efendi committed
156
157
        instance.related_course = validated_data.get(
            'related_course', instance.related_course)
Dwi Nanda Susanto's avatar
Dwi Nanda Susanto committed
158
        instance.ielts = validated_data.get('ielts', instance.ielts)
159
160
161
162
163
164
        instance.save()
        instance.user.save()
        return instance

    class Meta:
        model = Student
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
165
166
167
168
169
        fields = ['resume', 'email', 'phone_number', 'gender', 'photo', 'show_transcript', 'linkedin_url',
                  'hackerrank_url', 'region', 'intro', 'portfolio_link', 'website_url', 'student_gpa',
                  'work_experience', 'latest_work', 'latest_work_desc', 'volunteer', 'job_seeking_status', 'skills',
                  'expected_salary',
                  'self_description', 'github_url', 'gitlab_url', 'awards', 'certification', 'languages', 'seminar',
Dwi Nanda Susanto's avatar
Dwi Nanda Susanto committed
170
                  'interests', 'alamat', 'projects', 'dependants', 'student_toefl', 'student_toefl_file', 'related_course', 'ielts']
171
172


Zamil Majdy's avatar
Zamil Majdy committed
173
174
175
176
177
class CompanyUpdateSerializer(serializers.ModelSerializer):
    def to_representation(self, instance):
        return {
            'address': instance.address,
            'description': instance.description,
Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
178
            'founded': instance.founded,
179
            'name': instance.user.first_name,
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
180
181
182
            'website': instance.website,
            'linkedin_url': instance.linkedin_url,
            'size': instance.size,
Zamil Majdy's avatar
Zamil Majdy committed
183
184
185
186
        }

    def update(self, instance, validated_data):
        instance.address = validated_data.get('address', instance.address)
187
        instance.website = validated_data.get('website', instance.website)
Hadlina Rahmadinni's avatar
Hadlina Rahmadinni committed
188
        instance.size = validated_data.get('size', instance.size)
Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
189
190
        instance.description = validated_data.get('description', instance.description)
        instance.founded = validated_data.get('founded', instance.founded)
Zulia Putri's avatar
bug fix    
Zulia Putri committed
191
192
        instance.user.first_name = validated_data.get(
            'name', instance.user.first_name)
193
        instance.linkedin_url = validated_data.get('linkedin_url', instance.linkedin_url)
Zamil Majdy's avatar
Zamil Majdy committed
194
195
196
197
198
199
        instance.save()
        instance.user.save()
        return instance

    class Meta:
        model = Company
Metta Anantha Pindika's avatar
Metta Anantha Pindika committed
200
        fields = ['address', 'description', 'founded', 'name', 'website', 'linkedin_url', 'size']
Zamil Majdy's avatar
Zamil Majdy committed
201
202


203
class CompanySerializer(serializers.ModelSerializer):
204
    user = BasicUserSerializer()
Zamil Majdy's avatar
Zamil Majdy committed
205
206
    name = serializers.ReadOnlyField()

207
208
209
210
211
212
    class Meta:
        model = Company
        fields = '__all__'


class SupervisorSerializer(serializers.ModelSerializer):
213
    user = BasicUserSerializer()
Zamil Majdy's avatar
Zamil Majdy committed
214
215
    name = serializers.ReadOnlyField()

216
217
    class Meta:
        model = Supervisor
Zamil Majdy's avatar
Zamil Majdy committed
218
        fields = '__all__'
219
220


221
class UserSerializer(serializers.HyperlinkedModelSerializer):
222
223
224
225
226
227
    company = CompanySerializer()
    supervisor = SupervisorSerializer()
    student = StudentSerializer()

    class Meta:
        model = User
Zulia Putri's avatar
bug fix    
Zulia Putri committed
228
        fields = ('url', 'username', 'email', 'is_staff',
229
                  'company', 'supervisor', 'student', 'last_login')
230

231

232
233
234
235
236
237
class RegisterSerializer(serializers.HyperlinkedModelSerializer):
    company = CompanySerializer()

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