From 19ae3ab3b2af98c5e39e8a73dc9b5d973dda5aa3 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Sat, 8 Oct 2011 13:45:05 +0200 Subject: [PATCH] merge with master --- budget/api.py | 5 +++-- budget/forms.py | 9 +++++---- budget/tests.py | 3 ++- budget/utils.py | 15 --------------- budget/web.py | 6 +++--- 5 files changed, 13 insertions(+), 25 deletions(-) diff --git a/budget/api.py b/budget/api.py index c5ae76b..b197b81 100644 --- a/budget/api.py +++ b/budget/api.py @@ -2,8 +2,9 @@ from flask import * from models import db, Project, Person, Bill -from forms import ProjectForm, EditProjectForm, MemberForm, BillForm -from utils import for_all_methods, get_billform_for +from forms import (ProjectForm, EditProjectForm, MemberForm, BillForm, + get_billform_for) +from utils import for_all_methods from rest import RESTResource, need_auth# FIXME make it an ext from werkzeug import Response diff --git a/budget/forms.py b/budget/forms.py index 25731bc..499f382 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -1,4 +1,6 @@ from flaskext.wtf import * +from flask import request + from wtforms.widgets import html_params from models import Project, Person, Bill, db from datetime import datetime @@ -20,14 +22,14 @@ def select_multi_checkbox(field, ul_class='', **kwargs): return u''.join(html) -def get_billform_for(request, project, set_default=True): +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 display the default form, it will call set_default on it. """ - form = BillForm() + form = BillForm(**kwargs) 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] @@ -36,6 +38,7 @@ def get_billform_for(request, project, set_default=True): return form + class EditProjectForm(Form): name = TextField("Project name", validators=[Required()]) password = TextField("Private code", validators=[Required()]) @@ -71,10 +74,8 @@ class ProjectForm(EditProjectForm): 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. """)) diff --git a/budget/tests.py b/budget/tests.py index 1541cbb..2b573f4 100644 --- a/budget/tests.py +++ b/budget/tests.py @@ -410,7 +410,8 @@ class APITestCase(TestCase): resp = self.api_create("raclette") self.assertTrue(400, resp.status_code) - self.assertEqual('{"id": ["This project id is already used"]}', resp.data) + self.assertIn('id', json.loads(resp.data)) + # get information about it resp = self.app.get("/api/projects/raclette", headers=self.get_auth("raclette")) diff --git a/budget/utils.py b/budget/utils.py index df165b5..52fd151 100644 --- a/budget/utils.py +++ b/budget/utils.py @@ -18,21 +18,6 @@ def slugify(value): return re.sub('[-\s]+', '-', value) -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 - display the default form, it will call set_default on it. - - """ - form = BillForm(**kwargs) - 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 - class Redirect303(HTTPException, RoutingException): """Raise if the map requests a redirect. This is for example the case if `strict_slashes` are activated and an url that requires a trailing slash. diff --git a/budget/web.py b/budget/web.py index 94c42d3..28740fc 100644 --- a/budget/web.py +++ b/budget/web.py @@ -219,7 +219,7 @@ def list_bills(): bills = g.project.get_bills() return render_template("list_bills.html", bills=bills, member_form=MemberForm(g.project), - bill_form=get_billform_for(request, g.project) + bill_form=get_billform_for(g.project) ) @main.route("//members/add", methods=["GET", "POST"]) @@ -258,7 +258,7 @@ def remove_member(member_id): @main.route("//add", methods=["GET", "POST"]) def add_bill(): - form = get_billform_for(request, g.project) + form = get_billform_for(g.project) if request.method == 'POST': if form.validate(): bill = Bill() @@ -292,7 +292,7 @@ def edit_bill(bill_id): if not bill: raise werkzeug.exceptions.NotFound() - form = get_billform_for(request, g.project, set_default=False) + form = get_billform_for(g.project, set_default=False) if request.method == 'POST' and form.validate(): form.save(bill, g.project)