From 4d9663123a65d5b3833ad353c8b781edc33b3968 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Tue, 20 Jun 2017 09:02:13 +0200 Subject: [PATCH] add coinifyapiinvoice property to order model --- src/shop/models.py | 15 +++++++++++++++ src/shop/views.py | 12 +----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/shop/models.py b/src/shop/models.py index 4911d11c..1163e361 100644 --- a/src/shop/models.py +++ b/src/shop/models.py @@ -226,6 +226,21 @@ class Order(CreatedUpdatedModel): self.open = None self.save() + @property + def coinifyapiinvoice(self): + if not self.coinify_api_invoices.exists(): + return False + + coinifyinvoice = None + for tempinvoice in self.coinify_api_invoices.all(): + # we already have a coinifyinvoice for this order, check if it expired + if not tempinvoice.expired: + # this invoice is not expired, we are good to go + return tempinvoice + + # nope + return False + class ProductCategory(CreatedUpdatedModel, UUIDModel): class Meta: diff --git a/src/shop/views.py b/src/shop/views.py index 081e1eac..4c56996c 100644 --- a/src/shop/views.py +++ b/src/shop/views.py @@ -535,18 +535,8 @@ class CoinifyRedirectView(LoginRequiredMixin, EnsureUserOwnsOrderMixin, EnsureUn def dispatch(self, request, *args, **kwargs): order = self.get_object() - # check if we already have a coinifyinvoice for this order - if order.coinify_api_invoices.exists(): - coinifyinvoice = None - for tempinvoice in order.coinify_api_invoices.all(): - # we already have a coinifyinvoice for this order, check if it expired - if not tempinvoice.expired: - # this invoice is not expired, we are good to go - coinifyinvoice = tempinvoice - break - # create a new coinify invoice if needed - if not coinifyinvoice: + if not order.coinifyapiinvoice: coinifyinvoice = create_coinify_invoice(order, request) if not coinifyinvoice: messages.error(request, "There was a problem with the payment provider. Please try again later")