2011-07-23 18:45:40 +02:00
|
|
|
from flaskext.wtf import *
|
2011-08-10 00:20:16 +02:00
|
|
|
from wtforms.widgets import html_params
|
2011-09-13 22:58:53 +02:00
|
|
|
from models import Project, Person, Bill, db
|
2011-08-09 18:28:48 +02:00
|
|
|
from datetime import datetime
|
2011-09-11 05:25:42 +02:00
|
|
|
from jinja2 import Markup
|
|
|
|
from utils import slugify
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-08-10 00:20:16 +02:00
|
|
|
|
|
|
|
def select_multi_checkbox(field, ul_class='', **kwargs):
|
|
|
|
kwargs.setdefault('type', 'checkbox')
|
|
|
|
field_id = kwargs.pop('id', field.id)
|
2011-08-21 01:42:10 +02:00
|
|
|
html = [u'<ul %s>' % html_params(id=field_id, class_="inputs-list")]
|
2011-08-10 00:20:16 +02:00
|
|
|
for value, label, checked in field.iter_choices():
|
|
|
|
choice_id = u'%s-%s' % (field_id, value)
|
|
|
|
options = dict(kwargs, name=field.name, value=value, id=choice_id)
|
|
|
|
if checked:
|
|
|
|
options['checked'] = 'checked'
|
2011-08-21 01:42:10 +02:00
|
|
|
html.append(u'<li><label for="%s">%s<span>%s</span></label></li>' % (choice_id, '<input %s /> ' % html_params(**options), label))
|
2011-08-10 00:20:16 +02:00
|
|
|
html.append(u'</ul>')
|
|
|
|
return u''.join(html)
|
|
|
|
|
|
|
|
|
2011-09-11 05:25:42 +02:00
|
|
|
def get_billform_for(request, project, set_default=True):
|
|
|
|
"""Return an instance of BillForm configured for a particular project.
|
|
|
|
|
|
|
|
:set_default: if set to True, on GET methods (usually when we want to
|
|
|
|
display the default form, it will call set_default on it.
|
|
|
|
|
|
|
|
"""
|
|
|
|
form = BillForm()
|
|
|
|
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]
|
|
|
|
|
|
|
|
if set_default and request.method == "GET":
|
|
|
|
form.set_default()
|
|
|
|
return form
|
|
|
|
|
|
|
|
|
2011-09-14 22:03:18 +02:00
|
|
|
class EditProjectForm(Form):
|
2011-07-23 18:45:40 +02:00
|
|
|
name = TextField("Project name", validators=[Required()])
|
2011-09-18 23:38:12 +02:00
|
|
|
password = TextField("Private code", validators=[Required()])
|
2011-07-23 18:45:40 +02:00
|
|
|
contact_email = TextField("Email", validators=[Required(), Email()])
|
2011-09-14 22:03:18 +02:00
|
|
|
submit = SubmitField("Edit the project")
|
2011-08-10 17:06:32 +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-09-13 22:58:53 +02:00
|
|
|
def update(self, project):
|
|
|
|
"""Update the project with the information from the form"""
|
|
|
|
project.name = self.name.data
|
|
|
|
project.password = self.password.data
|
|
|
|
project.contact_email = self.contact_email.data
|
|
|
|
|
|
|
|
return project
|
|
|
|
|
2011-09-18 23:38:12 +02:00
|
|
|
|
2011-09-14 22:03:18 +02:00
|
|
|
class ProjectForm(EditProjectForm):
|
|
|
|
id = TextField("Project identifier", validators=[Required()])
|
2011-09-18 23:50:14 +02:00
|
|
|
password = PasswordField("Private code", validators=[Required()])
|
2011-08-05 16:12:02 +02:00
|
|
|
submit = SubmitField("Create the project")
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-08-10 17:06:32 +02:00
|
|
|
def validate_id(form, field):
|
2011-09-11 05:25:42 +02:00
|
|
|
form.id.data = slugify(field.data)
|
|
|
|
if Project.query.get(form.id.data):
|
|
|
|
raise ValidationError(Markup("""The project identifier is used
|
|
|
|
to log in and for the URL of the project.
|
|
|
|
<br />
|
|
|
|
We tried to generate an identifier for you but
|
2011-09-14 01:16:25 +02:00
|
|
|
a project with this identifier already exists.
|
2011-09-11 05:25:42 +02:00
|
|
|
<br />
|
|
|
|
Please create a new identifier you will be able
|
|
|
|
to remember.
|
|
|
|
"""))
|
2011-08-10 17:06:32 +02:00
|
|
|
|
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-09-11 05:25:42 +02:00
|
|
|
password = PasswordField("Private code", validators=[Required()])
|
2011-07-23 18:45:40 +02:00
|
|
|
submit = SubmitField("Get in")
|
|
|
|
|
|
|
|
|
|
|
|
class BillForm(Form):
|
2011-08-09 18:28:48 +02:00
|
|
|
date = DateField("Date", validators=[Required()], default=datetime.now)
|
2011-07-23 18:45:40 +02:00
|
|
|
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?",
|
2011-08-10 00:20:16 +02:00
|
|
|
validators=[Required()], widget=select_multi_checkbox)
|
2011-08-10 12:59:30 +02:00
|
|
|
submit = SubmitField("Send the bill")
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-10-08 13:22:18 +02:00
|
|
|
def save(self, bill, project):
|
2011-08-10 12:59:30 +02:00
|
|
|
bill.payer_id=self.payer.data
|
|
|
|
bill.amount=self.amount.data
|
|
|
|
bill.what=self.what.data
|
|
|
|
bill.date=self.date.data
|
2011-10-08 13:22:18 +02:00
|
|
|
bill.owers = [Person.query.get(ower, project)
|
|
|
|
for ower in self.payed_for.data]
|
2011-07-31 00:41:28 +02:00
|
|
|
|
|
|
|
return bill
|
|
|
|
|
2011-08-10 12:59:30 +02:00
|
|
|
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
|
|
|
|
|
2011-07-23 20:36:13 +02:00
|
|
|
|
|
|
|
class MemberForm(Form):
|
|
|
|
|
|
|
|
name = TextField("Name", validators=[Required()])
|
|
|
|
submit = SubmitField("Add a member")
|
|
|
|
|
2011-09-13 22:58:53 +02:00
|
|
|
def __init__(self, project, *args, **kwargs):
|
|
|
|
super(MemberForm, self).__init__(*args, **kwargs)
|
|
|
|
self.project = project
|
|
|
|
|
2011-07-23 20:36:13 +02:00
|
|
|
def validate_name(form, field):
|
2011-07-30 01:51:13 +02:00
|
|
|
if Person.query.filter(Person.name == field.data)\
|
2011-08-22 23:19:00 +02:00
|
|
|
.filter(Person.project == form.project)\
|
|
|
|
.filter(Person.activated == True).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-09-13 22:58:53 +02:00
|
|
|
def save(self, project, person):
|
|
|
|
# if the user is already bound to the project, just reactivate him
|
|
|
|
person.name = self.name.data
|
|
|
|
person.project = project
|
|
|
|
|
|
|
|
return person
|
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)
|
2011-08-10 00:20:16 +02:00
|
|
|
|
2011-09-09 19:14:19 +02:00
|
|
|
|
|
|
|
class CreateArchiveForm(Form):
|
|
|
|
start_date = DateField("Start date", validators=[Required(),])
|
|
|
|
end_date = DateField("End date", validators=[Required(),])
|
|
|
|
name = TextField("Name for this archive (optional)")
|