1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/ihatemoney_ynh.git synced 2024-09-03 19:26:15 +02:00

use "member.id" instead of "member"

This commit is contained in:
A.Avenel 2013-04-07 20:25:25 +02:00
parent b410e48851
commit 1fa0cff180
5 changed files with 29 additions and 31 deletions

View file

@ -45,21 +45,19 @@ class Project(db.Model):
for person in self.members: for person in self.members:
balance = should_receive[person] - should_pay[person] balance = should_receive[person] - should_pay[person]
balances[person] = round(balance, 2) balances[person.id] = round(balance, 2)
return balances return balances
def settle_bill(self): def settle_bills(self):
"""Return a list of transactions that could be made to settle the bill""" """Return a list of transactions that could be made to settle the bill"""
balances = self.balance credits, debts, transactions = [],[],[]
credits, debts = list(), list()
transactions = list()
# Create lists of credits and debts # Create lists of credits and debts
for person in balances.keys(): for person in self.members:
if balances[person] > 0: if self.balance[person.id] > 0:
credits.append({"person": person, "balance": balances[person]}) credits.append({"person": person, "balance": self.balance[person.id]})
elif balances[person] < 0: elif self.balance[person.id] < 0:
debts.append({"person": person, "balance": -balances[person]}) debts.append({"person": person, "balance": -self.balance[person.id]})
# Try and find exact matches # Try and find exact matches
for credit in credits: for credit in credits:
match = self.exactmatch(credit["balance"], debts) match = self.exactmatch(credit["balance"], debts)
@ -83,7 +81,7 @@ class Project(db.Model):
def exactmatch(self, credit, debts): def exactmatch(self, credit, debts):
"""Recursively try and find subsets of 'debts' whose sum is equal to credit""" """Recursively try and find subsets of 'debts' whose sum is equal to credit"""
if not debts: if not debts:
return [] return None
if debts[0]["balance"] > credit: if debts[0]["balance"] > credit:
return self.exactmatch(credit, debts[1:]) return self.exactmatch(credit, debts[1:])
elif debts[0]["balance"] == credit: elif debts[0]["balance"] == credit:

View file

@ -61,20 +61,20 @@
<div id="table_overflow"> <div id="table_overflow">
<table class="balance table"> <table class="balance table">
{% set balance = g.project.balance %} {% set balance = g.project.balance %}
{% for member in g.project.members | sort(attribute='name') if member.activated or balance[member] != 0 %} {% for member in g.project.members | sort(attribute='name') if member.activated or balance[member.id] != 0 %}
<tr id="bal-member-{{ member }}" action={% if member.activated %}delete{% else %}reactivate{% endif %}> <tr id="bal-member-{{ member.id }}" action={% if member.activated %}delete{% else %}reactivate{% endif %}>
<td class="balance-name">{{ member.name }}</td> <td class="balance-name">{{ member.name }}</td>
{% if member.activated %} {% if member.activated %}
<td> <td>
<form class="action delete" action="{{ url_for(".remove_member", member_id=member) }}" method="POST"> <form class="action delete" action="{{ url_for(".remove_member", member_id=member.id) }}" method="POST">
<button type="submit">{{ _("delete") }}</button></form></td> <button type="submit">{{ _("delete") }}</button></form></td>
{% else %} {% else %}
<td> <td>
<form class="action reactivate" action="{{ url_for(".reactivate", member_id=member) }}" method="POST"> <form class="action reactivate" action="{{ url_for(".reactivate", member_id=member.id) }}" method="POST">
<button type="submit">{{ _("reactivate") }}</button></form></td> <button type="submit">{{ _("reactivate") }}</button></form></td>
{% endif %} {% endif %}
<td class="balance-value {% if balance[member] > 0 %}positive{% elif balance[member] < 0 %}negative{% endif %}"> <td class="balance-value {% if balance[member] > 0 %}positive{% elif balance[member.id] < 0 %}negative{% endif %}">
{% if balance[member] > 0 %}+{% endif %}{{ balance[member] }} {% if balance[member.id] > 0 %}+{% endif %}{{ balance[member.id] }}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View file

