Skip to content

Commit

Permalink
Adding filtering capabilities for bills
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyaNagunuri committed Jan 11, 2025
1 parent 19ecdb5 commit 4e99980
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 14 deletions.
18 changes: 18 additions & 0 deletions ihatemoney/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ def order_bills(query):
.order_by(Bill.creation_date.desc())
.order_by(Bill.id.desc())
)

@staticmethod
def filter(query, start_date, end_date, payer, owers, bill_name):
if start_date:
query = query.filter(Bill.date >= start_date)
if end_date:
query = query.filter(Bill.date <= end_date)
if payer:
query = query.filter(Bill.payer_id == payer)
if owers:
query = query.filter(Bill.owers.any(Person.id.in_(owers)))
if bill_name:
query = query.filter(Bill.what.ilike(f"%{bill_name}%"))
return query

def get_bill_weights(self):
"""
Expand All @@ -284,6 +298,10 @@ def get_bill_weights(self):
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, payer, owers, bill_name):
return self.filter(self.get_bill_weights_ordered(), start_date=start_date, end_date=end_date, payer=payer, owers=owers, bill_name=bill_name)


def get_member_bills(self, member_id):
"""Return the list of bills related to a specific member"""
Expand Down
58 changes: 58 additions & 0 deletions ihatemoney/templates/list_bills.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,64 @@ <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 %}

<div class="d-flex flex-wrap w-100 pt-2 mt-2" id="bill-toolbar">
<button class="btn btn-secondary btn-sm mb-2" type="button" data-toggle="collapse" data-target="#filter-options" aria-expanded="false" aria-controls="filter-options">
Filter Bills
</button>

<div class="collapse w-100" id="filter-options">
<form action="{{ url_for('.list_bills') }}" method="post" class="p-2 border rounded bg-light">
{{ csrf_form.csrf_token }}

<div class="form-row">
<div class="form-group col-md-3">
<label for="start_date" class="small">Start Date:</label>
<input type="date" id="start_date" name="start_date" class="form-control form-control-sm" value="{{ start_date if start_date else '' }}">
</div>

<div class="form-group col-md-3">
<label for="end_date" class="small">End Date:</label>
<input type="date" id="end_date" name="end_date" class="form-control form-control-sm" value="{{ end_date if end_date else '' }}">
</div>

<div class="form-group col-md-3">
<label for="payer" class="small">Who Paid:</label>
<select id="payer" name="payer" class="form-control form-control-sm">
<option value="">-- Select --</option>
{% for member in g.project.members %}
<option value="{{ member.id }}" {% if selected_payer == member.id %}selected{% endif %}>
{{ member.name }}
</option>
{% endfor %}
</select>
</div>

<div class="form-group col-md-3">
<label for="owers" class="small">For Whom:</label>
<select id="owers" name="owers" multiple class="form-control form-control-sm">
{% for member in g.project.members %}
<option value="{{ member.id }}" {% if selected_owers and member.id in selected_owers %}selected{% endif %}>
{{ member.name }}
</option>
{% endfor %}
</select>
</div>

<div class="form-group col-md-3">
<label for="bill_name" class="small">For What?</label>
<input type="text" id="bill_name" name="bill_name" class="form-control form-control-sm" value="{{ bill_name if bill_name else '' }}" placeholder="Enter bill name">
</div>

</div>

<div class="text-right">
<button type="submit" class="btn btn-primary btn-sm">Apply Filters</button>
</div>
</form>
</div>
</div>

<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
51 changes: 37 additions & 14 deletions ihatemoney/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,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 @@ -673,19 +673,42 @@ 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
)

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 == "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']
payer = request.form.get('payer', type=int)
owers = request.form.getlist('owers', type=int)
bill_name = request.form.get('bill_name', '').strip()
weighted_bills = g.project.get_filtered_bill_weights_ordered(start_date, end_date, payer, owers, bill_name).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,
selected_payer=payer,
selected_owers=owers,
bill_name=bill_name,
)


@main.route("/<project_id>/members/add", methods=["GET", "POST"])
Expand Down

0 comments on commit 4e99980

Please sign in to comment.