From 5e710fb7f93abd6c23f44b47cacdd03a3be953a3 Mon Sep 17 00:00:00 2001 From: Sylvieox Date: Tue, 7 May 2024 04:22:52 -0400 Subject: [PATCH] Adding filter option for the bills --- docker-compose.yml | 2 +- hatch_build.py | 1 - ihatemoney.db | Bin 0 -> 139264 bytes ihatemoney/conf-templates/ihatemoney.cfg.j2 | 2 +- ihatemoney/default_settings.py | 2 +- ihatemoney/models.py | 89 +++++++++++++++++--- ihatemoney/templates/list_bills.html | 37 ++++++++ ihatemoney/web.py | 60 ++++++++++--- 8 files changed, 165 insertions(+), 28 deletions(-) create mode 100644 ihatemoney.db diff --git a/docker-compose.yml b/docker-compose.yml index df893beb..cea47350 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -33,7 +33,7 @@ services: - SECRET_KEY=tralala - SESSION_COOKIE_SECURE=True - SHOW_ADMIN_EMAIL=True - - SQLALCHEMY_DATABASE_URI=sqlite:////database/ihatemoney.db + - SQLALCHEMY_DATABASE_URI=sqlite:///database/ihatemoney.db - SQLALCHEMY_TRACK_MODIFICATIONS=False - APPLICATION_ROOT=/ - ENABLE_CAPTCHA=False diff --git a/hatch_build.py b/hatch_build.py index d536daed..bedd89b3 100644 --- a/hatch_build.py +++ b/hatch_build.py @@ -1,5 +1,4 @@ import sys - from hatchling.builders.hooks.plugin.interface import BuildHookInterface diff --git a/ihatemoney.db b/ihatemoney.db new file mode 100644 index 0000000000000000000000000000000000000000..b82834f27440f7340613d89af34235315fc82c91 GIT binary patch literal 139264 zcmeI5Uu+x6eaH6?$t9^j<2zg4(2chEUV2WpzP$WK>N~{y!0XHlaULOhafNc(|ZXDG(dp{?V;%5&_1M>rn!TGHrHH$ zc4q&OoLy2pyG~BleOKagcjh;{&F~6yfIg(>Dp?!vXQN6QQ?pvio(m9CI~`= zzDDRPKwlz#oue-gec1vlwha|>^Jsgy`QX0o3nANRl8_m$w`-i6>V{on2F_YW)I zPzJmo_Pp)A=J~egsPs+w9VsY&S@@Ok7X5P`7YBX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?a z8#`7}Tv@ucsRuy z0gXzQcCYK|b6wkgz7cX|%k$l{g^!^_mheKMdPr2%)2GEEr-goTSqPpC{AJH+9?&gg0 zm9Z(*Ha0U&qiXBQS6S^twrK-cX<}9X{m~JlBK-PUt|C-8qZwg3jC809xy(=8Tw0h- z(ZKV`lri+&eB6#nyGpXPuh72Z(yp)aBz*?$a`H;z=G>BYimo|1)!Kuw$J^m(s40|O zzMf1iCaIvKZn9F%*G$G()~J}A@>ZqEi8m@zqawRL>|-VVmXS-=q*YaaV~h5AbjPii z%R7hX7@H_KLqmo5_FcdF)M1%hK|&SKCh@JPKb4LaMRLHY`N5U zdM!RC!D@Aj_2_HHsAOq&-WZv5=!sV7Bruz(&F3o(5V3Isq&vBF@;OCaI4#!0cCX6H zIlGN?jBy>l$1S~^GpLq<+D%(aO4y)_O`-AY@?f#yjFW)=eWmbX$#hkx+f; zMMZs$GD>t}%A36%8 zi?_eZ=3BmY+u*^*6YPayhK-Etwm$XIL;Vj=DC+U!;_iF{zcK047+dnANMpQb2pIh| zCgqr0wyODNU(Gq9vBGd88s^q^_|u4u6E|J0i5zqN|8ME<|2>-{d}s{)A*wejz^2j+0aQ`PVAzv#(#fwljT6zrKAkJiR&* z&rL>S6Vb@T_*66&pNvLx6LCEn9nVFl@{{2_y;z-InVO8oW0BZOKD?4!%|;`U_(XU$ z8fTAuxm2!g>=g2P_FTELmNa*_rkf<1-~azepg-t<0|Y<-1V8`;KmY_l00ck)1V8`; PK;VE8P~?FCn-};$cuB#` literal 0 HcmV?d00001 diff --git a/ihatemoney/conf-templates/ihatemoney.cfg.j2 b/ihatemoney/conf-templates/ihatemoney.cfg.j2 index ba4793da..72e91973 100644 --- a/ihatemoney/conf-templates/ihatemoney.cfg.j2 +++ b/ihatemoney/conf-templates/ihatemoney.cfg.j2 @@ -8,7 +8,7 @@ DEBUG = False # The database URI, reprensenting the type of database and how to connect to it. # Enter an absolute path here. -SQLALCHEMY_DATABASE_URI = 'sqlite:////var/lib/ihatemoney/ihatemoney.sqlite' +SQLALCHEMY_DATABASE_URI = 'sqlite:///var/lib/ihatemoney/ihatemoney.sqlite' SQLACHEMY_ECHO = DEBUG # Will likely become the default value in flask-sqlalchemy >=3 ; could be removed diff --git a/ihatemoney/default_settings.py b/ihatemoney/default_settings.py index 48112afb..b477462e 100644 --- a/ihatemoney/default_settings.py +++ b/ihatemoney/default_settings.py @@ -1,6 +1,6 @@ # Verbose and documented settings are in conf-templates/ihatemoney.cfg.j2 DEBUG = SQLACHEMY_ECHO = False -SQLALCHEMY_DATABASE_URI = "sqlite:////tmp/ihatemoney.db" +SQLALCHEMY_DATABASE_URI = "sqlite:///C:/Users/sylvie c/Documents/GitHub/ihatemoney/ihatemoney.db" SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = "tralala" MAIL_DEFAULT_SENDER = "Budget manager " diff --git a/ihatemoney/models.py b/ihatemoney/models.py index c591b85b..4228edea 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -133,7 +133,7 @@ class Project(db.Model): should_receive[bill.payer.id] += bill.converted_amount for ower in bill.owers: should_pay[ower.id] += ( - ower.weight * bill.converted_amount / total_weight + ower.weight * bill.converted_amount / total_weight ) if bill.bill_type == BillType.REIMBURSEMENT: @@ -264,6 +264,51 @@ class Project(db.Model): .order_by(Bill.id.desc()) ) + @staticmethod + def filter_by_date(query, start_date=None, end_date=None): + if start_date and end_date: + return query.filter(Bill.date.between(start_date, end_date)) + else: + return query + + @staticmethod + def filter_by_paid_by(query, paid_by=None): + if paid_by: + return query.filter(Bill.payer.has(paid_by)) + else: + return query + + @staticmethod + def filter_by_for_what(query, for_what=None): + if for_what: + return query.filter(Bill.what.has(for_what)) + else: + return query + + @staticmethod + def filter_by_for_whom(query, for_whom=None): + if for_whom: + return query.filter(Bill.payed_for.has(for_whom)) + else: + return query + + @staticmethod + def filter_by_how_much(query, how_much=None): + if how_much is not None: + return query.filter(Bill.amount.has(how_much)) + else: + return query + + @staticmethod + def apply_filters(self, query, **kwargs): + filtered_query = query + filtered_query = self.filter_by_date(filtered_query, kwargs.get('start_date'), kwargs.get('end_date')) + filtered_query = self.filter_by_paid_by(filtered_query, kwargs.get('paid_by')) + filtered_query = self.filter_by_for_what(filtered_query, kwargs.get('for_what')) + filtered_query = self.filter_by_for_whom(filtered_query, kwargs.get('for_whom')) + filtered_query = self.filter_by_how_much(filtered_query, kwargs.get('how_much')) + return filtered_query + def get_bill_weights(self): """ Return all bills for this project, along with the sum of weight for each bill. @@ -285,6 +330,28 @@ class Project(db.Model): """Ordered version of get_bill_weights""" return self.order_bills(self.get_bill_weights()) + def get_filtered_bill_weights_ordered(self, start_date=None, end_date=None, paid_by=None, for_what=None, + for_whom=None, + how_much=None): + query = self.get_bill_weights_ordered() + + if start_date and end_date: + query = self.filter_by_date(query, start_date, end_date) + + if paid_by: + query = self.filter_by_paid_by(query, paid_by) + + if for_what: + query = self.filter_by_for_what(query, for_what) + + if for_whom: + query = self.filter_by_for_whom(query, for_whom) + + if how_much is not None: + query = self.filter_by_how_much(query, how_much) + + return query + def get_member_bills(self, member_id): """Return the list of bills related to a specific member""" return ( @@ -697,16 +764,16 @@ class Bill(db.Model): currency_helper = CurrencyConverter() def __init__( - self, - amount: float, - date: datetime.datetime = None, - external_link: str = "", - original_currency: str = "", - owers: list = [], - payer_id: int = None, - project_default_currency: str = "", - what: str = "", - bill_type: str = "Expense", + self, + amount: float, + date: datetime.datetime = None, + external_link: str = "", + original_currency: str = "", + owers: list = [], + payer_id: int = None, + project_default_currency: str = "", + what: str = "", + bill_type: str = "Expense", ): super().__init__() self.amount = amount diff --git a/ihatemoney/templates/list_bills.html b/ihatemoney/templates/list_bills.html index 79e25262..f151bc86 100644 --- a/ihatemoney/templates/list_bills.html +++ b/ihatemoney/templates/list_bills.html @@ -105,6 +105,43 @@
  • {{ _("Older bills") }} »
  • {% endif %} +
    + {{ csrf_form.csrf_token }} + +
    + + +
    + +
    + + +
    + +
    + + +
    + +
    + + +
    + +
    + + +
    + +
    + + +
    + + +
    + + {{ static_include("images/plus.svg") | safe }} diff --git a/ihatemoney/web.py b/ihatemoney/web.py index 0d0bdd20..5005e43c 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,53 @@ 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.get('start_date', None) + end_date = request.form.get('end_date', None) + paid_by = request.form.get('paid_by', None) + for_what = request.form.get('for_what', None) + for_whom = request.form.get('for_whom', None) + how_much = request.form.get('how_much', None) + + weighted_bills = g.project.get_filtered_bill_weights_ordered( + start_date=start_date, + end_date=end_date, + paid_by=paid_by, + for_what=for_what, + for_whom=for_whom, + how_much=how_much + ).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, + paid_by=paid_by, + for_what=for_what, + for_whom=for_whom, + how_much=how_much + ) - 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"])