@ -5,7 +5,7 @@
{% endblock %} {% endblock %}
{% block navbar %} {% block navbar %}
<li><a href="{{ url_for(".list_bills") }}">{{ _("Bills") }}</a></li> <li><a href="{{ url_for(".list_bills") }}">{{ _("Bills") }}</a></li>
<li class="active"><a href="{{ url_for(".settle_bill") }}">{{ _("Settle") }}</a></li> <li class="active"><a href="{{ url_for(".settle_bills") }}">{{ _("Settle") }}</a></li>
{% endblock %} {% endblock %}
{% block sidebar %} {% block sidebar %}
@ -14,11 +14,11 @@
<div id="table_overflow"> <div id="table_overflow">
<table class="balance table"> <table class="balance table">
{% set balance = g.project.balance %} {% set balance = g.project.balance %}
{% for member in g.project.members | sort(attribute='name') if member.activated or balance[member] != 0 %} {% for member in g.project.members | sort(attribute='name') if member.activated or balance[member.id] != 0 %}
<tr id="bal-member-{{ member }}" action={% if member.activated %}delete{% else %}reactivate{% endif %}> <tr id="bal-member-{{ member.id }}" action={% if member.activated %}delete{% else %}reactivate{% endif %}>
<td class="balance-name">{{ member.name }}</td> <td class="balance-name">{{ member.name }}</td>
<td class="balance-value {% if balance[member] > 0 %}positive{% elif balance[member] < 0 %}negative{% endif %}"> <td class="balance-value {% if balance[member.id] > 0 %}positive{% elif balance[member.id] < 0 %}negative{% endif %}">
{% if balance[member] > 0 %}+{% endif %}{{ balance[member] }} {% if balance[member.id] > 0 %}+{% endif %}{{ balance[member.id] }}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View file

@ -436,9 +436,9 @@ class BudgetTestCase(TestCase):
balance = models.Project.query.get("raclette").balance balance = models.Project.query.get("raclette").balance
result = {} result = {}
result[models.Project.query.get("raclette").members[0]] = 8.12 result[models.Project.query.get("raclette").members[0].id] = 8.12
result[models.Project.query.get("raclette").members[1]] = 0.0 result[models.Project.query.get("raclette").members[1].id] = 0.0
result[models.Project.query.get("raclette").members[2]] = -8.12 result[models.Project.query.get("raclette").members[2].id] = -8.12
self.assertDictEqual(balance, result) self.assertDictEqual(balance, result)
def test_edit_project(self): def test_edit_project(self):
@ -472,7 +472,7 @@ class BudgetTestCase(TestCase):
def test_settle_page(self): def test_settle_page(self):
self.post_project("raclette") self.post_project("raclette")
response = self.app.get("/raclette/settle_bill") response = self.app.get("/raclette/settle_bills")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_settle(self): def test_settle(self):
@ -510,7 +510,7 @@ class BudgetTestCase(TestCase):
'amount': '10', 'amount': '10',
}) })
project = models.Project.query.get('raclette') project = models.Project.query.get('raclette')
transactions = project.settle_bill() transactions = project.settle_bills()
members = defaultdict(int) members = defaultdict(int)
#We should have the same values between transactions and project balances #We should have the same values between transactions and project balances
for t in transactions: for t in transactions:
@ -518,7 +518,7 @@ class BudgetTestCase(TestCase):
members[t['payer']]+=t['amount'] members[t['payer']]+=t['amount']
balance = models.Project.query.get("raclette").balance balance = models.Project.query.get("raclette").balance
for m, a in members.items(): for m, a in members.items():
self.assertEqual(a, balance[m]) self.assertEqual(a, balance[m.id])
return return

View file

@ -383,11 +383,11 @@ def change_lang(lang):
return redirect(request.headers.get('Referer') or url_for('.home')) return redirect(request.headers.get('Referer') or url_for('.home'))
@main.route("/<project_id>/settle_bill") @main.route("/<project_id>/settle_bills")
def settle_bill(): def settle_bill():
"""Compute the sum each one have to pay to each other and display it""" """Compute the sum each one have to pay to each other and display it"""
bills = g.project.settle_bill() bills = g.project.settle_bills()
return render_template("settle_bill.html", bills=bills) return render_template("settle_bills.html", bills=bills)
@main.route("/<project_id>/archives/create", methods=["GET", "POST"]) @main.route("/<project_id>/archives/create", methods=["GET", "POST"])