Coverage for apps/organizations/views.py: 50%
74 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-05-08 08:34 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-05-08 08:34 -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.serializers import ValidationError
11from rest_framework.viewsets import GenericViewSet, ModelViewSet
13from apps.users.mixins import AdminMixin, UserMixin
14from services.pusher import PusherClient
16from .models import Affiliation
17from .serializers import AffiliationSerializer, OrganizationRoleSerializer, OrganizationSerializer
20class AdminOrganizationViewSet(AdminMixin, ModelViewSet):
21 """
22 Model ViewSet for Organizations
23 """
25 serializer_class = OrganizationSerializer
28class AdminOrganizationRoleViewSet(AdminMixin, ModelViewSet):
29 """
30 Model ViewSet for Organization Roles
31 """
33 serializer_class = OrganizationRoleSerializer
36class AdminAffiliationViewSet(AdminMixin, ModelViewSet):
37 """
38 Model ViewSet for Affiliations
39 """
41 serializer_class = AffiliationSerializer
44class UserOrganizationViewSet(UserMixin, GenericViewSet, Create, List, Detail, Update):
45 """
46 Model ViewSet for Organizations
47 """
49 serializer_class = OrganizationSerializer
50 ordering_fields = ["name", "created_at"]
52 def get_queryset(self, *args, **kwargs):
53 return super().get_queryset(*args, **kwargs).filter(affiliation__user=self.user)
55 def _push_notification(self, channel: str, event: str = "organization-update", data: dict = {}):
56 pusher = PusherClient()
57 pusher.trigger(channel, event, data)
59 def update(self, request, *args, **kwargs):
60 organization = self.get_object()
61 user_is_owner = request.user.affiliations.get(
62 organization=organization,
63 ).is_owner
64 if not user_is_owner:
65 raise ValidationError({"Solo owners pueden editar organizaciones"})
66 self._push_notification(
67 self.get_object().random_slug,
68 "organization-update",
69 {
70 "user": getattr(request.user, "pk", None),
71 },
72 )
73 return super().update(request, *args, **kwargs)
75 def partial_update(self, request, *args, **kwargs):
76 organization = self.get_object()
77 user_is_owner = request.user.affiliations.get(
78 organization=organization,
79 ).is_owner
80 if not user_is_owner:
81 raise ValidationError({"Solo owners pueden editar organizaciones"})
83 self._push_notification(
84 self.get_object().random_slug,
85 "organization-update",
86 {
87 "user": getattr(request.user, "pk", None),
88 },
89 )
90 return super().partial_update(request, *args, **kwargs)
92 def destroy(self, request, *args, **kwargs):
93 organization = self.get_object()
94 user_is_owner = request.user.affiliations.get(
95 organization=organization,
96 ).is_owner
97 if not user_is_owner:
98 raise ValidationError({"Solo owners pueden editar organizaciones"})
99 self._push_notification(
100 self.get_object().random_slug,
101 "organization-delete",
102 {
103 "user": getattr(request.user, "pk", None),
104 },
105 )
106 return super().destroy(request, *args, **kwargs)
109class UserAffiliationViewSet(UserMixin, GenericViewSet, Create, List, Detail, Delete, Update):
110 """
111 Model ViewSet for Affiliations
112 """
114 serializer_class = AffiliationSerializer
115 filterset_fields = ["organization"]
117 def get_queryset(self, *args, **kwargs):
118 return (
119 super()
120 .get_queryset(*args, **kwargs)
121 .filter(organization__in=self.user.affiliations.values_list("organization"))
122 )
124 @action(methods=["PATCH"], detail=True, permission_classes=[AllowAny])
125 def accept(self, request, *args, **kwargs):
126 try:
127 affiliation = Affiliation.objects.get(random_slug=self.kwargs.get("pk"))
128 except Affiliation.DoesNotExist:
129 raise ValidationError({"No existe esa afiliacion"})
131 if affiliation.status != Affiliation.Status.PENDING:
132 raise ValidationError(detail="Afiliacion ya deliberada", code=403)
133 affiliation.status = Affiliation.Status.ACCEPTED
134 affiliation.save()
135 return Response(self.serializer_class(affiliation).data)
137 @action(methods=["PATCH"], detail=True, permission_classes=[AllowAny])
138 def reject(self, request, *args, **kwargs):
139 affiliation = Affiliation.objects.get(random_slug=self.kwargs.get("pk"))
140 if affiliation.status != Affiliation.Status.PENDING:
141 raise ValidationError(detail="Afiliacion ya deliberada", code=403)
142 affiliation.status = Affiliation.Status.REJECTED
143 affiliation.save()
144 return Response(self.serializer_class(affiliation).data)