lingo: filter transaction list by regie (#56651)
This commit is contained in:
parent
f765f0be8a
commit
54c277a36e
|
@ -152,3 +152,12 @@ class TransactionExportForm(forms.Form):
|
|||
today = datetime.date.today()
|
||||
self.initial['start_date'] = today - datetime.timedelta(days=30)
|
||||
self.initial['end_date'] = today
|
||||
|
||||
|
||||
class TransactionSearchForm(forms.Form):
|
||||
regie = forms.ModelChoiceField(queryset=Regie.objects.none(), empty_label=_('All regies'), required=False)
|
||||
q = forms.CharField(required=False)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['regie'].queryset = Regie.objects.all()
|
||||
|
|
|
@ -30,7 +30,7 @@ from django.utils.timezone import make_aware, now
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.views.generic import CreateView, DeleteView, ListView, UpdateView, View
|
||||
|
||||
from .forms import PaymentBackendForm, RegieForm, TransactionExportForm
|
||||
from .forms import PaymentBackendForm, RegieForm, TransactionExportForm, TransactionSearchForm
|
||||
from .models import BasketItem, PaymentBackend, Regie, Transaction
|
||||
|
||||
|
||||
|
@ -97,16 +97,23 @@ class TransactionListView(ListView):
|
|||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['query'] = self.request.GET.get('q') or ''
|
||||
context['form'] = self.form
|
||||
return context
|
||||
|
||||
def get_queryset(self):
|
||||
self.form = TransactionSearchForm(data=self.request.GET)
|
||||
qs = (
|
||||
Transaction.objects.select_related('user')
|
||||
.prefetch_related(Prefetch('items', to_attr='prefetched_items'))
|
||||
.filter(status__in=(eopayment.PAID, eopayment.ACCEPTED))
|
||||
.order_by('-start_date')
|
||||
)
|
||||
query = self.request.GET.get('q')
|
||||
query, regie = None, None
|
||||
if self.form.is_valid():
|
||||
query = self.form.cleaned_data['q']
|
||||
regie = self.form.cleaned_data['regie']
|
||||
if regie:
|
||||
qs = qs.filter(regie=regie)
|
||||
if query:
|
||||
try:
|
||||
date = date_parser.parse(query, dayfirst=True)
|
||||
|
|
|
@ -14,7 +14,8 @@
|
|||
{% block content %}
|
||||
|
||||
<form>
|
||||
<p><input name="q" type="search" value="{{query}}"> <button>{% trans 'Search' %}</button>
|
||||
<p>
|
||||
{{ form.regie }} <input name="q" type="search" value="{{ form.cleaned_data.q }}"> <button>{% trans 'Search' %}</button>
|
||||
<span class="help_text">{% trans "(supports order identifiers, transaction identifiers, and dates)" %}</span>
|
||||
</p>
|
||||
</form>
|
||||
|
@ -55,14 +56,10 @@
|
|||
|
||||
{% else %}
|
||||
<div class="big-msg-info">
|
||||
{% if query %}
|
||||
{% blocktrans %}
|
||||
No transactions found matching "{{query}}".
|
||||
{% endblocktrans %}
|
||||
{% if form.cleaned_data.q or form.cleaned_data.regie %}
|
||||
{% trans "No transactions found matching the current search." %}
|
||||
{% else %}
|
||||
{% blocktrans %}
|
||||
This site doesn't have any transaction yet.
|
||||
{% endblocktrans %}
|
||||
{% trans "This site doesn't have any transaction yet." %}
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
|
|
@ -233,20 +233,26 @@ def test_download_transaction(app, admin_user, regie):
|
|||
assert Decimal(row[6]) == b_item.amount
|
||||
|
||||
|
||||
def test_transactions_search(app, admin_user):
|
||||
def test_transactions_search(app, admin_user, payment_backend, regie):
|
||||
regie2 = Regie.objects.create(label='Test-2', slug='regie-2', payment_backend=payment_backend)
|
||||
for i in range(50):
|
||||
Transaction(
|
||||
transaction = Transaction(
|
||||
status=eopayment.PAID,
|
||||
order_id='order id %s' % (i + 1),
|
||||
bank_transaction_id='bank id %s' % (i + 1),
|
||||
amount=1 + i,
|
||||
).save()
|
||||
)
|
||||
if i % 3 == 0:
|
||||
transaction.regie = regie
|
||||
elif i % 3 == 1:
|
||||
transaction.regie = regie2
|
||||
transaction.save()
|
||||
|
||||
app = login(app)
|
||||
with CaptureQueriesContext(connection) as ctx:
|
||||
resp = app.get('/manage/lingo/', status=200)
|
||||
assert resp.text.count('<tr') == 11
|
||||
assert len(ctx.captured_queries) == 5
|
||||
assert len(ctx.captured_queries) == 6
|
||||
|
||||
resp.form['q'] = 'order id 16'
|
||||
resp = resp.form.submit()
|
||||
|
@ -261,6 +267,26 @@ def test_transactions_search(app, admin_user):
|
|||
assert resp.text.count('<tr') == 0
|
||||
assert 'No transactions found matching' in resp.text
|
||||
|
||||
resp.form['q'] = ''
|
||||
resp.form['regie'] = regie.pk
|
||||
resp = resp.form.submit()
|
||||
assert resp.text.count('<tr') == 11
|
||||
|
||||
resp.form['q'] = ''
|
||||
resp.form['regie'] = regie2.pk
|
||||
resp = resp.form.submit()
|
||||
assert resp.text.count('<tr') == 11
|
||||
|
||||
resp.form['q'] = 'order id 5'
|
||||
resp.form['regie'] = regie.pk
|
||||
resp = resp.form.submit()
|
||||
assert resp.text.count('<tr') == 0
|
||||
|
||||
resp.form['q'] = 'order id 5'
|
||||
resp.form['regie'] = regie2.pk
|
||||
resp = resp.form.submit()
|
||||
assert resp.text.count('<tr') == 2
|
||||
|
||||
|
||||
def test_basketitem_error_list(app, admin_user, payment_backend):
|
||||
regie = Regie.objects.create(label='test-regie', slug='test-regie', payment_backend=payment_backend)
|
||||
|
|
Loading…
Reference in New Issue