Coverage for memberships/views/member.py: 97%
46 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-04-14 14:48 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-04-14 14:48 -0600
1from rest_framework.decorators import action
2from rest_framework.mixins import (
3 ListModelMixin as List,
4 RetrieveModelMixin as Retrieve,
5 CreateModelMixin as Create,
6 UpdateModelMixin as Update,
7 DestroyModelMixin as Delete,
8)
9from rest_framework.response import Response
10from rest_framework.viewsets import GenericViewSet
11from app.stripe.actions import Stripe
12from ..mixins import MemberMixin
13from ..serializers import (
14 AffiliationSerializer,
15 MembershipMemberSerializer,
16 MembershipEmailSerializer,
17 MembershipPhoneSerializer,
18 MembershipAddressSerializer,
19 MembershipContactSerializer,
20 MemberDirectorySerializer,
21 PaymentMethodSerializer,
22)
23from ..filters import MembershipFilter
24from ..models import Membership
27class MemberMembershipViewSet(MemberMixin, GenericViewSet, List, Retrieve, Update):
28 """
29 Membership Model ViewSet
30 """
32 serializer_class = MembershipMemberSerializer
33 search_fields = ["name", "users__email"]
35 def get_queryset(self):
36 return self.memberships.filter(affiliation__is_active=True)
38 @action(methods=["GET"], detail=True)
39 def subsidiaries(self, request, *args, **kwargs):
40 self.queryset = Membership.objects.filter(holder=self.get_object())
41 return super(MemberMembershipViewSet, self).list(request)
43 @action(methods=["GET"], detail=True)
44 def contacts(self, request, *args, **kwargs):
45 self.queryset = self.model
46 membership = self.get_object()
47 serializer = MembershipContactSerializer(membership)
48 return Response(serializer.data)
51class MembershipEmailViewSet(MemberMixin, GenericViewSet, Create, Update, Delete):
52 """
53 Membership Emails ViewSet
54 """
56 serializer_class = MembershipEmailSerializer
59class MembershipPhoneViewSet(MemberMixin, GenericViewSet, Create, Update, Delete):
60 """
61 Membership Phones ViewSet
62 """
64 serializer_class = MembershipPhoneSerializer
67class MembershipAddressViewSet(MemberMixin, GenericViewSet, Create, Update, Delete):
68 """
69 Membership Addresss ViewSet
70 """
72 serializer_class = MembershipAddressSerializer
75class MemberAffiliationsViewSet(MemberMixin, GenericViewSet, List, Create, Update, Delete):
76 """
77 Affiliations viewset for member user
78 """
80 serializer_class = AffiliationSerializer
81 filterset_fields = ["membership"]
82 search_fields = ["name", "user__email"]
85class MemberDirectoryViewSet(MemberMixin, GenericViewSet, List, Retrieve):
86 """
87 Directory memberships view for member
88 """
90 serializer_class = MemberDirectorySerializer
91 filterset_class = MembershipFilter
92 search_fields = ["name", "emails__email"]
94 def get_queryset(self):
95 return Membership.objects.exclude(visibility=Membership.VisibilityChoices.NONE)
98class MemberPaymentMethodViewSet(MemberMixin, GenericViewSet, Create, List, Retrieve, Delete):
99 """Viewset for Member Payment Methods"""
101 LOGGING_FIELDS = ["reference_name"]
103 serializer_class = PaymentMethodSerializer
105 def perform_destroy(self, instance):
106 """
107 Destroy Model Instance and delete stripe object
108 """
109 Stripe(membership=instance.membership).delete_payment_method(instance)
110 super().perform_destroy(instance)