mirror of
https://github.com/YunoHost-Apps/ihatemoney_ynh.git
synced 2024-09-03 19:26:15 +02:00
Provide a way to edit a project. Fix #17
This commit is contained in:
parent
5721be1d15
commit
20ab40690d
6 changed files with 73 additions and 19 deletions
|
@ -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()])
|
||||
|
|
|
@ -2,10 +2,7 @@
|
|||
|
||||
{% block content %}
|
||||
<h2>Create a new project</h2>
|
||||
<div class="uniForm">
|
||||
<form method="post" class="container span-24 add-bill">
|
||||
<form method="post">
|
||||
{{ forms.create_project(form) }}
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
|
|
|
@ -52,6 +52,17 @@
|
|||
|
||||
{% endmacro %}
|
||||
|
||||
{% macro edit_project(form) %}
|
||||
|
||||
{% include "display_errors.html" %}
|
||||
{{ form.hidden_tag() }}
|
||||
{{ input(form.name) }}
|
||||
{{ input(form.password) }}
|
||||
{{ input(form.contact_email) }}
|
||||
{{ submit(form.submit) }}
|
||||
|
||||
{% endmacro %}
|
||||
|
||||
{% macro add_bill(form, edit=False) %}
|
||||
|
||||
<fieldset>
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
<li class="menu">
|
||||
<a href="#" class="menu"><strong>{{ g.project.name }}</strong> options</a>
|
||||
<ul class="menu-dropdown" style="display: none; ">
|
||||
<li><a href="">Project settings</a></li>
|
||||
<li><a href="{{ url_for(".edit_project") }}">Project settings</a></li>
|
||||
<li class="divider"></li>
|
||||
{% for id, name in session['projects'] %}
|
||||
{% if id != g.project.id %}
|
||||
|
|
|
@ -30,9 +30,8 @@ class TestCase(unittest.TestCase):
|
|||
|
||||
def login(self, project, password=None, test_client=None):
|
||||
password = password or project
|
||||
test_client = test_client or self.app
|
||||
|
||||
return test_client.post('/authenticate', data=dict(
|
||||
return self.app.post('/authenticate', data=dict(
|
||||
id=project, password=password), follow_redirects=True)
|
||||
|
||||
def post_project(self, name):
|
||||
|
@ -307,6 +306,31 @@ class BudgetTestCase(TestCase):
|
|||
balance = models.Project.query.get("raclette").get_balance()
|
||||
self.assertEqual(set(balance.values()), set([19.0, -19.0]))
|
||||
|
||||
def test_edit_project(self):
|
||||
# A project should be editable
|
||||
|
||||
self.post_project("raclette")
|
||||
new_data = {
|
||||
'name': 'Super raclette party!',
|
||||
'contact_email': 'alexis@notmyidea.org',
|
||||
'password': 'didoudida'
|
||||
}
|
||||
|
||||
resp = self.app.post("/raclette/edit", data=new_data,
|
||||
follow_redirects=True)
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
project = models.Project.query.get("raclette")
|
||||
|
||||
for key, value in new_data.items():
|
||||
self.assertEqual(getattr(project, key), value, key)
|
||||
|
||||
# Editing a project with a wrong email address should fail
|
||||
new_data['contact_email'] = 'wrong_email'
|
||||
|
||||
resp = self.app.post("/raclette/edit", data=new_data,
|
||||
follow_redirects=True)
|
||||
self.assertIn("Invalid email address", resp.data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
@ -7,7 +7,7 @@ import werkzeug
|
|||
# local modules
|
||||
from models import db, Project, Person, Bill
|
||||
from forms import (ProjectForm, AuthenticationForm, BillForm, MemberForm,
|
||||
InviteForm, CreateArchiveForm)
|
||||
InviteForm, CreateArchiveForm, EditProjectForm)
|
||||
from utils import get_billform_for, Redirect303
|
||||
|
||||
"""
|
||||
|
@ -130,6 +130,24 @@ def create_project():
|
|||
|
||||
return render_template("create_project.html", form=form)
|
||||
|
||||
@main.route("/<project_id>/edit", methods=["GET", "POST"])
|
||||
def edit_project():
|
||||
form = EditProjectForm()
|
||||
if request.method == "POST":
|
||||
if form.validate():
|
||||
project = form.update(g.project)
|
||||
db.session.commit()
|
||||
session[project.id] = project.password
|
||||
|
||||
return redirect(url_for(".list_bills"))
|
||||
else:
|
||||
form.name.data = g.project.name
|
||||
form.password.data = g.project.password
|
||||
form.contact_email.data = g.project.contact_email
|
||||
|
||||
return render_template("edit_project.html", form=form)
|
||||
|
||||
|
||||
@main.route("/exit")
|
||||
def exit():
|
||||
# delete the session
|
||||
|
|
Loading…
Reference in a new issue