Coverage for users/views.py: 74%
38 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
1from rest_framework.decorators import action
2from rest_framework.mixins import CreateModelMixin as Create
3from rest_framework.permissions import AllowAny
4from rest_framework.response import Response
5from rest_framework.viewsets import GenericViewSet
6from users.models import User
7from .permissions import AdminPanel
8from .serializers import UserSerializer, FunctionarySerializer, ResetPasswordSerializer, SetPasswordSerializer
9from .services import send_password_reset_email
12class UsersViewSet(GenericViewSet, Create):
13 """
14 Django User Model ViewSet
15 """
17 serializer_class = UserSerializer
18 queryset = User.objects.all().order_by("-date_joined")
19 permission_classes = [AllowAny]
21 @action(detail=True, methods=["POST"])
22 def inactivate(self, request, pk):
23 user = self.get_object()
24 user.is_active = False
25 user.save()
26 serializer = self.get_serializer(user)
27 return Response(serializer.data)
29 @action(detail=False, methods=["POST"], permission_classes=[AllowAny])
30 def reset(self, request):
31 data = request.data
32 serializer = ResetPasswordSerializer(data=data)
33 serializer.is_valid(True)
34 url, user = serializer.save(data)
35 send_password_reset_email(user, url)
37 return Response()
39 @action(detail=False, methods=["POST"], permission_classes=[AllowAny])
40 def confirm(self, request):
41 data = request.data
42 serializer = SetPasswordSerializer(data=data)
43 serializer.is_valid(True)
44 serializer.save(data)
46 return Response()
49class FunctionaryViewSet(GenericViewSet, Create):
50 """
51 ViewSet for Functionary
52 """
54 serializer_class = FunctionarySerializer
55 permission_classes = [AdminPanel]