Fakultas Ilmu Komputer UI

vacancies.py 2.99 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
from core.serializers.vacancies import VacancySerializer
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
33
34
35
36
37
38
39
40
41
42
43
    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):
        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.applied_vacancies.add(vacancy)
        return Response(self.serializer_class(student.applied_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.applied_vacancies.remove(vacancy)
        return Response(self.serializer_class(student.applied_vacancies, many=True, context={'request': request}).data)

44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)