From 4e999808720d1953068d3346c9e58f8952797ea2 Mon Sep 17 00:00:00 2001 From: shreyaNagunuri Date: Sat, 11 Jan 2025 02:03:05 -0800 Subject: [PATCH] Adding filtering capabilities for bills --- ihatemoney/models.py | 18 +++++++++ ihatemoney/templates/list_bills.html | 58 ++++++++++++++++++++++++++++ ihatemoney/web.py | 51 +++++++++++++++++------- 3 files changed, 113 insertions(+), 14 deletions(-) diff --git a/ihatemoney/models.py b/ihatemoney/models.py index af21994d8..0d2ad6be7 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -263,6 +263,20 @@ def order_bills(query): .order_by(Bill.creation_date.desc()) .order_by(Bill.id.desc()) ) + + @staticmethod + def filter(query, start_date, end_date, payer, owers, bill_name): + if start_date: + query = query.filter(Bill.date >= start_date) + if end_date: + query = query.filter(Bill.date <= end_date) + if payer: + query = query.filter(Bill.payer_id == payer) + if owers: + query = query.filter(Bill.owers.any(Person.id.in_(owers))) + if bill_name: + query = query.filter(Bill.what.ilike(f"%{bill_name}%")) + return query def get_bill_weights(self): """ @@ -284,6 +298,10 @@ def get_bill_weights(self): def get_bill_weights_ordered(self): """Ordered version of get_bill_weights""" return self.order_bills(self.get_bill_weights()) + + def get_filtered_bill_weights_ordered(self, start_date, end_date, payer, owers, bill_name): + return self.filter(self.get_bill_weights_ordered(), start_date=start_date, end_date=end_date, payer=payer, owers=owers, bill_name=bill_name) + def get_member_bills(self, member_id): """Return the list of bills related to a specific member""" diff --git a/ihatemoney/templates/list_bills.html b/ihatemoney/templates/list_bills.html index 79e252625..55d6d3b9f 100644 --- a/ihatemoney/templates/list_bills.html +++ b/ihatemoney/templates/list_bills.html @@ -105,6 +105,64 @@
  • {{ _("Older bills") }} »
  • {% endif %} + +
    + + +
    +
    + {{ csrf_form.csrf_token }} + +
    +
    + + +
    + +
    + + +
    + +
    + + +
    + +
    + + +
    + +
    + + +
    + +
    + +
    + +
    +
    +
    +
    + {{ static_include("images/plus.svg") | safe }} diff --git a/ihatemoney/web.py b/ihatemoney/web.py index 37bd811f8..e9bf222cf 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -649,7 +649,7 @@ def invite(): return render_template("send_invites.html", form=form, qrcode=qrcode_svg) -@main.route("//") +@main.route("//", methods=["GET", "POST"]) def list_bills(): bill_form = get_billform_for(g.project) # Used for CSRF validation @@ -673,19 +673,42 @@ def list_bills(): # 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 - ) - - return render_template( - "list_bills.html", - bills=weighted_bills, - member_form=MemberForm(g.project), - bill_form=bill_form, - csrf_form=csrf_form, - add_bill=request.values.get("add_bill", False), - current_view="list_bills", - ) + if request.method == "GET": + weighted_bills = g.project.get_bill_weights_ordered().paginate( + per_page=100, error_out=True + ) + return render_template( + "list_bills.html", + bills=weighted_bills, + member_form=MemberForm(g.project), + bill_form=bill_form, + csrf_form=csrf_form, + add_bill=request.values.get("add_bill", False), + current_view="list_bills", + ) + if request.method == "POST": + start_date = request.form['start_date'] + end_date = request.form['end_date'] + payer = request.form.get('payer', type=int) + owers = request.form.getlist('owers', type=int) + bill_name = request.form.get('bill_name', '').strip() + weighted_bills = g.project.get_filtered_bill_weights_ordered(start_date, end_date, payer, owers, bill_name).paginate( + per_page=100, error_out=True + ) + return render_template( + "list_bills.html", + bills=weighted_bills, + member_form=MemberForm(g.project), + bill_form=bill_form, + csrf_form=csrf_form, + add_bill=request.values.get("add_bill", False), + current_view="list_bills", + start_date=start_date, + end_date=end_date, + selected_payer=payer, + selected_owers=owers, + bill_name=bill_name, + ) @main.route("//members/add", methods=["GET", "POST"])