Fakultas Ilmu Komputer UI

views.py 2.9 KB
Newer Older
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
1
2
from rest_framework import viewsets, status
from rest_framework.response import Response
3
4
from rest_framework.views import APIView
from rest_framework_simplejwt.tokens import RefreshToken
Kefas Satrio Bangkit Solidedantyo's avatar
Kefas Satrio Bangkit Solidedantyo committed
5
from dj_rest_auth.views import LoginView
6
7
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.hashers import make_password
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
8
9
from dietela_quiz.models import DietProfile
from dietela_quiz.serializers import DietProfileSerializer
10
11
from payment.models import Cart
from payment.serializers import CartSerializer
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
12
from .models import CustomUser
Kefas Satrio Bangkit Solidedantyo's avatar
Kefas Satrio Bangkit Solidedantyo committed
13
from .serializers import CustomUserDetailsSerializer, CustomLoginSerializer
14
from .google_utils import validate_google_token
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
15

Kefas Satrio Bangkit Solidedantyo's avatar
Kefas Satrio Bangkit Solidedantyo committed
16
17
18
class CustomLoginView(LoginView):
    serializer_class = CustomLoginSerializer

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
19

20
class LinkDataViewSet(viewsets.ViewSet):
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
21
22
23
24
25

    @staticmethod
    def create(request):
        email = request.data['email']
        diet_profile_id = request.data['diet_profile_id']
26
        cart_id = request.data['cart_id']
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
27
28
29
30
31
32
33
34
35
36
37
38

        try:
            user = CustomUser.objects.get(email=email)
        except CustomUser.DoesNotExist:
            return Response({'message': 'User is not found.'}, status=status.HTTP_400_BAD_REQUEST)

        try:
            diet_profile = DietProfile.objects.get(id=diet_profile_id)
        except DietProfile.DoesNotExist:
            return Response({'message': 'Diet profile is not found.'}, \
                status=status.HTTP_400_BAD_REQUEST)

39
40
41
42
43
        try:
            cart = Cart.objects.get(id=cart_id)
        except Cart.DoesNotExist:
            return Response({'message': 'Cart is not found.'}, status=status.HTTP_400_BAD_REQUEST)

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
44
        user.diet_profile = diet_profile
45
        user.nutritionist = cart.nutritionist
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
46
47
        user.save()

48
49
50
        cart.user = user
        cart.save()

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
51
52
        return Response({\
            'user': CustomUserDetailsSerializer(user).data,\
53
54
            'diet_profile': DietProfileSerializer(diet_profile).data, \
            'cart': CartSerializer(cart).data \
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
55
        }, status=status.HTTP_200_OK)
56
57
58
59
60
61
62
63
64


class GoogleView(APIView):
    def post(self, request):
        success, google_data = validate_google_token(request.data.get('access_token'))
        if not success:
            return google_data
        try:
            user = CustomUser.objects.get(email=
65
                google_data.get('email')
66
67
            )
        except CustomUser.DoesNotExist:
68
69
70
71
72
73
            user = CustomUser.objects.create_user(
                email=google_data.get('email'),
                name=google_data.get('name'),
                # random default password
                password=make_password(BaseUserManager().make_random_password()),
            )
74
75
76
77
78
79
        token = RefreshToken.for_user(user)  # generate token
        response = {}
        response['user'] = CustomUserDetailsSerializer(user).data
        response['access_token'] = str(token.access_token)
        response['refresh_token'] = str(token)
        return Response(response)