Fakultas Ilmu Komputer UI

vacancies.py 4.68 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
25
26
27
28
    def list(self, request, *args, **kwargs):
        vacancies = Vacancy.objects.all()
        if not ('verified' in request.query_params) or request.query_params['verified']:
            vacancies = vacancies.filter(verified=True)
        return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)

29

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

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

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

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

74

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

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