Coverage for apps/products/views.py: 86%
52 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-19 09:45 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-19 09:45 -0600
1from rest_framework.mixins import CreateModelMixin as Create
2from rest_framework.mixins import ListModelMixin as List
3from rest_framework.mixins import RetrieveModelMixin as Detail
4from rest_framework.viewsets import GenericViewSet, ModelViewSet
6from app.stripe.serializers import StripePriceSerializer, StripeProductSerializer
7from apps.users.mixins import AdminMixin, UserMixin
9from .filters import ProductFilter
10from .serializers import (
11 ConsultationProductSerializer,
12 PolymorphicProductSerializer,
13 ProcedureProductSerializer,
14 ProductOrganizationPriceSerializer,
15 SellableProductSerializer,
16 SurgicalProductSerializer,
17)
20class AdminProductViewSet(AdminMixin, ModelViewSet):
21 """
22 Model ViewSet for Products
23 """
25 serializer_class = PolymorphicProductSerializer
28class AdminProductOrganizationPriceViewSet(AdminMixin, ModelViewSet):
29 """
30 Model ViewSet for Product Organizations
31 """
33 serializer_class = ProductOrganizationPriceSerializer
36class AdminStripeProductViewSet(AdminMixin, GenericViewSet, Create, List, Detail):
37 """
38 Stripe Product ViewSet for Admin
39 """
41 serializer_class = StripeProductSerializer
44class AdminStripePriceViewSet(AdminMixin, GenericViewSet, Create, List, Detail):
45 """
46 Stripe Price ViewSet for Admin
47 """
49 serializer_class = StripePriceSerializer
52class UserStripeProductViewSet(UserMixin, GenericViewSet, List, Detail):
53 """
54 Stripe Product ViewSet for User
55 """
57 serializer_class = StripeProductSerializer
60class UserProductViewSet(UserMixin, ModelViewSet):
61 """
62 Model ViewSet for Products
63 """
65 serializer_class = PolymorphicProductSerializer
66 filterset_class = ProductFilter
67 ordering_fields = ["name", "polymorphic_ctype", "ordering"]
69 def create(self, request, *args, **kwargs):
70 request.data["practitioner"] = request.user.practitioner.pk
71 return super().create(request, args, kwargs)
73 def perform_destroy(self, instance):
74 instance.is_active = False
75 instance.save()
77 def get_queryset(self, *args, **kwargs):
78 return super().get_queryset(*args, **kwargs).filter(practitioner__user=self.request.user, is_active=True)
81class UserProductOrganizationPriceViewSet(UserMixin, ModelViewSet):
82 """
83 Model ViewSet for Product Organizations
84 """
86 serializer_class = ProductOrganizationPriceSerializer
87 filterset_fields = ["product", "organization"]
88 ordering_fields = ["prodcut__name", "prodcut__polymorphic_ctype", "prodcut__ordering"]
90 def get_queryset(self):
91 return (
92 super()
93 .get_queryset()
94 .filter(organization__in=self.user.affiliations.values_list("organization"), product__is_active=True)
95 )
98class UserConsultationProductViewSet(UserMixin, ModelViewSet):
99 """
100 Model ViewSet for Consultation Products
101 """
103 serializer_class = ConsultationProductSerializer
105 def get_queryset(self, *args, **kwargs):
106 return super().get_queryset(*args, **kwargs).filter(practitioner__user=self.request.user, is_active=True)
109class UserProcedureProductViewSet(UserMixin, ModelViewSet):
110 """
111 Model ViewSet for Procedure Products
112 """
114 serializer_class = ProcedureProductSerializer
116 def get_queryset(self, *args, **kwargs):
117 return super().get_queryset(*args, **kwargs).filter(practitioner__user=self.request.user, is_active=True)
120class UserSellableProductViewSet(UserMixin, ModelViewSet):
121 """
122 Model ViewSet for Sellable Products
123 """
125 serializer_class = SellableProductSerializer
127 def get_queryset(self, *args, **kwargs):
128 return super().get_queryset(*args, **kwargs).filter(practitioner__user=self.request.user, is_active=True)
131class UserSurgicalProductViewSet(UserMixin, ModelViewSet):
132 """
133 Model ViewSet for Surgical Products
134 """
136 serializer_class = SurgicalProductSerializer
138 def get_queryset(self, *args, **kwargs):
139 return super().get_queryset(*args, **kwargs).filter(practitioner__user=self.request.user, is_active=True)