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
|
- SECRET_KEY=tralala
|
||||||
- SESSION_COOKIE_SECURE=True
|
- SESSION_COOKIE_SECURE=True
|
||||||
- SHOW_ADMIN_EMAIL=True
|
- SHOW_ADMIN_EMAIL=True
|
||||||
- SQLALCHEMY_DATABASE_URI=sqlite:////database/ihatemoney.db
|
- SQLALCHEMY_DATABASE_URI=sqlite:///database/ihatemoney.db
|
||||||
- SQLALCHEMY_TRACK_MODIFICATIONS=False
|
- SQLALCHEMY_TRACK_MODIFICATIONS=False
|
||||||
- APPLICATION_ROOT=/
|
- APPLICATION_ROOT=/
|
||||||
- ENABLE_CAPTCHA=False
|
- ENABLE_CAPTCHA=False
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
|
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.
|
# The database URI, reprensenting the type of database and how to connect to it.
|
||||||
# Enter an absolute path here.
|
# 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
|
SQLACHEMY_ECHO = DEBUG
|
||||||
|
|
||||||
# Will likely become the default value in flask-sqlalchemy >=3 ; could be removed
|
# 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
|
# Verbose and documented settings are in conf-templates/ihatemoney.cfg.j2
|
||||||
DEBUG = SQLACHEMY_ECHO = False
|
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
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
SECRET_KEY = "tralala"
|
SECRET_KEY = "tralala"
|
||||||
MAIL_DEFAULT_SENDER = "Budget manager <admin@example.com>"
|
MAIL_DEFAULT_SENDER = "Budget manager <admin@example.com>"
|
||||||
|
|
|
@ -133,7 +133,7 @@ class Project(db.Model):
|
||||||
should_receive[bill.payer.id] += bill.converted_amount
|
should_receive[bill.payer.id] += bill.converted_amount
|
||||||
for ower in bill.owers:
|
for ower in bill.owers:
|
||||||
should_pay[ower.id] += (
|
should_pay[ower.id] += (
|
||||||
ower.weight * bill.converted_amount / total_weight
|
ower.weight * bill.converted_amount / total_weight
|
||||||
)
|
)
|
||||||
|
|
||||||
if bill.bill_type == BillType.REIMBURSEMENT:
|
if bill.bill_type == BillType.REIMBURSEMENT:
|
||||||
|
@ -264,6 +264,51 @@ class Project(db.Model):
|
||||||
.order_by(Bill.id.desc())
|
.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):
|
def get_bill_weights(self):
|
||||||
"""
|
"""
|
||||||
Return all bills for this project, along with the sum of weight for each bill.
|
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"""
|
"""Ordered version of get_bill_weights"""
|
||||||
return self.order_bills(self.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):
|
def get_member_bills(self, member_id):
|
||||||
"""Return the list of bills related to a specific member"""
|
"""Return the list of bills related to a specific member"""
|
||||||
return (
|
return (
|
||||||
|
@ -697,16 +764,16 @@ class Bill(db.Model):
|
||||||
currency_helper = CurrencyConverter()
|
currency_helper = CurrencyConverter()
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
amount: float,
|
amount: float,
|
||||||
date: datetime.datetime = None,
|
date: datetime.datetime = None,
|
||||||
external_link: str = "",
|
external_link: str = "",
|
||||||
original_currency: str = "",
|
original_currency: str = "",
|
||||||
owers: list = [],
|
owers: list = [],
|
||||||
payer_id: int = None,
|
payer_id: int = None,
|
||||||
project_default_currency: str = "",
|
project_default_currency: str = "",
|
||||||
what: str = "",
|
what: str = "",
|
||||||
bill_type: str = "Expense",
|
bill_type: str = "Expense",
|
||||||
):
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.amount = amount
|
self.amount = amount
|
||||||
|
|
|
@ -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>
|
<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>
|
</ul>
|
||||||
{% endif %}
|
{% 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 %}>
|
<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>
|
<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>
|
<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)
|
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():
|
def list_bills():
|
||||||
bill_form = get_billform_for(g.project)
|
bill_form = get_billform_for(g.project)
|
||||||
# Used for CSRF validation
|
# Used for CSRF validation
|
||||||
|
@ -666,19 +666,53 @@ def list_bills():
|
||||||
# Each item will be a (weight_sum, Bill) tuple.
|
# Each item will be a (weight_sum, Bill) tuple.
|
||||||
# TODO: improve this awkward result using column_property:
|
# TODO: improve this awkward result using column_property:
|
||||||
# https://docs.sqlalchemy.org/en/14/orm/mapped_sql_expr.html.
|
# https://docs.sqlalchemy.org/en/14/orm/mapped_sql_expr.html.
|
||||||
weighted_bills = g.project.get_bill_weights_ordered().paginate(
|
if request.method == "GET":
|
||||||
per_page=100, error_out=True
|
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("/<project_id>/members/add", methods=["GET", "POST"])
|
@main.route("/<project_id>/members/add", methods=["GET", "POST"])
|
||||||
|
|
Loading…
Reference in a new issue