Adding a bill is now working properly

This commit is contained in:
Alexis Metaireau 2011-07-31 00:41:28 +02:00
parent 3bbc3343a2
commit 2df6e11f05
8 changed files with 29 additions and 17 deletions

View file

@ -1,7 +1,6 @@
from flaskext.wtf import * from flaskext.wtf import *
from models import Project, Person from models import Project, Person, Bill
# define forms
class ProjectForm(Form): class ProjectForm(Form):
name = TextField("Project name", validators=[Required()]) name = TextField("Project name", validators=[Required()])
id = TextField("Project identifier", validators=[Required()]) id = TextField("Project identifier", validators=[Required()])
@ -34,6 +33,15 @@ class BillForm(Form):
validators=[Required()]) validators=[Required()])
submit = SubmitField("Add the bill") submit = SubmitField("Add the bill")
def save(self):
bill = Bill(payer_id=self.payer.data, amount=self.amount.data,
what=self.what.data)
# set the owers
for ower in self.payed_for.data:
bill.owers.append(Person.query.get(ower))
return bill
class MemberForm(Form): class MemberForm(Form):
def __init__(self, project, *args, **kwargs): def __init__(self, project, *args, **kwargs):
@ -48,6 +56,7 @@ class MemberForm(Form):
.filter(Person.project == form.project).all(): .filter(Person.project == form.project).all():
raise ValidationError("This project already have this member") raise ValidationError("This project already have this member")
class InviteForm(Form): class InviteForm(Form):
emails = TextAreaField("People to notify") emails = TextAreaField("People to notify")
submit = SubmitField("Send invites") submit = SubmitField("Send invites")

View file

@ -24,6 +24,9 @@ class Person(db.Model):
name = db.Column(db.UnicodeText) name = db.Column(db.UnicodeText)
# activated = db.Column(db.Boolean, default=True) # activated = db.Column(db.Boolean, default=True)
def __str__(self):
return self.name
def __repr__(self): def __repr__(self):
return "<Person %s for project %s>" % (self.name, self.project.name) return "<Person %s for project %s>" % (self.name, self.project.name)

View file

@ -37,7 +37,9 @@
float: right; float: right;
} }
#topmenu ul li{ #topmenu ul li{
float: right;
list-style-type: none; list-style-type: none;
margin-left: 10px;
} }
/** links **/ /** links **/

View file

@ -9,7 +9,13 @@
{% if form.errors %} {% if form.errors %}
<p class=error><strong>Your form contains errors.</strong></p> <p class=error><strong>Your form contains errors.</strong></p>
<ul>{% for error in form.errors %}<li>{{ error }}</li>{% endfor %}</ul> <ul>
{% for field, errors in form.errors.items() %}
{% for error in errors %}
<li>{{ field }} : {{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
{% endif %} {% endif %}
<form action="{{ url_for('add_bill', project_id=project.id) }}" method=post class="container span-24 add-bill"> <form action="{{ url_for('add_bill', project_id=project.id) }}" method=post class="container span-24 add-bill">

View file

@ -9,11 +9,11 @@
<div class="container" class="span-24"> <div class="container" class="span-24">
<div id="title"> <div id="title">
<a href="/"><h1>Account manager ! <span class="small">Manage your shared expenses.</span></h1></a> <a href="/"><h1>Account manager ! <span class="small">Manage your shared expenses.</span></h1></a>
<hr>
<div class="fright" id="topmenu"> <div class="fright" id="topmenu">
{% block top_menu %}{% endblock %} {% block top_menu %}{% endblock %}
</div> </div>
</div> </div>
<hr>
{% for message in get_flashed_messages() %} {% for message in get_flashed_messages() %}
<div class=info>{{ message }}</div> <div class=info>{{ message }}</div>
{% endfor %} {% endfor %}

View file

@ -2,8 +2,7 @@
{% block top_menu %} {% block top_menu %}
<ul> <ul>
<li><a href="{{ url_for('add_bill', project_id=project.id) }}">Add a bill</a></li> <li><a class="awesome button" href="{{ url_for('add_bill', project_id=project.id) }}">Add a bill</a></li>
<li><a href="{{ url_for('add_member', project_id=project.id) }}">Add a member</a></li>
</ul> </ul>
{% endblock %} {% endblock %}

View file

@ -7,7 +7,7 @@ from forms import BillForm
def get_billform_for(project_id): def get_billform_for(project_id):
"""Return an instance of BillForm configured for a particular project.""" """Return an instance of BillForm configured for a particular project."""
form = BillForm() form = BillForm()
payers = [(m.id, m.name) for m in Project.query.get(project_id).members] payers = [(str(m.id), m.name) for m in Project.query.get(project_id).members]
form.payed_for.choices = form.payer.choices = payers form.payed_for.choices = form.payer.choices = payers
return form return form

View file

@ -123,18 +123,11 @@ def add_bill(project):
form = get_billform_for(project.id) form = get_billform_for(project.id)
if request.method == 'POST': if request.method == 'POST':
if form.validate(): if form.validate():
bill = Bill() db.session.add(form.save())
form.populate_obj(bill)
for ower in form.payed_for.data:
ower = BillOwer(name=ower)
db.session.add(ower)
bill.owers.append(ower)
db.session.add(bill)
db.session.commit() db.session.commit()
flash("The bill have been added") flash("The bill have been added")
return redirect(url_for('list_bills')) return redirect(url_for('list_bills', project_id=project.id))
return render_template("add_bill.html", form=form, project=project) return render_template("add_bill.html", form=form, project=project)