Fakultas Ilmu Komputer UI

accounts.py 2.15 KB
Newer Older
1
from django.contrib.auth.models import User
2
from django.shortcuts import get_object_or_404
3
from rest_framework import status
4
from rest_framework import viewsets
5
from rest_framework.decorators import list_route, detail_route
6
from rest_framework.exceptions import ValidationError
7
8
from rest_framework.response import Response

9
10
from core.models import Vacancy
from core.models.accounts import Student, Company, Supervisor, get_display_name
11
12
from core.serializers.accounts import UserSerializer, StudentSerializer, CompanySerializer, SupervisorSerializer

13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    @list_route(methods=['get'])
    def me(self, request):
        user = self.request.user
        serializer = UserSerializer(user, context={"request": request})
        return Response(serializer.data)


class StudentViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

29
30
31
32
33
34
    @detail_route(methods=['post'], url_path='bookmarked-vacancies')
    def bookmark_vacancies(self, request, pk):
        user = self.request.user
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data['vacancy_id'])
        student = get_object_or_404(Student.objects.all(), pk=pk)
        if student != user.student and not user.is_staff:
35
36
            raise ValidationError('You must be a student'
                                  )
37
38
        student.bookmarked_vacancies.add(vacancy)
        return Response(vacancy, status=status.HTTP_200_OK)
39

40
41
42
    @detail_route(methods=['delete'], url_path='bookmarked-vacancies')
    def unbookmark_vacancies(self, request, pk):
        vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data['vacancy_id'])
43
        student = self.request.user.student
44
        student.bookmarked_vacancies.remove(vacancy)
45
46
        return Response(vacancy, status=status.HTTP_200_OK)

47
48
49
50
51
52
53
54
55

class CompanyViewSet(viewsets.ModelViewSet):
    queryset = Company.objects.all()
    serializer_class = CompanySerializer


class SupervisorViewSet(viewsets.ModelViewSet):
    queryset = Supervisor.objects.all()
    serializer_class = SupervisorSerializer
56
57
58
59
60
61