Project

General

Profile

0001-lingo-poll-backend-during-asynchronous-rendering-577.patch

Benjamin Dauvergne, 13 Oct 2021 08:18 PM

Download (4.98 KB)

View differences:

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(-)
combo/apps/lingo/models.py
638 638
        ordering = ['regie', 'extra_fee', 'subject']
639 639

  
640 640
    @classmethod
641
    def get_items_to_be_paid(cls, user):
641
    def get_items_to_be_paid(cls, user, poll=False, raise_on_poll=False):
642 642
        qs = cls.objects.filter(
643 643
            user=user, payment_date__isnull=True, waiting_date__isnull=True, cancellation_date__isnull=True
644 644
        )
645
        for transaction in Transaction.objects.filter(items__in=qs):
646
            if transaction.can_poll_backend():
647
                transaction.poll_backend()
645
        if poll:
646
            for transaction in Transaction.objects.filter(items__in=qs):
647
                if transaction.can_poll_backend():
648
                    if raise_on_poll:
649
                        raise NothingInCacheException
650
                    transaction.poll_backend()
648 651
        return qs
649 652

  
650 653
    def notify(self, status):
......
1087 1090

  
1088 1091
    def render(self, context):
1089 1092
        basket_template = template.loader.get_template('lingo/combo/basket.html')
1090
        items = BasketItem.get_items_to_be_paid(context['request'].user)
1093
        items = BasketItem.get_items_to_be_paid(
1094
            context['request'].user, poll=True, raise_on_poll=not context.get('synchronous')
1095
        )
1091 1096
        regies = {}
1092 1097
        for item in items:
1093 1098
            if not item.regie_id in regies:
......
1112 1117
    def is_enabled(cls):
1113 1118
        return Regie.objects.exists()
1114 1119

  
1115
    def get_transactions_queryset(self, context):
1120
    def get_transactions_queryset(self, context, poll=False):
1116 1121
        user = context['request'].user
1117 1122
        # list transactions :
1118 1123
        # * paid by the user
......
1120 1125
        qs = Transaction.objects.filter(models.Q(user=user) | models.Q(items__user=user)).filter(
1121 1126
            start_date__gte=timezone.now() - datetime.timedelta(days=7)
1122 1127
        )
1123
        for transaction in qs:
1124
            if transaction.can_poll_backend() and transaction.is_running():
1125
                transaction.poll_backend()
1128
        if poll:
1129
            for transaction in qs:
1130
                if transaction.can_poll_backend() and transaction.is_running():
1131
                    if not context.get('synchronous'):
1132
                        raise NothingInCacheException
1133
                    transaction.poll_backend()
1126 1134
        return qs
1127 1135

  
1128 1136
    def is_relevant(self, context):
......
1132 1140

  
1133 1141
    def render(self, context):
1134 1142
        recent_transactions_template = template.loader.get_template('lingo/combo/recent_transactions.html')
1135
        context['transactions'] = self.get_transactions_queryset(context).distinct().order_by('-start_date')
1143
        context['transactions'] = (
1144
            self.get_transactions_queryset(context, poll=True).distinct().order_by('-start_date')
1145
        )
1136 1146
        return recent_transactions_template.render(context)
1137 1147

  
1138 1148

  
tests/test_lingo_payment.py
2153 2153
            self,
2154 2154
            payment_status,
2155 2155
            app,
2156
            user,
2157
            synchronous_cells,
2156 2158
        ):
2157 2159
            # Try to pay
2158 2160
            pay_resp = app.get('/test_basket_cell/')
......
2171 2173
                order_id=transaction.order_id,
2172 2174
            )
2173 2175

  
2176
            # check get_items_to_be_paid() does not poll anymore
2177
            BasketItem.get_items_to_be_paid(user)
2178
            assert payment_status.call_count == 0
2179

  
2174 2180
            # Try to pay again
2175 2181
            resp = app.get('/test_basket_cell/')
2176 2182
            assert 'foo item' not in resp
......
2180 2186
            assert 'Some items are already paid or' in resp
2181 2187
            assert 'foo item' not in resp
2182 2188
            assert 'Running' in resp
2189
            # two cells, two requests, one call per cell, and synchronous
2190
            # rendering is on
2191
            assert payment_status.call_count == 4
2183 2192

  
2184 2193
            # Simulate paid status on polling
2185 2194
            payment_status.return_value = eopayment.common.PaymentResponse(
......
2199 2208
            self,
2200 2209
            payment_status,
2201 2210
            app,
2211
            synchronous_cells,
2202 2212
            caplog,
2203 2213
        ):
2204 2214
            # Try to pay
2205
-