Fakultas Ilmu Komputer UI

views.py 3.02 KB
Newer Older
Muzaki Azami's avatar
Muzaki Azami committed
1
import datetime
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
2
3
from rest_framework import viewsets, status
from rest_framework.response import Response
4
5
from rest_framework.views import APIView
from rest_framework_simplejwt.tokens import RefreshToken
Kefas Satrio Bangkit Solidedantyo's avatar
Kefas Satrio Bangkit Solidedantyo committed
6
from dj_rest_auth.views import LoginView
7
8
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
9
10
from dietela_quiz.models import DietProfile
from dietela_quiz.serializers import DietProfileSerializer
11
12
from payment.models import Cart
from payment.serializers import CartSerializer
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
13
from .models import CustomUser
Kefas Satrio Bangkit Solidedantyo's avatar
Kefas Satrio Bangkit Solidedantyo committed
14
from .serializers import CustomUserDetailsSerializer, CustomLoginSerializer
15
from .google_utils import validate_google_token
Muzaki Azami's avatar
Muzaki Azami committed
16
from .utilities import get_first_deadline
Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
17

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

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
21

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

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

        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)

41
42
43
44
45
        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
46
        user.diet_profile = diet_profile
47
        user.nutritionist = cart.nutritionist
Muzaki Azami's avatar
Muzaki Azami committed
48
49
        user.deadline = get_first_deadline(datetime.date.today())

Glenda Emanuella Sutanto's avatar
Glenda Emanuella Sutanto committed
50
51
        user.save()

52
53
54
        cart.user = user
        cart.save()

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


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=
69
                google_data.get('email')
70
71
            )
        except CustomUser.DoesNotExist:
72
73
74
75
76
77
            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()),
            )
78
79
80
81
82
83
        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)