2011-07-23 18:45:40 +02:00
|
|
|
from flaskext.wtf import *
|
2011-07-31 00:41:28 +02:00
|
|
|
from models import Project, Person, Bill
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-07-26 16:03:00 +02:00
|
|
|
class ProjectForm(Form):
|
2011-07-23 18:45:40 +02:00
|
|
|
name = TextField("Project name", validators=[Required()])
|
|
|
|
id = TextField("Project identifier", validators=[Required()])
|
|
|
|
password = PasswordField("Password", validators=[Required()])
|
|
|
|
contact_email = TextField("Email", validators=[Required(), Email()])
|
2011-08-05 16:12:02 +02:00
|
|
|
submit = SubmitField("Create the project")
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-07-23 19:11:24 +02:00
|
|
|
def save(self):
|
|
|
|
"""Create a new project with the information given by this form.
|
|
|
|
|
|
|
|
Returns the created instance
|
|
|
|
"""
|
|
|
|
project = Project(name=self.name.data, id=self.id.data,
|
|
|
|
password=self.password.data,
|
|
|
|
contact_email=self.contact_email.data)
|
|
|
|
return project
|
|
|
|
|
2011-07-23 18:45:40 +02:00
|
|
|
|
|
|
|
class AuthenticationForm(Form):
|
2011-07-29 15:44:15 +02:00
|
|
|
id = TextField("Project identifier", validators=[Required()])
|
2011-07-23 18:45:40 +02:00
|
|
|
password = TextField("Password", validators=[Required()])
|
|
|
|
submit = SubmitField("Get in")
|
|
|
|
|
|
|
|
|
|
|
|
class BillForm(Form):
|
|
|
|
what = TextField("What?", validators=[Required()])
|
|
|
|
payer = SelectField("Payer", validators=[Required()])
|
|
|
|
amount = DecimalField("Amount payed", validators=[Required()])
|
|
|
|
payed_for = SelectMultipleField("Who has to pay for this?",
|
|
|
|
validators=[Required()])
|
|
|
|
submit = SubmitField("Add the bill")
|
|
|
|
|
2011-07-31 00:41:28 +02:00
|
|
|
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
|
|
|
|
|
2011-07-23 20:36:13 +02:00
|
|
|
|
|
|
|
class MemberForm(Form):
|
|
|
|
def __init__(self, project, *args, **kwargs):
|
|
|
|
super(MemberForm, self).__init__(*args, **kwargs)
|
|
|
|
self.project = project
|
|
|
|
|
|
|
|
name = TextField("Name", validators=[Required()])
|
|
|
|
submit = SubmitField("Add a member")
|
|
|
|
|
|
|
|
def validate_name(form, field):
|
2011-07-30 01:51:13 +02:00
|
|
|
if Person.query.filter(Person.name == field.data)\
|
|
|
|
.filter(Person.project == form.project).all():
|
2011-07-23 20:36:13 +02:00
|
|
|
raise ValidationError("This project already have this member")
|
2011-07-30 15:46:53 +02:00
|
|
|
|
2011-07-31 00:41:28 +02:00
|
|
|
|
2011-07-30 15:46:53 +02:00
|
|
|
class InviteForm(Form):
|
|
|
|
emails = TextAreaField("People to notify")
|
|
|
|
submit = SubmitField("Send invites")
|
|
|
|
|
|
|
|
def validate_emails(form, field):
|
|
|
|
validator = Email()
|
|
|
|
for email in [email.strip() for email in form.emails.data.split(",")]:
|
|
|
|
if not validator.regex.match(email):
|
|
|
|
raise ValidationError("The email %s is not valid" % email)
|