From 9433b7de92980ea397dd7caf7893fdb9d0dea391 Mon Sep 17 00:00:00 2001 From: charlieezh <123706642+charlieezh@users.noreply.github.com> Date: Fri, 26 Apr 2024 17:06:02 -0400 Subject: [PATCH 1/2] Add files via upload created a form in the html to filter the bills --- ihatemoney/templates/list_bills.html | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ihatemoney/templates/list_bills.html b/ihatemoney/templates/list_bills.html index 79e252625..58efd6cc4 100644 --- a/ihatemoney/templates/list_bills.html +++ b/ihatemoney/templates/list_bills.html @@ -105,6 +105,16 @@
  • {{ _("Older bills") }} »
  • {% endif %} +
    + {{ csrf_form.csrf_token }} + + + + + + + +
    {{ static_include("images/plus.svg") | safe }} From 9b514ca7c7a832a4eeca083b4c106b0615001e64 Mon Sep 17 00:00:00 2001 From: charlieezh <123706642+charlieezh@users.noreply.github.com> Date: Fri, 26 Apr 2024 17:07:39 -0400 Subject: [PATCH 2/2] Add files via upload implemented the filter feature for bills based on start date and end date. --- ihatemoney/models.py | 9 +++++++++ ihatemoney/web.py | 44 +++++++++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/ihatemoney/models.py b/ihatemoney/models.py index c591b85b6..11554d5b8 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -264,6 +264,12 @@ def order_bills(query): .order_by(Bill.id.desc()) ) + @staticmethod + def filter(query, start_date, end_date): + return ( + query.filter(Bill.date >= start_date, Bill.date <= end_date) + ) + def get_bill_weights(self): """ Return all bills for this project, along with the sum of weight for each bill. @@ -285,6 +291,9 @@ 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): + return self.filter(self.get_bill_weights_ordered(), start_date, end_date) + def get_member_bills(self, member_id): """Return the list of bills related to a specific member""" return ( diff --git a/ihatemoney/web.py b/ihatemoney/web.py index 4b5c82c53..13702e7f7 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -642,7 +642,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 @@ -666,19 +666,37 @@ 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 - ) + 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'] + weighted_bills = g.project.get_filtered_bill_weights_ordered(start_date, end_date).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, + ) - 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", - ) @main.route("//members/add", methods=["GET", "POST"])