Coverage for apps/payments/resources.py: 66%
28 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 djmoney.money import Money
2from import_export import resources
3from import_export.fields import Field
5from apps.payments.models import OrganizationPaymentMethod
7from .models import Payment
10class PaymentMethodField(Field):
11 def get_value(self, payment):
12 """
13 Custom payment method field
14 """
16 if payment.payment_method == self.attribute:
17 return payment.amount
18 else:
19 return Money(0, currency="MXN")
22class PaymentResource(resources.ModelResource):
23 """
24 Import export resource for Payment
25 """
27 date = Field(attribute="date", column_name="Fecha de Pago")
28 amount = Field(attribute="amount", column_name="Monto")
29 patient = Field(attribute="patient__full_name", column_name="Paciente")
31 class Meta:
32 models = Payment
33 fields = ["date", "patient", "amount"]
34 queryset = Payment.objects.all()
36 @property
37 def payment_methods(self):
38 return OrganizationPaymentMethod.objects.filter(
39 is_active=True, organization__in=self.get_queryset().values("payment_method__organization__pk")
40 )
42 def get_queryset(self):
43 return self.Meta.queryset
45 def get_fields(self, **kwargs):
46 for payment_method in self.payment_methods:
47 payment_method_field = PaymentMethodField(column_name=payment_method.name, attribute=payment_method)
48 self.fields[f"payment_method_{payment_method.pk}"] = payment_method_field
49 return super().get_fields(**kwargs)