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="col-md-2 mb-2">
|
||||
<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 class="col-md-2 mb-2">
|
||||
<label for="payer" class="sr-only">{{ _("Payer") }}</label>
|
||||
<select class="form-control form-control-sm w-100" id="payer" name="payer">
|
||||
<option value="">{{ _('Any payer') }}</option>
|
||||
{% for member in g.project.active_members %}
|
||||
<option value="{{ member.id }}" {% if filter_payer|string == member.id|string %}selected{% endif %}>{{ member.name }}</option>
|
||||
{% endfor %}
|
||||
<option value="">{{ _('Any payer') }}</option>
|
||||
{% for member in g.project.active_members %}
|
||||
<option value="{{ member.id }}" {% if payer|string == member.id|string %}selected{% endif %}>
|
||||
{{ member.name }}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-2 mb-2">
|
||||
<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 class="col-md-2 mb-2">
|
||||
<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 class="col-md-1 mb-2">
|
||||
<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 class="col-md-1 mb-2">
|
||||
<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 class="col-md-8 mb-2 text-left">
|
||||
<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]
|
||||
|
||||
# get filter values
|
||||
search_query = request.args.get('search', '')
|
||||
filter_date_from = request.args.get('date_from', '')
|
||||
filter_date_to = request.args.get('date_to', '')
|
||||
filter_amount_min = request.args.get('amount_min', '')
|
||||
filter_amount_max = request.args.get('amount_max', '')
|
||||
filter_payer = request.args.get('payer', '')
|
||||
# Extract filter values from request
|
||||
filters = {
|
||||
"search": request.args.get("search", "").strip(),
|
||||
"date_from": request.args.get("date_from", "").strip(),
|
||||
"date_to": request.args.get("date_to", "").strip(),
|
||||
"amount_min": request.args.get("amount_min", "").strip(),
|
||||
"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:
|
||||
# Each item will be a (weight_sum, Bill) tuple.
|
||||
# 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
|
||||
)
|
||||
weighted_bills = g.project.get_bill_weights_ordered().paginate(per_page=100, error_out=True)
|
||||
else:
|
||||
# start with all bills
|
||||
query = Bill.query.filter(Bill.payer_id == Person.id).filter(Person.project_id == g.project.id)
|
||||
query = Bill.query.join(Person).filter(Person.project_id == g.project.id)
|
||||
|
||||
# apply filters if there are any
|
||||
if search_query:
|
||||
query = query.filter(Bill.what.ilike(f'%{search_query}%'))
|
||||
if filters["search"]:
|
||||
query = query.filter(Bill.what.ilike(f'%{filters["search"]}%'))
|
||||
|
||||
if filter_date_from:
|
||||
date_obj = datetime.datetime.strptime(filter_date_from, '%Y-%m-%d').date()
|
||||
query = query.filter(Bill.date >= date_obj)
|
||||
if filters["date_from"]:
|
||||
query = query.filter(Bill.date >= datetime.datetime.strptime(filters["date_from"], '%Y-%m-%d').date())
|
||||
|
||||
if filter_date_to:
|
||||
date_obj = datetime.datetime.strptime(filter_date_to, '%Y-%m-%d').date()
|
||||
query = query.filter(Bill.date <= date_obj)
|
||||
if filters["date_to"]:
|
||||
query = query.filter(Bill.date <= datetime.datetime.strptime(filters["date_to"], '%Y-%m-%d').date())
|
||||
|
||||
if filter_amount_min:
|
||||
query = query.filter(Bill.amount >= float(filter_amount_min))
|
||||
if filters["amount_min"]:
|
||||
query = query.filter(Bill.amount >= float(filters["amount_min"]))
|
||||
|
||||
if filter_amount_max:
|
||||
query = query.filter(Bill.amount <= float(filter_amount_max))
|
||||
if filters["amount_max"]:
|
||||
query = query.filter(Bill.amount <= float(filters["amount_max"]))
|
||||
|
||||
if filter_payer:
|
||||
query = query.filter(Bill.payer_id == filter_payer)
|
||||
if filters["payer"]:
|
||||
query = query.filter(Bill.payer_id == filters["payer"])
|
||||
|
||||
filtered_bill_ids = [bill.id for bill in query.all()]
|
||||
bills_query = g.project.get_bill_weights().filter(Bill.id.in_(filtered_bill_ids))
|
||||
filtered_bill_ids = query.with_entities(Bill.id).all()
|
||||
|
||||
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)
|
||||
weighted_bills = bills_query.paginate(per_page=100, error_out=True)
|
||||
|
||||
|
@ -727,13 +721,8 @@ def list_bills():
|
|||
csrf_form=csrf_form,
|
||||
add_bill=request.values.get("add_bill", False),
|
||||
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,
|
||||
**filters # Unpack filter values for template use
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue