views.py
2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from cloud.s3_function import *
from django.shortcuts import render
from rest_framework.authtoken.models import Token
from accounts.serializers import (
UserSerializer,
LoginSerializer,
SignUpSerializer,
)
from rest_framework import viewsets, permissions, generics, status
from rest_framework.response import Response
from django.contrib.auth.models import User
# LoadUserAPI
# ssr user loading
# used for read_only endpoints to represent a single model instance.
class UserAPI(generics.RetrieveAPIView):
permission_classes = [
permissions.IsAuthenticated,
]
serializer_class = UserSerializer
def get_object(self):
print('Load Me 인증 성공', self.request.user)
user = UserSerializer(self.request.user).data
return self.request.user
# SignUpAPI
# 회원가입 API
class SignUpAPI(generics.GenericAPIView):
serializer_class = SignUpSerializer
def post(self, request, *args, **kwargs):
print(request.data)
serializer = self.get_serializer(data=request.data)
print(serializer)
serializer.is_valid(raise_exception=True)
user = serializer.save()
print(user.username)
create_bucket(user.username)
settings.AWS_STORAGE_BUCKET_NAME = user.username
token, created = Token.objects.get_or_create(user=user)
return Response({
'user': UserSerializer(
user, context=self.get_serializer_context()
).data,
'token': token.key
})
# LoginAPI: req.data(username, password)=>deserializer=>valid(authenticate)=>serializer
# 로그인 API
class LoginAPI(generics.GenericAPIView):
# field : username, password
serializer_class = LoginSerializer
def post(self, request, *args, **kwargs):
print('login request: ', request.data)
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data # complex type data
token, created = Token.objects.get_or_create(user=user)
print("User: ", user)
print("Token: ", token)
settings.AWS_STORAGE_BUCKET_NAME = user.username
return Response({
'user': UserSerializer(
user, context=self.get_serializer_context()
).data,
'token': token.key
})