Coverage for apps/users/views.py: 48%
75 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-22 14:26 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-22 14:26 -0600
1from rest_framework.decorators import action
2from rest_framework.exceptions import ValidationError
3from rest_framework.mixins import CreateModelMixin as Create
4from rest_framework.mixins import DestroyModelMixin as Delete
5from rest_framework.mixins import ListModelMixin as List
6from rest_framework.mixins import RetrieveModelMixin as Detail
7from rest_framework.mixins import UpdateModelMixin as Update
8from rest_framework.permissions import AllowAny
9from rest_framework.response import Response
10from rest_framework.views import APIView
11from rest_framework.viewsets import GenericViewSet
13from apps.organizations.models import Organization
14from apps.users.mixins import EmployeeMixin, FanMixin
15from apps.users.permissions import EmployeePanel, SuperUserPanel
17from .serializers import (
18 EmployeeSerializer,
19 Fan,
20 FanSerializer,
21 ResetPasswordSerializer,
22 SetPasswordSerializer,
23 UserPolymorphicSerializer,
24)
25from .services import send_password_reset_email
28class WhoAmIView(APIView):
29 """
30 View for current user
31 """
33 def get(self, request):
34 serializer = UserPolymorphicSerializer(request.user, context={"request": request})
35 return Response(serializer.data)
38class FanViewSet(FanMixin, GenericViewSet, Create, Update, Delete):
39 """
40 ViewSet for Fan
41 """
43 model_organization = "registered_organizations"
44 serializer_class = FanSerializer
45 queryset = Fan.objects.all()
47 def get_permissions(self):
48 if self.action == "create":
49 return [AllowAny()]
50 else:
51 return [permission() for permission in self.permission_classes]
53 def create(self, request, *args, **kwargs):
54 origin = request.headers.get("Origin", None)
55 try:
56 organization = Organization.objects.get(domain=origin)
57 except Organization.DoesNotExist:
58 raise ValidationError("There is no organization for the current origin")
60 parent_organization = organization.get_root().pk
62 try:
63 fan = Fan.objects.get(parent_organization=parent_organization, email=request.data["email"])
64 if organization in fan.registered_organizations.all():
65 raise ValidationError(
66 {"email": "There is already a user with the same email registered on this organization"}
67 )
68 fan.registered_organizations.add(organization)
69 for organization in organization.get_ancestors():
70 fan.registered_organizations.add(organization)
71 fan.save()
72 serializer = self.get_serializer(fan)
73 return Response(serializer.data)
74 except Fan.DoesNotExist:
75 request.data["parent_organization"] = organization.get_root().pk
76 request.data["registered_organizations"] = [
77 organization.random_slug for organization in organization.get_ancestors()
78 ]
79 request.data["registered_organizations"].append(organization.random_slug)
80 return super().create(request, *args, **kwargs)
82 def destroy(self, request, *args, **kwargs):
83 return super().destroy(request, args, kwargs)
85 @action(detail=False, methods=["POST"], permission_classes=[AllowAny])
86 def reset(self, request):
87 data = request.data
88 serializer = ResetPasswordSerializer(data=data, context={"request": request})
89 serializer.is_valid(True)
90 url, user = serializer.save(data)
91 send_password_reset_email(user, url)
93 return Response()
95 @action(detail=False, methods=["POST"], permission_classes=[AllowAny])
96 def confirm(self, request):
97 data = request.data
98 serializer = SetPasswordSerializer(data=data)
99 serializer.is_valid(True)
100 serializer.save(data)
102 return Response()
105class EmployeeFanViewSet(EmployeeMixin, GenericViewSet, List, Detail):
106 """
107 pass
108 """
110 model_organization = "registered_organizations"
111 serializer_class = FanSerializer
112 filterset_fields = {"registered_organizations": ["exact"]}
113 search_fields = ["email", "first_name", "last_name"]
116class EmployeeViewSet(GenericViewSet, Create):
117 """
118 ViewSet for Employee
119 """
121 permission_classes = [SuperUserPanel | EmployeePanel]
122 serializer_class = EmployeeSerializer