mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-04-28 17:32:38 +02:00
Adding filter option for the bills
This commit is contained in:
parent
996e68c7c0
commit
5e710fb7f9
8 changed files with 165 additions and 28 deletions
|
@ -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
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import sys
|
||||
|
||||
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
|
||||
|
||||
|
||||
|
|
BIN
ihatemoney.db
Normal file
BIN
ihatemoney.db
Normal file
Binary file not shown.
|
@ -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
|
||||
|
|
|
@ -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 <admin@example.com>"
|
||||
|
|
|
@ -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 (
|
||||
|
|
|
@ -105,6 +105,43 @@
|
|||
<li class="page-item {% if bills.page == bills.pages %}disabled{% endif %}"><a class="page-link" href="{{ url_for('main.list_bills', page=bills.next_num) }}">{{ _("Older bills") }} »</a></li>
|
||||
</ul>
|
||||
{% endif %}
|
||||
<form action="{{ url_for(".list_bills") }}" method="post">
|
||||
{{ csrf_form.csrf_token }}
|
||||
|
||||
<div>
|
||||
<label for="start_date">Start Date:</label>
|
||||
<input type="date" id="start_date" name="start_date" value="{{ start_date if start_date else '' }}">
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="end_date">End Date:</label>
|
||||
<input type="date" id="end_date" name="end_date" value="{{ end_date if end_date else '' }}">
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="paid_by">Paid By:</label>
|
||||
<input type="text" id="paid_by" name="paid_by" value="{{ paid_by if paid_by else '' }}">
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="for_what">For What:</label>
|
||||
<input type="text" id="for_what" name="for_what" value="{{ for_what if for_what else '' }}">
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="for_whom">For Whom:</label>
|
||||
<input type="text" id="for_whom" name="for_whom" value="{{ for_whom if for_whom else '' }}">
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="how_much">How Much:</label>
|
||||
<input type="number" id="how_much" name="how_much" value="{{ how_much if how_much else '' }}">
|
||||
</div>
|
||||
|
||||
<input type="submit" value="Enter">
|
||||
</form>
|
||||
|
||||
|
||||
<span id="new-bill" class="ml-auto pb-2" {% if not g.project.members %} data-toggle="tooltip" title="{{_('You should start by adding participants')}}" {% endif %}>
|
||||
<a href="{{ url_for('.add_bill') }}" class="btn btn-primary {% if not g.project.members %} disabled {% endif %}" data-toggle="modal" data-keyboard="true" data-target="#bill-form" autofocus>
|
||||
<i class="icon icon-white before-text">{{ static_include("images/plus.svg") | safe }}</i>
|
||||
|
|
|
@ -642,7 +642,7 @@ def invite():
|
|||
return render_template("send_invites.html", form=form, qrcode=qrcode_svg)
|
||||
|
||||
|
||||
@main.route("/<project_id>/")
|
||||
@main.route("/<project_id>/", methods=["GET", "POST"])
|
||||
def list_bills():
|
||||
bill_form = get_billform_for(g.project)
|
||||
# Used for CSRF validation
|
||||
|
@ -666,10 +666,10 @@ 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.
|
||||
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,
|
||||
|
@ -680,6 +680,40 @@ def list_bills():
|
|||
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
|
||||
)
|
||||
|
||||
|
||||
|
||||
@main.route("/<project_id>/members/add", methods=["GET", "POST"])
|
||||
def add_member():
|
||||
|
|
Loading…
Reference in a new issue