Projet

Général

Profil

0001-lingo-filter-transaction-list-by-regie-56651.patch

Lauréline Guérin, 01 octobre 2021 15:33

Télécharger (6,59 ko)

Voir les différences:

Subject: [PATCH 1/2] lingo: filter transaction list by regie (#56651)

 combo/apps/lingo/forms.py                     |  9 +++++
 combo/apps/lingo/manager_views.py             | 11 +++++-
 .../templates/lingo/transaction_list.html     | 13 +++----
 tests/test_lingo_manager.py                   | 39 +++++++++++++++++--
 4 files changed, 58 insertions(+), 14 deletions(-)
combo/apps/lingo/forms.py
152 152
        today = datetime.date.today()
153 153
        self.initial['start_date'] = today - datetime.timedelta(days=30)
154 154
        self.initial['end_date'] = today
155

  
156

  
157
class TransactionSearchForm(forms.Form):
158
    regie = forms.ModelChoiceField(queryset=Regie.objects.none(), empty_label=_('All regies'), required=False)
159
    q = forms.CharField(required=False)
160

  
161
    def __init__(self, *args, **kwargs):
162
        super().__init__(*args, **kwargs)
163
        self.fields['regie'].queryset = Regie.objects.all()
combo/apps/lingo/manager_views.py
30 30
from django.utils.translation import ugettext_lazy as _
31 31
from django.views.generic import CreateView, DeleteView, ListView, UpdateView, View
32 32

  
33
from .forms import PaymentBackendForm, RegieForm, TransactionExportForm
33
from .forms import PaymentBackendForm, RegieForm, TransactionExportForm, TransactionSearchForm
34 34
from .models import BasketItem, PaymentBackend, Regie, Transaction
35 35

  
36 36

  
......
96 96
    def get_context_data(self, **kwargs):
97 97
        context = super().get_context_data(**kwargs)
98 98
        context['query'] = self.request.GET.get('q') or ''
99
        context['form'] = self.form
99 100
        return context
100 101

  
101 102
    def get_queryset(self):
103
        self.form = TransactionSearchForm(data=self.request.GET)
102 104
        qs = (
103 105
            Transaction.objects.select_related('user')
104 106
            .prefetch_related(Prefetch('items', to_attr='prefetched_items'))
105 107
            .filter(status__in=(eopayment.PAID, eopayment.ACCEPTED))
106 108
            .order_by('-start_date')
107 109
        )
108
        query = self.request.GET.get('q')
110
        query, regie = None, None
111
        if self.form.is_valid():
112
            query = self.form.cleaned_data['q']
113
            regie = self.form.cleaned_data['regie']
114
        if regie:
115
            qs = qs.filter(regie=regie)
109 116
        if query:
110 117
            try:
111 118
                date = date_parser.parse(query, dayfirst=True)
combo/apps/lingo/templates/lingo/transaction_list.html
14 14
{% block content %}
15 15

  
16 16
<form>
17
  <p><input name="q" type="search" value="{{query}}"> <button>{% trans 'Search' %}</button>
17
  <p>
18
  {{ form.regie }} <input name="q" type="search" value="{{ form.cleaned_data.q }}"> <button>{% trans 'Search' %}</button>
18 19
  <span class="help_text">{% trans "(supports order identifiers, transaction identifiers, and dates)" %}</span>
19 20
  </p>
20 21
</form>
......
55 56

  
56 57
{% else %}
57 58
<div class="big-msg-info">
58
  {% if query %}
59
  {% blocktrans %}
60
  No transactions found matching "{{query}}".
61
  {% endblocktrans %}
59
  {% if form.cleaned_data.q or form.cleaned_data.regie %}
60
  {% trans "No transactions found matching the current search." %}
62 61
  {% else %}
63
  {% blocktrans %}
64
  This site doesn't have any transaction yet.
65
  {% endblocktrans %}
62
  {% trans "This site doesn't have any transaction yet." %}
66 63
  {% endif %}
67 64
</div>
68 65
{% endif %}
tests/test_lingo_manager.py
27 27
    return PaymentBackend.objects.create(label='test1', slug='test1', service='dummy', service_options={})
28 28

  
29 29

  
30
@pytest.fixture
31
def regie(payment_backend):
32
    return Regie.objects.create(label='Test', slug='test', payment_backend=payment_backend)
33

  
34

  
30 35
def login(app, username='admin', password='admin'):
31 36
    login_page = app.get('/login/')
32 37
    login_form = login_page.forms[0]
......
235 240
    assert Decimal(row[6]) == b_item.amount
236 241

  
237 242

  
238
def test_transactions_search(app, admin_user):
243
def test_transactions_search(app, admin_user, payment_backend, regie):
244
    regie2 = Regie.objects.create(label='Test-2', slug='regie-2', payment_backend=payment_backend)
239 245
    for i in range(50):
240
        Transaction(
246
        transaction = Transaction(
241 247
            status=eopayment.PAID,
242 248
            order_id='order id %s' % (i + 1),
243 249
            bank_transaction_id='bank id %s' % (i + 1),
244 250
            amount=1 + i,
245
        ).save()
251
        )
252
        if i % 3 == 0:
253
            transaction.regie = regie
254
        elif i % 3 == 1:
255
            transaction.regie = regie2
256
        transaction.save()
246 257

  
247 258
    app = login(app)
248 259
    with CaptureQueriesContext(connection) as ctx:
249 260
        resp = app.get('/manage/lingo/', status=200)
250 261
    assert resp.text.count('<tr') == 11
251
    assert len(ctx.captured_queries) == 5
262
    assert len(ctx.captured_queries) == 6
252 263

  
253 264
    resp.form['q'] = 'order id 16'
254 265
    resp = resp.form.submit()
......
263 274
    assert resp.text.count('<tr') == 0
264 275
    assert 'No transactions found matching' in resp.text
265 276

  
277
    resp.form['q'] = ''
278
    resp.form['regie'] = regie.pk
279
    resp = resp.form.submit()
280
    assert resp.text.count('<tr') == 11
281

  
282
    resp.form['q'] = ''
283
    resp.form['regie'] = regie2.pk
284
    resp = resp.form.submit()
285
    assert resp.text.count('<tr') == 11
286

  
287
    resp.form['q'] = 'order id 5'
288
    resp.form['regie'] = regie.pk
289
    resp = resp.form.submit()
290
    assert resp.text.count('<tr') == 0
291

  
292
    resp.form['q'] = 'order id 5'
293
    resp.form['regie'] = regie2.pk
294
    resp = resp.form.submit()
295
    assert resp.text.count('<tr') == 2
296

  
266 297

  
267 298
def test_basketitem_error_list(app, admin_user, payment_backend):
268 299
    regie = Regie.objects.create(label='test-regie', slug='test-regie', payment_backend=payment_backend)
269
-