Fakultas Ilmu Komputer UI

vacancies.py 1.87 KB
Newer Older
1
from rest_framework import viewsets
2
3
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
4

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


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


class ApplicationViewSet(viewsets.ModelViewSet):
    queryset = Application.objects.all()
    serializer_class = ApplicationSerializer
20
    permission_classes = [IsAdminOrStudent]
21
22


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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)