Coverage for memberships/serializers/__init__.py: 96%
33 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-06-07 11:59 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-06-07 11:59 -0600
1from .administration import CommentSerializer, MembershipExportSerializer
2from .membership import (
3 AffiliationSerializer,
4 MembershipSerializer,
5 ReferralsSerializer,
6 MembershipMemberSerializer,
7 MemberDirectorySerializer,
8 MembershipRenewalSerializer,
9 BasicMembershipSerializer,
10)
11from .classification import ExtraFieldSerializer, TagSerializer, TagCategorySerializer
12from .contact import (
13 MembershipEmailSerializer,
14 MembershipPhoneSerializer,
15 MembershipAddressSerializer,
16 MembershipContactSerializer,
17)
19from rest_framework import serializers
20from app.stripe.actions import Stripe
21from ..models import PaymentMethod, MembershipFile, MembershipPeriod
24class PaymentMethodSerializer(serializers.ModelSerializer):
25 """
26 Serializer for Payment Method Create through Stripe.
27 And representation from Model
28 """
30 number = serializers.CharField(max_length=20, write_only=True)
31 exp_month = serializers.IntegerField(write_only=True)
32 exp_year = serializers.IntegerField(write_only=True)
33 cvc = serializers.CharField(max_length=5, write_only=True)
35 class Meta:
36 model = PaymentMethod
37 read_only_fields = ["reference_name", "stripe_id", "random_slug"]
38 fields = ["membership", "number", "exp_month", "exp_year", "cvc"] + read_only_fields
40 def create(self, validated_data):
41 membership = validated_data.pop("membership")
42 try:
43 pm_id, brand = Stripe(membership=membership).create_membership_payment_method(validated_data)
44 except Exception as e:
45 raise serializers.ValidationError({e.code: e.user_message})
46 return PaymentMethod.objects.create(
47 membership=membership, stripe_id=pm_id, reference_name=f'{brand}-{validated_data.get("number")[-4:]}'
48 )
51class MembershipFileSerializer(serializers.ModelSerializer):
52 """Serializer for Membership File"""
54 class Meta:
55 model = MembershipFile
56 read_only_fields = ["random_slug", "membership", "file_type", "file"]
57 fields = read_only_fields + ["status", "valid_until"]
60class MembershipPeriodSerializer(serializers.ModelSerializer):
61 """Serializer for Memberships periods"""
63 class Meta:
64 model = MembershipPeriod
65 fields = ["created_at", "random_slug", "period_start", "period_end", "product"]
66 read_only_fields = fields