Projet

Général

Profil

0001-lingo-report-errors-on-invoices-retrieval-43967.patch

Benjamin Dauvergne, 18 juin 2020 11:00

Télécharger (5,82 ko)

Voir les différences:

Subject: [PATCH] lingo: report errors on invoices retrieval (#43967)

 combo/apps/lingo/models.py                    | 41 +++++++++++++++----
 .../lingo/templates/lingo/combo/items.html    |  9 +++-
 tests/test_lingo_remote_regie.py              | 11 +++++
 3 files changed, 53 insertions(+), 8 deletions(-)
combo/apps/lingo/models.py
25 25
from dateutil import parser
26 26
import eopayment
27 27
from jsonfield import JSONField
28
from requests import RequestException
28 29

  
29 30
from django import template
30 31
from django.conf import settings
......
118 119
        return eopayment.Payment(self.service, options)
119 120

  
120 121

  
122
class RegieException(Exception):
123
    pass
124

  
125

  
121 126
@python_2_unicode_compatible
122 127
class Regie(models.Model):
123 128
    label = models.CharField(verbose_name=_('Label'), max_length=64)
......
176 181
            url = self.webservice_url + '/invoices/'
177 182
            if history:
178 183
                url += 'history/'
179
            items = requests.get(url, user=user, remote_service='auto', cache_duration=0).json()
184

  
185
            regie_exc_msg = _('Regie "%(label)s" is unavailable, please retry later.') % {
186
                'label': self.label,
187
            }
188

  
189
            try:
190
                response = requests.get(url, user=user, remote_service='auto', cache_duration=0)
191
                response.raise_for_status()
192
            except RequestException as e:
193
                raise RegieException(regie_exc_msg) from e
194
            try:
195
                items = response.json()
196
            except ValueError as e:
197
                raise RegieException(regie_exc_msg) from e
180 198
            if items.get('data'):
181 199
                return [build_remote_item(item, self) for item in items.get('data')]
182 200
            return []
......
693 711
        return Regie.objects.all()
694 712

  
695 713
    def get_invoices(self, user):
696
        return []
714
        return [], []
697 715

  
698 716
    def get_cell_extra_context(self, context):
699 717
        ctx = super(Items, self).get_cell_extra_context(context)
......
701 719
            # don't call webservices when we're just looking for placeholders
702 720
            return ctx
703 721
        ctx.update({'title': self.title, 'text': self.text})
704
        items = self.get_invoices(user=context['user'])
722
        items, errors = self.get_invoices(user=context['user'])
705 723
        none_date = datetime.datetime(1900, 1, 1)  # to avoid None-None comparison errors
706 724
        items.sort(key=lambda i: i.creation_date or none_date, reverse=True)
707 725
        ctx.update({
708 726
            'items': items,
727
            'errors': errors,
709 728
            'with_payment_limit_date': any(i.payment_limit_date for i in items),
710 729
            'with_amount_paid': any(getattr(i, 'amount_paid', None) for i in items),
711 730
        })
......
726 745

  
727 746
    def get_invoices(self, user):
728 747
        items = []
748
        errors = []
729 749
        for r in self.get_regies():
730
            items.extend(r.get_invoices(user, history=True))
731
        return items
750
            try:
751
                items.extend(r.get_invoices(user, history=True))
752
            except RegieException as e:
753
                errors.append(e)
754
        return items, errors
732 755

  
733 756

  
734 757
@register_cell_class
......
740 763

  
741 764
    def get_invoices(self, user):
742 765
        items = []
766
        errors = []
743 767
        for r in self.get_regies():
744
            items.extend(r.get_invoices(user))
745
        return items
768
            try:
769
                items.extend(r.get_invoices(user))
770
            except RegieException as e:
771
                errors.append(e)
772
        return items, errors
746 773

  
747 774

  
748 775
@register_cell_class
combo/apps/lingo/templates/lingo/combo/items.html
1 1
{% load i18n %}
2 2
{% block cell-content %}
3
{% if items or not cell.hide_if_empty %}
3
{% if errors or items or not cell.hide_if_empty %}
4 4
{% if title %}<h2>{{ title|safe }}</h2>{% endif %}
5 5
<div>
6 6
{% if text %}{{ text|safe }}{% endif %}
7
{% if errors %}
8
  <ul class="errorlist">
9
    {% for error in errors %}
10
      <li>{{ error }}</li>
11
    {% endfor %}
12
  </ul>
13
{% endif %}
7 14
{% if items %}
8 15
<table class="invoices">
9 16
  <thead>
tests/test_lingo_remote_regie.py
129 129
    content = cell.render(context)
130 130
    assert 'No items yet' in content
131 131

  
132
    # regie is down
133
    mock_send.side_effect = ConnectionError
134
    content = cell.render(context)
135
    assert 'Regie &quot;Remote&quot; is unavailable, please retry later.' in content
136

  
137

  
132 138
@mock.patch('combo.utils.requests_wrapper.RequestsSession.send')
133 139
def test_remote_regie_past_invoices_cell(mock_send, remote_regie):
134 140
    assert remote_regie.is_remote() == True
......
215 221
    content = cell.render(context)
216 222
    assert content.strip() == ''
217 223

  
224
    # regie is down
225
    mock_send.side_effect = ConnectionError
226
    content = cell.render(context)
227
    assert 'Regie &quot;Remote&quot; is unavailable, please retry later.' in content
228

  
218 229

  
219 230
@mock.patch('combo.apps.lingo.models.Regie.pay_invoice')
220 231
@mock.patch('combo.apps.lingo.models.requests.get')
221
-