Fakultas Ilmu Komputer UI

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

6
7
from core.lib.permissions import IsAdminOrStudent, IsAdminOrSelfOrReadOnly, IsAdminOrCompany, IsAdminOrSupervisor
from core.models.accounts import Student, Company, Supervisor
8
9
from core.serializers.accounts import UserSerializer, StudentSerializer, CompanySerializer, SupervisorSerializer

10

11
12
13
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
14
    permission_classes = [IsAdminOrSelfOrReadOnly]
15
16
17
18
19
20
21
22
23
24
25

    @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
26
    permission_classes = [IsAdminOrStudent]
27

28
29
30
31

class CompanyViewSet(viewsets.ModelViewSet):
    queryset = Company.objects.all()
    serializer_class = CompanySerializer
32
    permission_classes = [IsAdminOrCompany]
33
34
35
36
37


class SupervisorViewSet(viewsets.ModelViewSet):
    queryset = Supervisor.objects.all()
    serializer_class = SupervisorSerializer
38
    permission_classes = [IsAdminOrSupervisor]
39
40
41
42
43
44