Coverage for payments/resources.py: 50%
68 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-06-08 12:01 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-06-08 12:01 -0600
1from import_export import resources, fields
2from .models import Payment, ManualPayment, PaymentIntent, PaymentAllocation
5class PaymentResource(resources.ModelResource):
6 date = fields.Field()
7 membership_type = fields.Field()
9 class Meta:
10 model = Payment
11 fields = ["date", "membership_type"]
13 def dehydrate_date(self, instance):
14 if isinstance(instance, ManualPayment):
15 return instance.date
16 elif isinstance(instance, PaymentIntent):
17 return instance.created_at.date()
18 return instance.created_at
20 def dehydrate_membership_type(self, instance):
21 return instance.membership.type_of.name
24class PaymentAllocationResource(resources.ModelResource):
25 allocation_date = fields.Field()
26 payment_date = fields.Field()
27 membership_type = fields.Field()
28 allocation_amount = fields.Field()
29 payment_amount = fields.Field()
30 payment_reference = fields.Field()
31 concept = fields.Field()
32 product = fields.Field()
33 segment = fields.Field()
34 segment_comment = fields.Field()
35 product_comment = fields.Field()
36 product_variation_comment = fields.Field()
38 class Meta:
39 model = PaymentAllocation
40 fields = [
41 "allocation_date",
42 "payment_date",
43 "membership_type",
44 "allocation_amount",
45 "payment_amount",
46 "payment_reference",
47 "concept",
48 "product",
49 "segment",
50 "segment_comment",
51 "product_comment",
52 "product_variation_comment",
53 ]
55 def dehydrate_allocation_date(self, instance):
56 return instance.created_at.date()
58 def dehydrate_payment_date(self, instance):
59 payment = instance.payment
60 if isinstance(payment, ManualPayment):
61 return payment.date
62 elif isinstance(payment, PaymentIntent):
63 return payment.created_at.date()
64 return payment.created_at
66 def dehydrate_membership_type(self, instance):
67 return instance.payment.membership.type_of.name
69 def dehydrate_allocation_amount(self, instance):
70 return instance.amount
72 def dehydrate_payment_amount(self, instance):
73 return instance.payment.amount
75 def dehydrate_payment_reference(self, instance):
76 payment = instance.payment
77 if isinstance(payment, ManualPayment):
78 return payment.reference
79 elif isinstance(payment, PaymentIntent):
80 return payment.stripe_pi_id
82 def dehydrate_concept(self, instance):
83 return instance.payment.concept
85 def dehydrate_product(self, instance):
86 return instance.product_charge.product.name
88 def dehydrate_segment(self, instance):
89 return instance.product_charge.membership.segment
91 def dehydrate_segment_comment(self, instance):
92 segment = instance.payment.membership.segment
93 if segment is not None:
94 return segment.comment
96 def dehydrate_product_comment(self, instance):
97 return instance.product_charge.product.comment
99 def dehydrate_product_variation_comment(self, instance):
100 product_purchase = instance.product_charge.purchase
101 return ", ".join(
102 variation.get("comment") for variation in product_purchase.price_breakdown.get("variations", [])
103 )