diff --git a/budget/forms.py b/budget/forms.py
index a81d8ca..4699531 100644
--- a/budget/forms.py
+++ b/budget/forms.py
@@ -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):
diff --git a/budget/templates/edit_bill.html b/budget/templates/edit_bill.html
new file mode 100644
index 0000000..c78eb09
--- /dev/null
+++ b/budget/templates/edit_bill.html
@@ -0,0 +1,15 @@
+{% extends "layout.html" %}
+
+{% block top_menu %}
+Back to the list
+{% endblock %}
+
+{% block content %}
+
Edit a bill
+
+
+
+
+{% endblock %}
diff --git a/budget/templates/list_bills.html b/budget/templates/list_bills.html
index 76a7701..569e30c 100644
--- a/budget/templates/list_bills.html
+++ b/budget/templates/list_bills.html
@@ -38,7 +38,8 @@
{{ bill.what }} |
{% for ower in bill.owers %}{{ ower.name }} {% endfor %} |
{{ bill.amount }} ({{ bill.pay_each() }} each) |
- delete |
+ edit
+ delete |
{% endfor %}
diff --git a/budget/utils.py b/budget/utils.py
index ed283cf..c4b1e75 100644
--- a/budget/utils.py
+++ b/budget/utils.py
@@ -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):
diff --git a/budget/web.py b/budget/web.py
index 24a034b..601ee81 100644
--- a/budget/web.py
+++ b/budget/web.py
@@ -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("//delete/")
+@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("//edit/", 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("//compute")
@requires_auth
def compute_bills(project):
@@ -196,17 +226,6 @@ def reset_bills(project):
return redirect(url_for('list_bills'))
-@app.route("//delete/")
-@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)