From fec940f7c3a09cafa0ace74e4706f56e3f94b89b Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 13 Oct 2021 19:34:59 +0200 Subject: [PATCH] lingo: poll backend during asynchronous rendering (#57790) --- combo/apps/lingo/models.py | 30 ++++++++++++++++++++---------- tests/test_lingo_payment.py | 10 ++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py index c8f150a6..8784420c 100644 --- a/combo/apps/lingo/models.py +++ b/combo/apps/lingo/models.py @@ -638,13 +638,16 @@ class BasketItem(models.Model): ordering = ['regie', 'extra_fee', 'subject'] @classmethod - def get_items_to_be_paid(cls, user): + def get_items_to_be_paid(cls, user, poll=False, raise_on_poll=False): qs = cls.objects.filter( user=user, payment_date__isnull=True, waiting_date__isnull=True, cancellation_date__isnull=True ) - for transaction in Transaction.objects.filter(items__in=qs): - if transaction.can_poll_backend(): - transaction.poll_backend() + if poll: + for transaction in Transaction.objects.filter(items__in=qs): + if transaction.can_poll_backend(): + if raise_on_poll: + raise NothingInCacheException + transaction.poll_backend() return qs def notify(self, status): @@ -1087,7 +1090,9 @@ class LingoBasketCell(CellBase): def render(self, context): basket_template = template.loader.get_template('lingo/combo/basket.html') - items = BasketItem.get_items_to_be_paid(context['request'].user) + items = BasketItem.get_items_to_be_paid( + context['request'].user, poll=True, raise_on_poll=not context.get('synchronous') + ) regies = {} for item in items: if not item.regie_id in regies: @@ -1112,7 +1117,7 @@ class LingoRecentTransactionsCell(CellBase): def is_enabled(cls): return Regie.objects.exists() - def get_transactions_queryset(self, context): + def get_transactions_queryset(self, context, poll=False): user = context['request'].user # list transactions : # * paid by the user @@ -1120,9 +1125,12 @@ class LingoRecentTransactionsCell(CellBase): qs = Transaction.objects.filter(models.Q(user=user) | models.Q(items__user=user)).filter( start_date__gte=timezone.now() - datetime.timedelta(days=7) ) - for transaction in qs: - if transaction.can_poll_backend() and transaction.is_running(): - transaction.poll_backend() + if poll: + for transaction in qs: + if transaction.can_poll_backend() and transaction.is_running(): + if not context.get('synchronous'): + raise NothingInCacheException + transaction.poll_backend() return qs def is_relevant(self, context): @@ -1132,7 +1140,9 @@ class LingoRecentTransactionsCell(CellBase): def render(self, context): recent_transactions_template = template.loader.get_template('lingo/combo/recent_transactions.html') - context['transactions'] = self.get_transactions_queryset(context).distinct().order_by('-start_date') + context['transactions'] = ( + self.get_transactions_queryset(context, poll=True).distinct().order_by('-start_date') + ) return recent_transactions_template.render(context) diff --git a/tests/test_lingo_payment.py b/tests/test_lingo_payment.py index a03aede0..13b1c6f1 100644 --- a/tests/test_lingo_payment.py +++ b/tests/test_lingo_payment.py @@ -2153,6 +2153,8 @@ class TestPolling: self, payment_status, app, + user, + synchronous_cells, ): # Try to pay pay_resp = app.get('/test_basket_cell/') @@ -2171,6 +2173,10 @@ class TestPolling: order_id=transaction.order_id, ) + # check get_items_to_be_paid() does not poll anymore + BasketItem.get_items_to_be_paid(user) + assert payment_status.call_count == 0 + # Try to pay again resp = app.get('/test_basket_cell/') assert 'foo item' not in resp @@ -2180,6 +2186,9 @@ class TestPolling: assert 'Some items are already paid or' in resp assert 'foo item' not in resp assert 'Running' in resp + # two cells, two requests, one call per cell, and synchronous + # rendering is on + assert payment_status.call_count == 4 # Simulate paid status on polling payment_status.return_value = eopayment.common.PaymentResponse( @@ -2199,6 +2208,7 @@ class TestPolling: self, payment_status, app, + synchronous_cells, caplog, ): # Try to pay -- 2.33.0