-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
108 lines (91 loc) · 3.45 KB
/
api.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from drf_spectacular.utils import extend_schema
from rest_framework import mixins, status, viewsets
from rest_framework.decorators import action
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework import routers
from .serializers import (
UserChangePasswordErrorSerializer,
UserChangePasswordSerializer,
UserCreateErrorSerializer,
UserCreateSerializer,
UserCurrentErrorSerializer,
UserCurrentSerializer, PermissionSerializer,
)
User = get_user_model()
class UserViewSet(
mixins.CreateModelMixin,
viewsets.GenericViewSet,
):
queryset = User.objects.all()
serializer_class = UserCurrentSerializer
permission_classes = [IsAuthenticated]
lookup_field = "username"
def get_queryset(self):
return self.queryset.filter(pk=self.request.user.pk)
def get_permissions(self):
if self.action == "create":
return [AllowAny()]
return super().get_permissions()
def get_serializer_class(self):
if self.action == "create":
return UserCreateSerializer
elif self.action == "me":
return UserCurrentSerializer
elif self.action == "change_password":
return UserChangePasswordSerializer
return super().get_serializer_class()
@extend_schema(
responses={
200: UserCreateSerializer,
400: UserCreateErrorSerializer,
}
)
def create(self, request, *args, **kwargs):
return super().create(request, *args, **kwargs)
@extend_schema(
responses={
200: UserCurrentSerializer,
400: UserCurrentErrorSerializer,
}
)
@action(["get", "put", "patch"], detail=False)
def me(self, request, *args, **kwargs):
if request.method == "GET":
serializer = self.get_serializer(self.request.user)
return Response(serializer.data)
elif request.method == "PUT":
serializer = self.get_serializer(
self.request.user, data=request.data, partial=False
)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
elif request.method == "PATCH":
serializer = self.get_serializer(
self.request.user, data=request.data, partial=True
)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
@extend_schema(
responses={
204: None,
400: UserChangePasswordErrorSerializer,
}
)
@action(["post"], url_path="change-password", detail=False)
def change_password(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.request.user.set_password(serializer.data["password_new"])
self.request.user.save()
return Response(status=status.HTTP_204_NO_CONTENT)
@action(["delete"], url_path="delete-account", detail=False)
def delete_account(self, request, *args, **kwargs):
self.request.user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
authentication_router = routers.DefaultRouter()
authentication_router.register("users", UserViewSet, basename="api-users")