mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-04-28 17:32:38 +02:00
Store filters more efficiently
This commit is contained in:
parent
5368399158
commit
87955f6647
2 changed files with 40 additions and 49 deletions
|
@ -126,32 +126,34 @@
|
||||||
<div class="form-row w-100">
|
<div class="form-row w-100">
|
||||||
<div class="col-md-2 mb-2">
|
<div class="col-md-2 mb-2">
|
||||||
<label for="search" class="sr-only">{{ _("Search") }}</label>
|
<label for="search" class="sr-only">{{ _("Search") }}</label>
|
||||||
<input type="text" class="form-control form-control-sm w-100" id="search" name="search" placeholder="{{ _('Search in For what?') }}" value="{{ search_query }}">
|
<input type="text" class="form-control form-control-sm w-100" id="search" name="search" placeholder="{{ _('Search in For what?') }}" value="{{ search }}">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-2 mb-2">
|
<div class="col-md-2 mb-2">
|
||||||
<label for="payer" class="sr-only">{{ _("Payer") }}</label>
|
<label for="payer" class="sr-only">{{ _("Payer") }}</label>
|
||||||
<select class="form-control form-control-sm w-100" id="payer" name="payer">
|
<select class="form-control form-control-sm w-100" id="payer" name="payer">
|
||||||
<option value="">{{ _('Any payer') }}</option>
|
<option value="">{{ _('Any payer') }}</option>
|
||||||
{% for member in g.project.active_members %}
|
{% for member in g.project.active_members %}
|
||||||
<option value="{{ member.id }}" {% if filter_payer|string == member.id|string %}selected{% endif %}>{{ member.name }}</option>
|
<option value="{{ member.id }}" {% if payer|string == member.id|string %}selected{% endif %}>
|
||||||
|
{{ member.name }}
|
||||||
|
</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-2 mb-2">
|
<div class="col-md-2 mb-2">
|
||||||
<label for="date_from" class="sr-only">{{ _("Start date") }}</label>
|
<label for="date_from" class="sr-only">{{ _("Start date") }}</label>
|
||||||
<input type="date" class="form-control form-control-sm w-100" id="date_from" name="date_from" value="{{ filter_date_from }}">
|
<input type="date" class="form-control form-control-sm w-100" id="date_from" name="date_from" value="{{ date_from }}">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-2 mb-2">
|
<div class="col-md-2 mb-2">
|
||||||
<label for="date_to" class="sr-only">{{ _("End date") }}</label>
|
<label for="date_to" class="sr-only">{{ _("End date") }}</label>
|
||||||
<input type="date" class="form-control form-control-sm w-100" id="date_to" name="date_to" value="{{ filter_date_to }}">
|
<input type="date" class="form-control form-control-sm w-100" id="date_to" name="date_to" value="{{ date_to }}">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-1 mb-2">
|
<div class="col-md-1 mb-2">
|
||||||
<label for="amount_min" class="sr-only">{{ _("Amount min") }}</label>
|
<label for="amount_min" class="sr-only">{{ _("Amount min") }}</label>
|
||||||
<input type="text" class="form-control form-control-sm w-100" id="amount_min" name="amount_min" placeholder="{{ _('Min cost') }}" value="{{ filter_amount_min }}">
|
<input type="text" class="form-control form-control-sm w-100" id="amount_min" name="amount_min" placeholder="{{ _('Min cost') }}" value="{{ amount_min }}">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-1 mb-2">
|
<div class="col-md-1 mb-2">
|
||||||
<label for="amount_max" class="sr-only">{{ _("Amount max") }}</label>
|
<label for="amount_max" class="sr-only">{{ _("Amount max") }}</label>
|
||||||
<input type="text" class="form-control form-control-sm w-100" id="amount_max" name="amount_max" placeholder="{{ _('Max cost') }}" value="{{ filter_amount_max }}">
|
<input type="text" class="form-control form-control-sm w-100" id="amount_max" name="amount_max" placeholder="{{ _('Max cost') }}" value="{{ amount_max }}">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-8 mb-2 text-left">
|
<div class="col-md-8 mb-2 text-left">
|
||||||
<button type="submit" class="btn btn-sm btn-primary">{{ _("Apply filters") }}</button>
|
<button type="submit" class="btn btn-sm btn-primary">{{ _("Apply filters") }}</button>
|
||||||
|
|
|
@ -671,51 +671,45 @@ def list_bills():
|
||||||
):
|
):
|
||||||
bill_form.payed_for.data = session["last_selected_payed_for"][g.project.id]
|
bill_form.payed_for.data = session["last_selected_payed_for"][g.project.id]
|
||||||
|
|
||||||
# get filter values
|
# Extract filter values from request
|
||||||
search_query = request.args.get('search', '')
|
filters = {
|
||||||
filter_date_from = request.args.get('date_from', '')
|
"search": request.args.get("search", "").strip(),
|
||||||
filter_date_to = request.args.get('date_to', '')
|
"date_from": request.args.get("date_from", "").strip(),
|
||||||
filter_amount_min = request.args.get('amount_min', '')
|
"date_to": request.args.get("date_to", "").strip(),
|
||||||
filter_amount_max = request.args.get('amount_max', '')
|
"amount_min": request.args.get("amount_min", "").strip(),
|
||||||
filter_payer = request.args.get('payer', '')
|
"amount_max": request.args.get("amount_max", "").strip(),
|
||||||
|
"payer": request.args.get("payer", "").strip(),
|
||||||
|
}
|
||||||
|
|
||||||
filters_active = any([search_query, filter_date_to, filter_date_from, filter_amount_min, filter_amount_max, filter_payer])
|
filters_active = any(filters.values())
|
||||||
|
|
||||||
# if no filters active, use standard query
|
# Fetch bills
|
||||||
if not filters_active:
|
if not filters_active:
|
||||||
# Each item will be a (weight_sum, Bill) tuple.
|
weighted_bills = g.project.get_bill_weights_ordered().paginate(per_page=100, error_out=True)
|
||||||
# TODO: improve this awkward result using column_property:
|
|
||||||
# https://docs.sqlalchemy.org/en/14/orm/mapped_sql_expr.html.
|
|
||||||
weighted_bills = g.project.get_bill_weights_ordered().paginate(
|
|
||||||
per_page=100, error_out=True
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
# start with all bills
|
query = Bill.query.join(Person).filter(Person.project_id == g.project.id)
|
||||||
query = Bill.query.filter(Bill.payer_id == Person.id).filter(Person.project_id == g.project.id)
|
|
||||||
|
|
||||||
# apply filters if there are any
|
if filters["search"]:
|
||||||
if search_query:
|
query = query.filter(Bill.what.ilike(f'%{filters["search"]}%'))
|
||||||
query = query.filter(Bill.what.ilike(f'%{search_query}%'))
|
|
||||||
|
|
||||||
if filter_date_from:
|
if filters["date_from"]:
|
||||||
date_obj = datetime.datetime.strptime(filter_date_from, '%Y-%m-%d').date()
|
query = query.filter(Bill.date >= datetime.datetime.strptime(filters["date_from"], '%Y-%m-%d').date())
|
||||||
query = query.filter(Bill.date >= date_obj)
|
|
||||||
|
|
||||||
if filter_date_to:
|
if filters["date_to"]:
|
||||||
date_obj = datetime.datetime.strptime(filter_date_to, '%Y-%m-%d').date()
|
query = query.filter(Bill.date <= datetime.datetime.strptime(filters["date_to"], '%Y-%m-%d').date())
|
||||||
query = query.filter(Bill.date <= date_obj)
|
|
||||||
|
|
||||||
if filter_amount_min:
|
if filters["amount_min"]:
|
||||||
query = query.filter(Bill.amount >= float(filter_amount_min))
|
query = query.filter(Bill.amount >= float(filters["amount_min"]))
|
||||||
|
|
||||||
if filter_amount_max:
|
if filters["amount_max"]:
|
||||||
query = query.filter(Bill.amount <= float(filter_amount_max))
|
query = query.filter(Bill.amount <= float(filters["amount_max"]))
|
||||||
|
|
||||||
if filter_payer:
|
if filters["payer"]:
|
||||||
query = query.filter(Bill.payer_id == filter_payer)
|
query = query.filter(Bill.payer_id == filters["payer"])
|
||||||
|
|
||||||
filtered_bill_ids = [bill.id for bill in query.all()]
|
filtered_bill_ids = query.with_entities(Bill.id).all()
|
||||||
bills_query = g.project.get_bill_weights().filter(Bill.id.in_(filtered_bill_ids))
|
|
||||||
|
bills_query = g.project.get_bill_weights().filter(Bill.id.in_([bill.id for bill in filtered_bill_ids]))
|
||||||
bills_query = g.project.order_bills(bills_query)
|
bills_query = g.project.order_bills(bills_query)
|
||||||
weighted_bills = bills_query.paginate(per_page=100, error_out=True)
|
weighted_bills = bills_query.paginate(per_page=100, error_out=True)
|
||||||
|
|
||||||
|
@ -727,13 +721,8 @@ def list_bills():
|
||||||
csrf_form=csrf_form,
|
csrf_form=csrf_form,
|
||||||
add_bill=request.values.get("add_bill", False),
|
add_bill=request.values.get("add_bill", False),
|
||||||
current_view="list_bills",
|
current_view="list_bills",
|
||||||
search_query=search_query,
|
|
||||||
filter_date_to=filter_date_to,
|
|
||||||
filter_date_from=filter_date_from,
|
|
||||||
filter_amount_min=filter_amount_min,
|
|
||||||
filter_amount_max=filter_amount_max,
|
|
||||||
filter_payer=filter_payer,
|
|
||||||
search_active=filters_active,
|
search_active=filters_active,
|
||||||
|
**filters # Unpack filter values for template use
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue