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

It is now possible to edit existing bills.

This commit is contained in:
Frédéric Sureau 2011-08-10 12:59:30 +02:00
parent af2ca220a7
commit 922bf769f9
5 changed files with 68 additions and 27 deletions

View file

@ -50,17 +50,28 @@ class BillForm(Form):
amount = DecimalField("Amount payed", validators=[Required()])
payed_for = SelectMultipleField("Who has to pay for this?",
validators=[Required()], widget=select_multi_checkbox)
submit = SubmitField("Add the bill")
submit = SubmitField("Send the bill")
def save(self):
bill = Bill(payer_id=self.payer.data, amount=self.amount.data,
what=self.what.data, date=self.date.data)
# set the owers
for ower in self.payed_for.data:
bill.owers.append(Person.query.get(ower))
def save(self, bill):
bill.payer_id=self.payer.data
bill.amount=self.amount.data
bill.what=self.what.data
bill.date=self.date.data
bill.owers = [Person.query.get(ower) for ower in self.payed_for.data]
print self.payed_for.data
return bill
def fill(self, bill):
self.payer.data = bill.payer_id
self.amount.data = bill.amount
self.what.data = bill.what
self.date.data = bill.date
self.payed_for.data = [str(ower.id) for ower in bill.owers]
def set_default(self):
self.payed_for.data = self.payed_for.default
class MemberForm(Form):
def __init__(self, project, *args, **kwargs):

View file

@ -0,0 +1,15 @@
{% extends "layout.html" %}
{% block top_menu %}
<a href="{{ url_for('list_bills', project_id=project.id) }}">Back to the list</a>
{% endblock %}
{% block content %}
<h2>Edit a bill</h2>
<div class="container span-24 add-bill uniForm" style="width: 400px">
<form action="{{ url_for('edit_bill', project_id=project.id, bill_id=bill_id) }}" method="post" class=uniForm">
{{ forms.add_bill(form) }}
</form>
</div>
{% endblock %}

View file

@ -38,7 +38,8 @@
<td>{{ bill.what }}</td>
<td>{% for ower in bill.owers %}{{ ower.name }} {% endfor %}</td>
<td>{{ bill.amount }} ({{ bill.pay_each() }} each)</td>
<td><a href="{{ url_for("delete_bill", bill_id=bill.id, project_id=project.id) }}">delete</a></td>
<td><a href="{{ url_for("edit_bill", bill_id=bill.id, project_id=project.id) }}">edit</a>
<a href="{{ url_for("delete_bill", bill_id=bill.id, project_id=project.id) }}">delete</a></td>
</tr>
{% endfor %}
</tbody>

View file

@ -6,14 +6,9 @@ from forms import BillForm
def get_billform_for(project):
"""Return an instance of BillForm configured for a particular project."""
payers = []
ids = []
for m in project.active_members:
payers.append( (str(m.id), m.name) )
ids.append( str(m.id) )
form = BillForm()
form.payed_for.choices = form.payer.choices = payers
form.payed_for.data = ids
form.payed_for.choices = form.payer.choices = [(str(m.id), m.name) for m in project.active_members]
form.payed_for.default = [ str(m.id) for m in project.active_members]
return form
def requires_auth(f):

View file

@ -166,15 +166,45 @@ def add_bill(project):
form = get_billform_for(project)
if request.method == 'POST':
if form.validate():
db.session.add(form.save())
bill = Bill()
db.session.add(form.save(bill))
db.session.commit()
flash("The bill has been added")
return redirect(url_for('list_bills', project_id=project.id))
form.set_default()
return render_template("add_bill.html", form=form, project=project)
@app.route("/<string:project_id>/delete/<int:bill_id>")
@requires_auth
def delete_bill(project, bill_id):
bill = Bill.query.get_or_404(bill_id)
db.session.delete(bill)
# FIXME Delete also billowers relations
db.session.commit()
flash("The bill has been deleted")
return redirect(url_for('list_bills', project_id=project.id))
@app.route("/<string:project_id>/edit/<int:bill_id>", methods=["GET", "POST"])
@requires_auth
def edit_bill(project, bill_id):
bill = Bill.query.get_or_404(bill_id)
form = get_billform_for(project)
if request.method == 'POST' and form.validate():
# FIXME Edit also billowers relations
form.save(bill)
db.session.commit()
flash("The bill has been modified")
return redirect(url_for('list_bills', project_id=project.id))
form.fill(bill)
return render_template("edit_bill.html", form=form, project=project, bill_id=bill_id)
@app.route("/<string:project_id>/compute")
@requires_auth
def compute_bills(project):
@ -196,17 +226,6 @@ def reset_bills(project):
return redirect(url_for('list_bills'))
@app.route("/<string:project_id>/delete/<int:bill_id>")
@requires_auth
def delete_bill(project, bill_id):
Bill.query.filter(Bill.id == bill_id).delete()
BillOwer.query.filter(BillOwer.bill_id == bill_id).delete()
db.session.commit()
flash("the bill was deleted")
return redirect(url_for('list_bills'))
def main():
app.run(host="0.0.0.0", debug=True)