Fakultas Ilmu Komputer UI

accounts.py 1.62 KB
Newer Older
1
from django.contrib.auth.models import User
2
from rest_framework import status
3
from rest_framework import viewsets
4
from rest_framework.decorators import list_route, detail_route
5
6
from rest_framework.response import Response

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

11

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

27
28
29
30
31
32
33
34
35
36
37
38
    @detail_route(methods=['get'], url_path='vacancies')
    def vacancies(self, request, pk):
        student = Student.objects.get(pk=pk)
        return Response(get_display_name(student))

    @detail_route(methods=['post'], url_path='vacancies')
    def insert_vacancies(self, request, pk):
        vacancy = Vacancy.objects.get(request.data.id)
        student = self.request.user.student
        student.bookmarked_vacancies.add(vacancy)
        return Response(vacancy, status=status.HTTP_200_OK)

39
40
41
42
43
44
45
46
47

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


class SupervisorViewSet(viewsets.ModelViewSet):
    queryset = Supervisor.objects.all()
    serializer_class = SupervisorSerializer