1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/ihatemoney_ynh.git synced 2024-09-03 19:26:15 +02:00
ihatemoney_ynh/budget/forms.py

108 lines
3.9 KiB
Python
Raw Normal View History

from flaskext.wtf import *
from wtforms.widgets import html_params
2011-07-31 00:41:28 +02:00
from models import Project, Person, Bill
from datetime import datetime
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")]
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))
html.append(u'</ul>')
return u''.join(html)
2011-07-26 16:03:00 +02:00
class ProjectForm(Form):
name = TextField("Project name", validators=[Required()])
id = TextField("Project identifier", validators=[Required()])
password = PasswordField("Password", validators=[Required()])
contact_email = TextField("Email", validators=[Required(), Email()])
submit = SubmitField("Create the project")
def validate_id(form, field):
if Project.query.get(field.data):
raise ValidationError("This project id is already used")
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
class AuthenticationForm(Form):
id = TextField("Project identifier", validators=[Required()])
password = PasswordField("Password", validators=[Required()])
submit = SubmitField("Get in")
class BillForm(Form):
date = DateField("Date", validators=[Required()], default=datetime.now)
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()], widget=select_multi_checkbox)
submit = SubmitField("Send the bill")
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]
2011-07-31 00:41:28 +02:00
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
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):
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-31 00:41:28 +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-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)")