Fakultas Ilmu Komputer UI

vacancies.py 4.75 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()
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
    def list(self, request, *args, **kwargs):
        vacancies = Vacancy.objects.all()
25
26
        verified = request.query_params['verified'] if 'verified' in request.query_params else "True"
        if verified.lower() in ("yes", "true", "t", "1"):
27
            vacancies = vacancies.filter(verified=True)
28

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

31

32
class ApplicationViewSet(viewsets.GenericViewSet):
33
    serializer_class = ApplicationSerializer
34
    permission_classes = [IsAdminOrStudent]
35

36
    def list(self, request, student_id):
37
38
39
40
        """
        Get list of a student {student_id}'s application
        ---
        """
41
        student = get_object_or_404(Student.objects.all(), pk=student_id)
42
43
        vacancies = student.applied_vacancies
        return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)
44
45

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

    def destroy(self, request, student_id, pk):
66
67
68
69
        """
        Remove a application {id} for student {student_id}
        ---
        """
70
71
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
        student = get_object_or_404(Student.objects.all(), pk=student_id)
72
        application = get_object_or_404(Application.objects.all(), student=student, vacancy=vacancy)
73
        application.delete()
74
        return Response(ApplicationSerializer(application, context={'request': request}).data)
75

76

77
78
79
80
81
class BookmarkedVacancyByStudentViewSet(viewsets.GenericViewSet):
    serializer_class = VacancySerializer
    permission_classes = [IsAdminOrStudent]

    def list(self, request, student_id):
82
83
84
85
        """
        Get list of a student {student_id}'s bookmarked vacancies
        ---
        """
86
87
88
89
90
        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):
91
92
93
94
95
96
97
98
99
100
        """
        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
        """
101
102
103
104
105
106
        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):
107
108
109
110
        """
        Remove bookmark {id} for student {student_id}
        ---
        """
111
112
113
114
        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)