Fakultas Ilmu Komputer UI

vacancies.py 4.38 KB
Newer Older
1
from rest_framework import viewsets
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

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


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

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

23

24
class ApplicationViewSet(viewsets.GenericViewSet):
25
    serializer_class = ApplicationSerializer
26
    permission_classes = [IsAdminOrStudent]
27

28
    def list(self, request, student_id):
29
30
31
32
        """
        Get list of a student {student_id}'s application
        ---
        """
33
        student = get_object_or_404(Student.objects.all(), pk=student_id)
34
35
        applications = Application.objects.filter(student=student)
        return Response(ApplicationSerializer(applications, many=True, context={'request': request}).data)
36
37

    def create(self, request, student_id):
38
39
40
41
42
43
44
45
46
47
        """
        Create a new application for student {student_id}
        ---
        parameters:
            - name: body
              description: JSON object containing only one string: vacancy_id
              required: true
              type: string
              paramType: body
        """
48
49
        cover_letter = request.data.get('cover_letter')
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data.get('vacancy_id'))
50
        student = get_object_or_404(Student.objects.all(), pk=student_id)
51
52
        if Application.objects.filter(vacancy=vacancy, student=student).exists():
            raise ValidationError("You have already applied for the vacancy")
53
54
55
        application = Application(vacancy=vacancy, student=student, cover_letter=cover_letter)
        application.save()
        return Response(ApplicationSerializer(application, context={'request': request}).data)
56
57

    def destroy(self, request, student_id, pk):
58
59
60
61
        """
        Remove a application {id} for student {student_id}
        ---
        """
62
63
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
        student = get_object_or_404(Student.objects.all(), pk=student_id)
64
        application = get_object_or_404(Application.objects.all(), student=student, vacancy=vacancy)
65
        application.delete()
66
        return Response(ApplicationSerializer(application, context={'request': request}).data)
67

68

69
70
71
72
73
class BookmarkedVacancyByStudentViewSet(viewsets.GenericViewSet):
    serializer_class = VacancySerializer
    permission_classes = [IsAdminOrStudent]

    def list(self, request, student_id):
74
75
76
77
        """
        Get list of a student {student_id}'s bookmarked vacancies
        ---
        """
78
79
80
81
82
        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):
83
84
85
86
87
88
89
90
91
92
        """
        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
        """
93
94
95
96
97
98
        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):
99
100
101
102
        """
        Remove bookmark {id} for student {student_id}
        ---
        """
103
104
105
106
        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)