Fakultas Ilmu Komputer UI

vacancies.py 6.92 KB
Newer Older
1
from rest_framework import viewsets, status
2
from rest_framework.exceptions import ValidationError
3
from rest_framework.generics import get_object_or_404
4
from rest_framework.permissions import IsAuthenticated
5
from rest_framework.response import Response
6
from rest_framework.decorators import detail_route
7

8
from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany, IsAdminOrVacancyOwner
9
from core.models import Student, Company
10
from core.models.vacancies import Vacancy, Application
11
from core.serializers.vacancies import VacancySerializer, ApplicationSerializer, ApplicationStatusSerializer
12
13
14


class VacancyViewSet(viewsets.ModelViewSet):
15
    queryset = Vacancy.objects.all()
16
    serializer_class = VacancySerializer
17
    permission_classes = [IsAdminOrCompany]
18

19
    def get_permissions(self):
20
        if self.action in ["retrieve", "list"]:
21
22
23
            return [IsAuthenticated()]
        return super(VacancyViewSet, self).get_permissions()

24
25
    def list(self, request, *args, **kwargs):
        vacancies = Vacancy.objects.all()
26
27
        verified = request.query_params['verified'] if 'verified' in request.query_params else "True"
        if verified.lower() in ("yes", "true", "t", "1"):
28
            vacancies = vacancies.filter(verified=True)
29

30
31
        return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)

32
33
34
35
36
37
38
    @detail_route(permission_classes=[IsAdminOrCompany])
    def count(self, request, pk=None):
        vacancy = self.get_object()
        count = Application.objects.filter(vacancy=vacancy).count()
        count_new = Application.objects.filter(vacancy=vacancy, status=Application.NEW).count()
        return Response({"count": count, "count_new": count_new}, status=status.HTTP_200_OK)

39

40
class ApplicationViewSet(viewsets.GenericViewSet):
41
    serializer_class = ApplicationSerializer
42
    permission_classes = [IsAdminOrStudent]
43

44
    def list(self, request, student_id):
45
46
47
48
        """
        Get list of a student {student_id}'s application
        ---
        """
49
        student = get_object_or_404(Student.objects.all(), pk=student_id)
50
51
        vacancies = student.applied_vacancies
        return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)
52
53

    def create(self, request, student_id):
54
55
56
57
58
        """
        Create a new application for student {student_id}
        ---
        parameters:
            - name: body
59
              description: JSON object containing an integer 'vacancy_id' and a string 'cover_letter'
60
61
62
63
              required: true
              type: string
              paramType: body
        """
64
65
        cover_letter = request.data.get('cover_letter')
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data.get('vacancy_id'))
66
        student = get_object_or_404(Student.objects.all(), pk=student_id)
67
68
        if Application.objects.filter(vacancy=vacancy, student=student).exists():
            raise ValidationError("You have already applied for the vacancy")
69
70
71
        application = Application(vacancy=vacancy, student=student, cover_letter=cover_letter)
        application.save()
        return Response(ApplicationSerializer(application, context={'request': request}).data)
72
73

    def destroy(self, request, student_id, pk):
74
75
76
77
        """
        Remove a application {id} for student {student_id}
        ---
        """
78
79
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
        student = get_object_or_404(Student.objects.all(), pk=student_id)
80
        application = get_object_or_404(Application.objects.all(), student=student, vacancy=vacancy)
81
        application.delete()
82
        return Response(ApplicationSerializer(application, context={'request': request}).data)
83

84

85
86
class CompanyApplicationViewSet(viewsets.GenericViewSet):
    queryset = Application.objects.all()
87
    permission_classes = [IsAdminOrCompany]
88
89
90
91
92
93
94
95
96
97
98
99

    def list(self, request, company_id):
        """
        Get list of company {company_id}'s applications
        ---
        """
        company = get_object_or_404(Company.objects.all(), pk=company_id)
        vacancies = Vacancy.objects.filter(company = company)
        applications = Application.objects.filter(vacancy__in = vacancies)
        return Response(ApplicationSerializer(applications, many=True, context={'request': request}).data)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class CompanyApplicationStatusViewSet(viewsets.GenericViewSet):
    queryset = Application.objects.all()
    serializer_class = ApplicationStatusSerializer
    permission_classes = [IsAdminOrVacancyOwner]

    def partial_update(self, request, pk=None):
        application = self.get_object()
        serializer = self.get_serializer_class()(application, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


115
class CompanyVacanciesViewSet(viewsets.GenericViewSet):
116
    queryset = Vacancy.objects.all()
117
118
119
120
121
122

    def list(self, request, company_id):
        """
        Get list of company {company_id}'s vacancies
        ---
        """
123
124
        company = get_object_or_404(Company.objects.all().order_by('-updated'), pk=company_id)
        vacancies = Vacancy.objects.filter(company=company)
125
126
127
        return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)


128
129
130
131
132
class BookmarkedVacancyByStudentViewSet(viewsets.GenericViewSet):
    serializer_class = VacancySerializer
    permission_classes = [IsAdminOrStudent]

    def list(self, request, student_id):
133
134
135
136
        """
        Get list of a student {student_id}'s bookmarked vacancies
        ---
        """
137
138
139
140
141
        student = get_object_or_404(Student.objects.all(), pk=student_id)
        vacancies = self.serializer_class(student.bookmarked_vacancies, many=True, context={'request': request})
        return Response(vacancies.data)

    def create(self, request, student_id):
142
143
144
145
146
147
148
149
150
151
        """
        Bookmarks a vacancy for student {student_id}
        ---
        parameters:
            - name: body
              description: JSON object containing only one string: vacancy_id
              required: true
              type: string
              paramType: body
        """
152
153
154
155
156
157
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data['vacancy_id'])
        student = get_object_or_404(Student.objects.all(), pk=student_id)
        student.bookmarked_vacancies.add(vacancy)
        return Response(self.serializer_class(student.bookmarked_vacancies, many=True, context={'request': request}).data)

    def destroy(self, request, student_id, pk):
158
159
160
161
        """
        Remove bookmark {id} for student {student_id}
        ---
        """
162
163
164
165
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
        student = get_object_or_404(Student.objects.all(), pk=student_id)
        student.bookmarked_vacancies.remove(vacancy)
        return Response(self.serializer_class(student.bookmarked_vacancies, many=True, context={'request': request}).data)