Coverage for users/views.py: 55%
38 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-11-03 16:42 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2022-11-03 16:42 -0600
1from rest_framework import status
2from rest_framework.decorators import action
3from rest_framework.permissions import AllowAny
4from rest_framework.response import Response
5from rest_framework.viewsets import GenericViewSet
6from rest_framework.mixins import UpdateModelMixin
7from rest_framework_simplejwt.tokens import RefreshToken, AccessToken
8from .serializers import (
9 UserSerializer,
10 RegisterSerializer,
11 ResetPasswordSerializer,
12 SetPasswordSerializer,
13)
14from .services import send_password_reset_email
15from .models import User
18class UsersViewSet(GenericViewSet, UpdateModelMixin):
19 permission_classes = [AllowAny]
20 queryset = User.objects.all()
21 serializer_class = UserSerializer
23 @action(methods=["POST"], detail=False)
24 def register(self, request):
25 serializer = RegisterSerializer(data=request.data)
26 serializer.is_valid(True)
27 user = serializer.save()
28 data = serializer.data
29 data["access"] = str(AccessToken.for_user(user))
30 data["refresh"] = str(RefreshToken.for_user(user))
31 return Response(data, status=status.HTTP_201_CREATED)
33 @action(detail=False, methods=["POST"])
34 def reset(self, request):
35 data = request.data
36 serializer = ResetPasswordSerializer(data=data)
37 serializer.is_valid(True)
38 url, user = serializer.save(data)
39 send_password_reset_email(user, url)
41 return Response()
43 @action(detail=False, methods=["POST"])
44 def confirm(self, request):
45 data = request.data
46 serializer = SetPasswordSerializer(data=data)
47 serializer.is_valid(True)
48 serializer.save(data)
50 return Response()