views.py
2.1 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
import json
from django.db import IntegrityError
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods
from rest_framework.authtoken.models import Token
from rest_framework.decorators import permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from .models import User
class UserView(APIView):
permission_classes(IsAuthenticated,)
def put(self, request):
email = request.data.get('email')
updating = User.objects.get(email=email)
nick_name = request.data.get('nick_name')
profile_image = request.data.get('profile_image')
region = request.data.get('region')
if nick_name:
updating.nick_name = nick_name
if profile_image:
updating.profile_image = profile_image
if region:
updating.region = region
updating.save()
return JsonResponse({'result': 'ok'})
def delete(self, request):
email = request.data.get('email')
deleting = User.objects.get(email=email)
deleting.delete()
return JsonResponse({'result': 'ok'})
def get(self, request):
email = request.data.get('email')
retrieved = User.objects.get(email=email)
return JsonResponse({'result': 'ok', 'email': str(retrieved.email), 'nick_name': str(retrieved.nick_name),
'region': str(retrieved.region),'period': str(retrieved.period)})
@require_http_methods(['POST'])
def sign_up(request):
data = json.loads(request.body.decode("utf-8"))
email = data['email']
password = data['password']
try:
user = User.objects.create_user(email, password)
except ValueError:
return JsonResponse({'result': 'ValueError'})
except IntegrityError:
return JsonResponse({'result': 'IntegrityError'})
try:
token = Token.objects.get(user=user)
except Token.DoesNotExist:
return JsonResponse({'result': 'Token does not exist'})
return JsonResponse({'result': 'ok', 'id': str(user.pk), 'token': str(token.key)})