Fakultas Ilmu Komputer UI

views.py 2.73 KB
Newer Older
1
2
from rest_framework import generics
from rest_framework.exceptions import ValidationError
3
from rest_framework_simplejwt.tokens import RefreshToken
4
from .models import UserAccount
5
from .serializers import LogoutSerializer, UserSerializer
6
7
8
9
10
11
12
from rest_framework.permissions import AllowAny, IsAuthenticated
from djangorestframework_camel_case.parser import (CamelCaseJSONParser,
                                                   CamelCaseMultiPartParser,
                                                   CamelCaseFormParser)
from djangorestframework_camel_case.render import (CamelCaseJSONRenderer,
                                                   CamelCaseBrowsableAPIRenderer)

13
14
15
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework_simplejwt.views import TokenObtainPairView

16
17
18
19
20
21
22
23
24
25
26
27
28
29
class UserCreate(generics.CreateAPIView):
    serializer_class = UserSerializer
    permission_classes = [AllowAny]
    parser_classes = (CamelCaseJSONParser, CamelCaseFormParser, CamelCaseMultiPartParser, )
    renderer_classes = (CamelCaseJSONRenderer, CamelCaseBrowsableAPIRenderer, )

    def perform_create(self, serializer):
        try:
            serializer = serializer.save()
            return serializer
        except Exception as err:
            error_message = {'error': err}
            raise ValidationError(error_message)

30
31
# Untuk get user
class Resource(generics.ListAPIView):
32
    serializer_class = UserSerializer
33
    permission_classes = [IsAuthenticated]
34
    parser_classes = (CamelCaseJSONParser, CamelCaseFormParser, CamelCaseMultiPartParser, )
35
36
    renderer_classes = (CamelCaseJSONRenderer, CamelCaseBrowsableAPIRenderer, )

37
38
39
40
    def get_queryset(self):
        user = UserAccount.objects.get(username = self.request.user)
        return [user]

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Antara pakai logout ini atau langsung di react
class Logout(generics.CreateAPIView):
    serializer_class = LogoutSerializer
    permission_classes = [IsAuthenticated]
    parser_classes = (CamelCaseJSONParser, CamelCaseFormParser, CamelCaseMultiPartParser, )
    renderer_classes = (CamelCaseJSONRenderer, CamelCaseBrowsableAPIRenderer, )

    def perform_create(self, request):
        try:
            refresh_token = request.data["refresh_token"]
            token = RefreshToken(refresh_token)
            token.blacklist()
        except Exception as err:
            error_message = {'error': err}
            raise ValidationError(error_message)
56
57
58
59
60
61
62
63
64
65
66
67
68
69

class MyTokenObtainPairSerializer(TokenObtainPairSerializer):

    @classmethod
    def get_token(cls, user):
        token = super().get_token(user)

        # Add custom claims
        token['username'] = user.username

        return token
    
class MyTokenObtainPairView(TokenObtainPairView):
    serializer_class = MyTokenObtainPairSerializer