Fakultas Ilmu Komputer UI

vacancies.py 3.16 KB
Newer Older
1
from rest_framework import viewsets
2
from rest_framework.generics import get_object_or_404
3
from rest_framework.permissions import IsAuthenticated
4
from rest_framework.response import Response
5

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


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

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

22

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

27
28
29
30
31
32
    def list(self, request, student_id):
        student = get_object_or_404(Student.objects.all(), pk=student_id)
        vacancies = self.serializer_class(student.applied_vacancies, many=True, context={'request': request})
        return Response(vacancies.data)

    def create(self, request, student_id):
33
34
        cover_letter = request.data.get('cover_letter')
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data.get('vacancy_id'))
35
        student = get_object_or_404(Student.objects.all(), pk=student_id)
36
37
38
        application = Application(vacancy=vacancy, student=student, cover_letter=cover_letter)
        application.save()
        return Response(ApplicationSerializer(application, context={'request': request}).data)
39
40
41
42

    def destroy(self, request, student_id, pk):
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
        student = get_object_or_404(Student.objects.all(), pk=student_id)
43
44
        application = get_object_or_404(Application.objects.all(), student=student, vacancy=vacancy)
        return Response(ApplicationSerializer(application, context={'request': request}).data)
45

46

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class BookmarkedVacancyByStudentViewSet(viewsets.GenericViewSet):
    serializer_class = VacancySerializer
    permission_classes = [IsAdminOrStudent]

    def list(self, request, student_id):
        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):
        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):
        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)