Coverage for apps/users/views.py: 42%
102 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-05-09 16:59 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-05-09 16:59 -0600
1import json
3import requests
4from django.http import HttpRequest
5from django.shortcuts import redirect
6from rest_framework.decorators import action
7from rest_framework.mixins import CreateModelMixin as Create
8from rest_framework.mixins import RetrieveModelMixin as Detail
9from rest_framework.mixins import UpdateModelMixin as Update
10from rest_framework.permissions import IsAuthenticated
11from rest_framework.response import Response
12from rest_framework.reverse import reverse
13from rest_framework.serializers import ValidationError
14from rest_framework.viewsets import GenericViewSet
16from apps.practitioners.services import PractitionerGoogleHandler
17from apps.users.mixins import PublicMixin
18from services.google import GoogleService
20from .models import GoogleAccount, User
21from .serializers import (
22 GoogleAccountSerializer,
23 GoogleValidateSerializer,
24 ResetPasswordSerializer,
25 SetPasswordSerializer,
26 UserSerializer,
27)
28from .services import send_password_reset_email
31class UserViewSet(PublicMixin, GenericViewSet, Create, Update, Detail):
32 serializer_class = UserSerializer
34 def get_permissions(self):
35 if self.action == "color":
36 return [IsAuthenticated()]
37 return super().get_permissions()
39 def get_object(self):
40 return self.request.user
42 @action(detail=False, methods=["PATCH"])
43 def color(self, request, *args, **kwargs):
44 primary_color = request.data.get("primary_color", None)
45 if primary_color is None:
46 raise ValidationError({"primary_color": "No puede ser nulo"})
47 user = self.get_object()
48 user.primary_color = primary_color
49 user.save()
50 return super().retrieve(request, args, kwargs)
52 @action(detail=False, methods=["PATCH"])
53 def onboarded(self, request, *args, **kwargs):
54 user = self.get_object()
55 user.onboarding_needed = False
56 user.save()
57 return super().retrieve(request, args, kwargs)
59 @action(detail=False, methods=["GET"])
60 def whoami(self, request, *args, **kwargs):
61 return super().retrieve(request, args, kwargs)
63 @action(methods=["POST"], detail=False, url_path="fetchcalendars")
64 def fetch_calendars(self, request, *args, **kwargs):
65 practitioner = getattr(self.request.user, "practitioner", None)
66 calendars = PractitionerGoogleHandler(practitioner).setup_calendars()
67 if not calendars:
68 raise ValidationError({"error al traer los calendarios"})
70 return self.retrieve(request, *args, **kwargs)
72 @action(detail=False, methods=["POST"])
73 def reset(self, request):
74 data = request.data
75 serializer = ResetPasswordSerializer(data=data)
76 serializer.is_valid(raise_exception=True)
77 url, user = serializer.save(data)
78 send_password_reset_email(user, url)
80 return Response()
82 @action(detail=False, methods=["POST"])
83 def confirm(self, request):
84 data = request.data
85 serializer = SetPasswordSerializer(data=data)
86 serializer.is_valid(raise_exception=True)
87 serializer.save(data)
89 return Response()
92class GoogleAccountViewSet(PublicMixin, GenericViewSet, Create, Update):
93 model = GoogleAccount
94 lookup_field = "state"
95 serializer_class = GoogleAccountSerializer
96 queryset = GoogleAccount.objects.filter(awaiting_code=True)
98 @action(detail=True, methods=["PATCH"])
99 def validate(self, request, *args, **kwargs):
100 """
101 [PATCH] api/v1/googleaccoutns/<state_str>/validate/
102 {code: <code_str>}
103 """
104 data = request.data
105 instance = self.get_object()
106 serializer = GoogleValidateSerializer(instance, data=data)
107 serializer.is_valid(raise_exception=True)
108 serializer.save()
110 data = {
111 "access_token": instance.token,
112 }
114 r = requests.post(reverse("google_login", request=request), data=data)
115 if r.ok:
116 user_id = r.json().get("user").get("pk")
117 user = User.objects.get(pk=int(user_id))
118 self.perform_update(serializer)
120 try:
121 previous_google_account = GoogleAccount.objects.filter(user=user)
122 previous_google_account.delete()
123 except Exception as e:
124 print(e)
126 instance.user = user
127 instance.save()
128 return Response(data=r.json())
129 else:
130 r.raise_for_status()
131 return Response(data=r.raise_for_status())
133 @action(detail=False, methods=["POST"])
134 def connect(self, request):
135 data = request.data
136 serializer = SetPasswordSerializer(data=data)
137 serializer.is_valid(raise_exception=True)
138 serializer.save(data)
140 return Response()