Fakultas Ilmu Komputer UI

vacancies.py 2.44 KB
Newer Older
1
from rest_framework import serializers
2

Zamil Majdy's avatar
Zamil Majdy committed
3
from core.models import Company
4
from core.models.vacancies import Vacancy, Application
5
from core.serializers.accounts import StudentSerializer, CompanySerializer
6
7
8


class VacancySerializer(serializers.ModelSerializer):
9
    company = CompanySerializer()
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    status = serializers.SerializerMethodField('_get_app_status')
    bookmarked = serializers.SerializerMethodField('_get_bookmarked_status')

    def _get_app_status(self, instance):
        try:
            request = self.context.get("request")
            student = request.user.student
            app = Application.objects.get(vacancy=instance, student=student)
            return app.status
        except:
            return None
        return None

    def _get_bookmarked_status(self, instance):
        try:
            request = self.context.get("request")
            if request.user.student.bookmarked_vacancies.filter(pk=instance.id).exists():
                return True
            return False
        except:
            return None
        return None
32
33
34

    class Meta:
        model = Vacancy
35
        fields = ['company', 'verified', 'open_time', 'description', 'close_time', 'created', 'updated', 'name', \
36
                  'status', 'bookmarked', 'id']
37
38
39


class PostVacancySerializer(serializers.ModelSerializer):
Zamil Majdy's avatar
Zamil Majdy committed
40
    company = serializers.PrimaryKeyRelatedField(queryset=Company.objects.all())
41

42
43
44
45
46
    class Meta:
        model = Vacancy
        fields = '__all__'


47
48
49
50
51
class ApplicationSerializer(serializers.ModelSerializer):
    vacancy = VacancySerializer()
    student = StudentSerializer()

    class Meta:
52
        model = Application
53
54
55
        fields = '__all__'


56
class ApplicationStatusSerializer(serializers.ModelSerializer):
57

58
59
60
    class Meta:
        model = Application
        fields = ['status']
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78


class SupervisorStudentApplicationSerializer(serializers.ModelSerializer):
    def to_representation(self, instance):
        status_map = ["new", "read", "bookmarked", "rejected", "accepted" ]
        return {
            'name' : instance.student.full_name,
            'npm' : instance.student.npm,
            'vacancy_name' : instance.vacancy.name,
            'company_name' : instance.vacancy.company.name,
            'status' : status_map[instance.status]
        }

    class Meta:
        model = Application
        fields = ['name', 'npm', 'vacancy_name', 'company_name', 'status']
        read_only_fields = ['name', 'npm', 'vacancy_name', 'company_name', 'status']