diff --git a/budget/forms.py b/budget/forms.py index dccf5fa3..ac2c1e2e 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -1,9 +1,11 @@ -from flaskext.wtf import * +from flaskext.wtf import DateField, DecimalField, Email, Form, PasswordField, \ + Required, SelectField, SelectMultipleField, SubmitField, TextAreaField, \ + TextField, ValidationError from flaskext.babel import lazy_gettext as _ from flask import request from wtforms.widgets import html_params -from models import Project, Person, Bill, db +from models import Project, Person from datetime import datetime from jinja2 import Markup from utils import slugify @@ -18,14 +20,16 @@ def select_multi_checkbox(field, ul_class='', **kwargs): js_function = u'toggle();' options = dict(kwargs, id=choice_id, onclick=js_function) label = _("Select All/None") - html.append(u'
  • ' % (choice_id, ' ' % html_params(**options), label)) + html.append(u'
  • ' + % (choice_id, ' ' % html_params(**options), label)) 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' - html.append(u'
  • ' % (choice_id, ' ' % html_params(**options), label)) + html.append(u'
  • ' + % (choice_id, ' ' % html_params(**options), label)) html.append(u'') return u''.join(html) @@ -33,18 +37,20 @@ def select_multi_checkbox(field, ul_class='', **kwargs): def get_billform_for(project, set_default=True, **kwargs): """Return an instance of BillForm configured for a particular project. - :set_default: if set to True, on GET methods (usually when we want to + :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(**kwargs) - form.payed_for.choices = form.payer.choices = [(m.id, m.name) for m in project.active_members] + form.payed_for.choices = form.payer.choices = [(m.id, m.name) + for m in project.active_members] form.payed_for.default = [m.id for m in project.active_members] if set_default and request.method == "GET": form.set_default() return form + class CommaDecimalField(DecimalField): """A class to deal with comma in Decimal Field""" def process_formdata(self, value): @@ -63,8 +69,8 @@ class EditProjectForm(Form): Returns the created instance """ - project = Project(name=self.name.data, id=self.id.data, - password=self.password.data, + project = Project(name=self.name.data, id=self.id.data, + password=self.password.data, contact_email=self.contact_email.data) return project @@ -85,7 +91,12 @@ class ProjectForm(EditProjectForm): def validate_id(form, field): 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. We tried to generate an identifier for you but a project with this identifier already exists. Please create a new identifier you will be able to remember."))) + raise ValidationError(Markup(_("The project identifier is used " + "to log in and for the URL of the project. " + "We tried to generate an identifier for you but a project " + "with this identifier already exists. " + "Please create a new identifier " + "that you will be able to remember."))) class AuthenticationForm(Form): @@ -108,17 +119,18 @@ class BillForm(Form): what = TextField(_("What?"), validators=[Required()]) payer = SelectField(_("Payer"), validators=[Required()], coerce=int) amount = CommaDecimalField(_("Amount paid"), validators=[Required()]) - payed_for = SelectMultipleField(_("For whom?"), + payed_for = SelectMultipleField(_("For whom?"), validators=[Required()], widget=select_multi_checkbox, coerce=int) submit = SubmitField(_("Submit")) submit2 = SubmitField(_("Submit and add a new one")) def save(self, bill, project): - 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, project) for ower in self.payed_for.data] + 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, project) + for ower in self.payed_for.data] return bill @@ -141,7 +153,8 @@ class BillForm(Form): class MemberForm(Form): - name = TextField(_("Name"), validators=[Required()], default=_("Type user name here")) + name = TextField(_("Name"), validators=[Required()], + default=_("Type user name here")) submit = SubmitField(_("Add")) def __init__(self, project, *args, **kwargs): @@ -163,6 +176,7 @@ class MemberForm(Form): return person + class InviteForm(Form): emails = TextAreaField(_("People to notify")) submit = SubmitField(_("Send invites")) @@ -171,11 +185,11 @@ class InviteForm(Form): validator = Email() for email in [email.strip() for email in form.emails.data.split(",")]: if not validator.regex.match(email): - raise ValidationError(_("The email %(email)s is not valid", + raise ValidationError(_("The email %(email)s is not valid", email=email)) class CreateArchiveForm(Form): - start_date = DateField(_("Start date"), validators=[Required(),]) - end_date = DateField(_("End date"), validators=[Required(),]) + start_date = DateField(_("Start date"), validators=[Required(), ]) + end_date = DateField(_("End date"), validators=[Required(), ]) name = TextField(_("Name for this archive (optional)"))