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

Default owers when adding a bill #1222

Merged
merged 2 commits into from
Oct 3, 2023
Merged
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
27 changes: 27 additions & 0 deletions ihatemoney/tests/budget_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2100,3 +2100,30 @@ def test_remember_payer_per_project(self):
session["last_selected_payer_per_project"]["tartiflette"]
== members_ids_tartif[2]
)

def test_remember_payed_for(self):
"""
Tests that the last ower is remembered
"""
self.post_project("raclette")
self.client.post("/raclette/members/add", data={"name": "zorglub"})
self.client.post("/raclette/members/add", data={"name": "fred"})
self.client.post("/raclette/members/add", data={"name": "pipistrelle"})
members_ids = [m.id for m in self.get_project("raclette").members]
# create a bill
self.client.post(
"/raclette/add",
data={
"date": "2011-08-10",
"what": "fromage à raclette",
"payer": members_ids[1],
"payed_for": members_ids[1:],
"amount": "25",
},
)

with self.client as c:
c.post("/authenticate", data={"id": "raclette", "password": "raclette"})
assert isinstance(session["last_selected_payed_for"], dict)
assert "raclette" in session["last_selected_payed_for"]
assert session["last_selected_payed_for"]["raclette"] == members_ids[1:]
12 changes: 10 additions & 2 deletions ihatemoney/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def list_bills():
bill_form = get_billform_for(g.project)
# Used for CSRF validation
csrf_form = EmptyForm()
# set the last selected payer as default choice if exists
# set the last selected payer and last selected owers as default choice if they exist
if "last_selected_payer_per_project" in session:
if g.project.id in session["last_selected_payer_per_project"]:
bill_form.payer.data = session["last_selected_payer_per_project"][
Expand All @@ -655,6 +655,11 @@ def list_bills():
else:
if "last_selected_payer" in session:
bill_form.payer.data = session["last_selected_payer"]
if (
"last_selected_payed_for" in session
and g.project.id in session["last_selected_payed_for"]
):
bill_form.payed_for.data = session["last_selected_payed_for"][g.project.id]

# Each item will be a (weight_sum, Bill) tuple.
# TODO: improve this awkward result using column_property:
Expand Down Expand Up @@ -758,10 +763,13 @@ def add_bill():
form = get_billform_for(g.project)
if request.method == "POST":
if form.validate():
# save last selected payer in session
# save last selected payer and last selected owers in session
if "last_selected_payer_per_project" not in session:
session["last_selected_payer_per_project"] = {}
session["last_selected_payer_per_project"][g.project.id] = form.payer.data
if "last_selected_payed_for" not in session:
session["last_selected_payed_for"] = {}
session["last_selected_payed_for"][g.project.id] = form.payed_for.data
session.update()

db.session.add(form.export(g.project))
Expand Down