From b0d41291afade8aec86502d07d1d29d000ff1bca Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Tue, 13 Sep 2011 22:58:53 +0200 Subject: API: Create and Update support --- budget/forms.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'budget/forms.py') diff --git a/budget/forms.py b/budget/forms.py index 7ac48cc..cfd5788 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -1,6 +1,6 @@ from flaskext.wtf import * from wtforms.widgets import html_params -from models import Project, Person, Bill +from models import Project, Person, Bill, db from datetime import datetime @@ -39,6 +39,15 @@ class ProjectForm(Form): contact_email=self.contact_email.data) return project + def update(self, project): + """Update the project with the information from the form""" + project.name = self.name.data + project.id = self.id.data + project.password = self.password.data + project.contact_email = self.contact_email.data + + return project + class AuthenticationForm(Form): id = TextField("Project identifier", validators=[Required()]) @@ -76,19 +85,26 @@ class BillForm(Form): 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 __init__(self, project, *args, **kwargs): + super(MemberForm, self).__init__(*args, **kwargs) + self.project = project + def validate_name(form, field): if Person.query.filter(Person.name == field.data)\ .filter(Person.project == form.project)\ .filter(Person.activated == True).all(): raise ValidationError("This project already have this member") + 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 class InviteForm(Form): emails = TextAreaField("People to notify") -- cgit v1.1 From 20ab40690d74befcd8fc75f24f301759840bf43a Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Wed, 14 Sep 2011 22:03:18 +0200 Subject: Provide a way to edit a project. Fix #17 --- budget/forms.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'budget/forms.py') diff --git a/budget/forms.py b/budget/forms.py index cfd5788..ceda0e7 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -18,16 +18,11 @@ def select_multi_checkbox(field, ul_class='', **kwargs): return u''.join(html) -class ProjectForm(Form): +class EditProjectForm(Form): name = TextField("Project name", validators=[Required()]) - id = TextField("Project identifier", validators=[Required()]) - password = PasswordField("Password", validators=[Required()]) + password = TextField("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") + submit = SubmitField("Edit the project") def save(self): """Create a new project with the information given by this form. @@ -42,12 +37,21 @@ class ProjectForm(Form): def update(self, project): """Update the project with the information from the form""" project.name = self.name.data - project.id = self.id.data project.password = self.password.data project.contact_email = self.contact_email.data return project +class ProjectForm(EditProjectForm): + id = TextField("Project identifier", validators=[Required()]) + password = PasswordField("Password", validators=[Required()]) + submit = SubmitField("Create the project") + + def validate_id(form, field): + if Project.query.get(field.data): + raise ValidationError("This project id is already used") + + class AuthenticationForm(Form): id = TextField("Project identifier", validators=[Required()]) -- cgit v1.1