Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a filter option to the Bills screen #1308

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ihatemoney/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 (
Expand Down
10 changes: 10 additions & 0 deletions ihatemoney/templates/list_bills.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ <h3 class="modal-title">{{ _('Add a bill') }}</h3>
<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") }} &raquo;</a></li>
</ul>
{% endif %}
<form action="{{ url_for(".list_bills") }}" method="post">
{{ csrf_form.csrf_token }}
<label for="start_date">Start Date:</label>
<input type="date" id="start_date" name="start_date" value="{{ start_date if start_date else '' }}">

<label for="end_date">End Date:</label>
<input type="date" id="end_date" name="end_date" value="{{ end_date if end_date else '' }}">

<input type="submit" value="Filter">
</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>
Expand Down
44 changes: 31 additions & 13 deletions ihatemoney/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("/<project_id>/members/add", methods=["GET", "POST"])
Expand Down
Loading