Skip to content

Commit

Permalink
♻️ [#4588] Reduce code duplication in payment related code
Browse files Browse the repository at this point in the history
in different places, the same query was being done to find successful SubmissionPayments, this has been moved to a method on the queryset to avoid duplication
  • Loading branch information
stevenbal authored and robinmolen committed Oct 15, 2024
1 parent 3e1df81 commit b261061
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
18 changes: 13 additions & 5 deletions src/openforms/payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ def mark_registered(self):
qs = self.filter(status=PaymentStatus.completed)
return qs.update(status=PaymentStatus.registered)

def get_completed_public_order_ids(self) -> list[str]:
return list(
self.filter(
status__in=(PaymentStatus.registered, PaymentStatus.completed)
).values_list("public_order_id", flat=True)
def paid(self) -> models.QuerySet["SubmissionPayment"]:
return self.filter(
status__in=(PaymentStatus.registered, PaymentStatus.completed)
)

def get_completed_public_order_ids(self) -> list[str]:
return list(self.paid().values_list("public_order_id", flat=True))

def get_completed_provider_payment_ids(self) -> list[str]:
return list(self.paid().values_list("provider_payment_id", flat=True))


class SubmissionPaymentManager(models.Manager.from_queryset(SubmissionPaymentQuerySet)):
def create_for(
Expand Down Expand Up @@ -80,6 +84,10 @@ def create_public_order_id_for(

def mark_registered(self) -> int: ...

def paid(self) -> models.QuerySet["SubmissionPayment"]: ...

def get_completed_provider_payment_ids(self) -> list[str]: ...

def get_completed_public_order_ids(self) -> list[str]: ...


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from django.utils.translation import gettext_lazy as _

from openforms.authentication.service import AuthAttribute, BaseAuth
from openforms.payments.constants import PaymentStatus
from openforms.plugins.registry import BaseRegistry
from openforms.variables.base import BaseStaticVariable
from openforms.variables.constants import FormVariableDataTypes
Expand Down Expand Up @@ -104,11 +103,7 @@ def get_initial_value(self, submission: Submission | None = None):
if submission is None:
return None

return list(
submission.payments.filter(
status__in=(PaymentStatus.registered, PaymentStatus.completed)
).values_list("provider_payment_id", flat=True)
)
return submission.payments.get_completed_provider_payment_ids()


@register("cosign_data")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
)
from openforms.formio.service import FormioData
from openforms.formio.typing import Component
from openforms.payments.constants import PaymentStatus
from openforms.registrations.exceptions import RegistrationFailed
from openforms.submissions.exports import create_submission_export
from openforms.submissions.mapping import SKIP, FieldConf, apply_data_mapping
Expand Down Expand Up @@ -361,11 +360,7 @@ def get_payment_context_data(submission: Submission) -> dict[str, Any]:
"completed": submission.payment_user_has_paid,
"amount": str(amount),
"public_order_ids": submission.payments.get_completed_public_order_ids(),
"provider_payment_ids": list(
submission.payments.filter(
status__in=(PaymentStatus.registered, PaymentStatus.completed)
).values_list("provider_payment_id", flat=True)
),
"provider_payment_ids": submission.payments.get_completed_provider_payment_ids(),
}

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from django.utils.translation import gettext_lazy as _

from openforms.payments.constants import PaymentStatus
from openforms.plugins.registry import BaseRegistry
from openforms.variables.base import BaseStaticVariable
from openforms.variables.constants import FormVariableDataTypes
Expand Down Expand Up @@ -69,8 +68,4 @@ def get_initial_value(self, submission: Submission | None = None):
if submission is None:
return None

return list(
submission.payments.filter(
status__in=(PaymentStatus.registered, PaymentStatus.completed)
).values_list("provider_payment_id", flat=True)
)
return submission.payments.get_completed_provider_payment_ids()
4 changes: 1 addition & 3 deletions src/openforms/submissions/models/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,7 @@ def payment_required(self) -> bool:
@property
def payment_user_has_paid(self) -> bool:
# TODO support partial payments
return self.payments.filter(
status__in=(PaymentStatus.registered, PaymentStatus.completed)
).exists()
return self.payments.paid().exists()

@property
def payment_registered(self) -> bool:
Expand Down

0 comments on commit b261061

Please sign in to comment.