From ec8fe2326b209d580922876e9b1d32cbe98646b7 Mon Sep 17 00:00:00 2001 From: Jocelyn Delande Date: Sat, 22 Aug 2015 01:19:27 +0200 Subject: [PATCH] Added member edit form --- budget/forms.py | 14 ++++++++++---- budget/static/css/main.css | 1 - budget/templates/edit_member.html | 17 +++++++++++++++++ budget/templates/forms.html | 14 ++++++++++++++ budget/templates/list_bills.html | 2 +- budget/web.py | 18 ++++++++++++++++++ 6 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 budget/templates/edit_member.html diff --git a/budget/forms.py b/budget/forms.py index 918e82a..7d6eb51 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -155,16 +155,18 @@ class MemberForm(Form): weight = CommaDecimalField(_("Weight"), default=1) submit = SubmitField(_("Add")) - def __init__(self, project, *args, **kwargs): + def __init__(self, project, edit=False, *args, **kwargs): super(MemberForm, self).__init__(*args, **kwargs) self.project = project + self.edit = edit def validate_name(form, field): if field.data == form.name.default: raise ValidationError(_("User name incorrect")) - if Person.query.filter(Person.name == field.data)\ - .filter(Person.project == form.project)\ - .filter(Person.activated == True).all(): + if (not form.edit and Person.query.filter( + Person.name == field.data, + Person.project == form.project, + Person.activated == True).all()): raise ValidationError(_("This project already have this member")) def save(self, project, person): @@ -175,6 +177,10 @@ class MemberForm(Form): return person + def fill(self, member): + self.name.data = member.name + self.weight.data = member.weight + class InviteForm(Form): emails = TextAreaField(_("People to notify")) diff --git a/budget/static/css/main.css b/budget/static/css/main.css index 143ce8b..55dad99 100644 --- a/budget/static/css/main.css +++ b/budget/static/css/main.css @@ -217,4 +217,3 @@ tr.payer_line .balance-name{ .row-fluid > .offset3{margin-left:25.5%;} .row-fluid > .offset2{margin-left:17%;} .row-fluid > .offset1{margin-left:8.5%;} - diff --git a/budget/templates/edit_member.html b/budget/templates/edit_member.html new file mode 100644 index 0000000..5f097f9 --- /dev/null +++ b/budget/templates/edit_member.html @@ -0,0 +1,17 @@ +{% extends "layout.html" %} + +{% block js %} + $('#cancel-form').click(function(){location.href={{ url_for(".list_bills") }};}); +{% endblock %} + + +{% block top_menu %} +{{ _("Back to the list") }} +{% endblock %} + +{% block content %} + +
+ {{ forms.edit_member(form, edit) }} +
+{% endblock %} diff --git a/budget/templates/forms.html b/budget/templates/forms.html index ec73515..07e5b3d 100644 --- a/budget/templates/forms.html +++ b/budget/templates/forms.html @@ -95,6 +95,20 @@ {{ form.name(placeholder=_("Type user name here")) }} {% endmacro %} +{% macro edit_member(form, title=True) %} +
+ {% if title %}{{ _("Edit this member") }}{% endif %} + {% include "display_errors.html" %} + {{ form.hidden_tag() }} + {{ input(form.name) }} + {{ input(form.weight) }} +
+
+ {{ form.submit(class="btn btn-primary") }} +
+{% endmacro %} + + {% macro invites(form) %} {{ form.hidden_tag() }} {{ input(form.emails) }} diff --git a/budget/templates/list_bills.html b/budget/templates/list_bills.html index 9bad344..0e0efd8 100644 --- a/budget/templates/list_bills.html +++ b/budget/templates/list_bills.html @@ -72,7 +72,7 @@
-
+
{% else %} diff --git a/budget/web.py b/budget/web.py index 77de026..63fbe4d 100644 --- a/budget/web.py +++ b/budget/web.py @@ -322,6 +322,24 @@ def remove_member(member_id): return redirect(url_for(".list_bills")) +@main.route("//members//edit", + methods=["POST", "GET"]) +def edit_member(member_id): + member = Person.query.get(member_id, g.project) + if not member: + raise werkzeug.exceptions.NotFound() + form = MemberForm(g.project, edit=True) + + if request.method == 'POST' and form.validate(): + form.save(g.project, member) + db.session.commit() + flash(_("User '%(name)s' has been edited", name=member.name)) + return redirect(url_for(".list_bills")) + + form.fill(member) + return render_template("edit_member.html", form=form, edit=True) + + @main.route("//add", methods=["GET", "POST"]) def add_bill(): form = get_billform_for(g.project)