Better error message for alredy taken project id.

Fixes #346
This commit is contained in:
Alexis Métaireau 2018-08-05 20:36:41 +02:00
parent 67de8c3b35
commit c63f1859f6
3 changed files with 13 additions and 18 deletions

View file

@ -74,12 +74,9 @@ class ProjectForm(EditProjectForm):
def validate_id(form, field): def validate_id(form, field):
form.id.data = slugify(field.data) form.id.data = slugify(field.data)
if (form.id.data == "dashboard") or Project.query.get(form.id.data): if (form.id.data == "dashboard") or Project.query.get(form.id.data):
message = _("The project identifier is used to log in and for the " message = _("A project with this identifier (\"%s\") already exists. "
"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 that you will be able " "Please create a new identifier that you will be able "
"to remember") "to remember" % form.id.data)
raise ValidationError(Markup(message)) raise ValidationError(Markup(message))

View file

@ -1,5 +1,5 @@
{% for field_name, field_errors in form.errors.items() if field_errors %} {% for field_name, field_errors in form.errors.items() if field_errors %}
{% for error in field_errors %} {% for error in field_errors %}
<p class="alert alert-danger"><strong>{{ form[field_name].label.text }}:</strong> {{ error }}</p> <p class="alert alert-warning"><strong>{{ form[field_name].label.text }}:</strong> {{ error }}</p>
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}

View file

@ -2,9 +2,9 @@
The blueprint for the web interface. The blueprint for the web interface.
Contains all the interaction logic with the end user (except forms which Contains all the interaction logic with the end user (except forms which
are directly handled in the forms module. are directly handled in the forms module).
Basically, this blueprint takes care of the authentication and provides This blueprint takes care of the authentication and provides
some shortcuts to make your life better when coding (see `pull_project` some shortcuts to make your life better when coding (see `pull_project`
and `add_project_id` for a quick overview) and `add_project_id` for a quick overview)
""" """
@ -62,9 +62,9 @@ def requires_admin(bypass=None):
@main.url_defaults @main.url_defaults
def add_project_id(endpoint, values): def add_project_id(endpoint, values):
"""Add the project id to the url calls if it is expected. """Add the project id to the url calls if it is expected by the endpoint.
This is to not carry it everywhere in the templates. This avoids carrying it all over the place (especially templates).
""" """
if 'project_id' in values or not hasattr(g, 'project'): if 'project_id' in values or not hasattr(g, 'project'):
return return
@ -86,12 +86,12 @@ def set_show_admin_dashboard_link(endpoint, values):
@main.url_value_preprocessor @main.url_value_preprocessor
def pull_project(endpoint, values): def pull_project(endpoint, values):
"""When a request contains a project_id value, transform it directly """When a request contains a project_id value, retrieve the project object
into a project by checking the credentials stored in the session. from the database after checking the credentials stored in the session.
With administration credentials, one can access any project. With administration credentials, one can access any project.
If not, redirect the user to an authentication form If no credentials are provided, redirect the user to an authentication form.
""" """
if endpoint == "authenticate": if endpoint == "authenticate":
return return
@ -219,11 +219,9 @@ def create_project():
form.name.data = request.values['project_id'] form.name.data = request.values['project_id']
if request.method == "POST": if request.method == "POST":
# At first, we don't want the user to bother with the identifier # As we don't want the user to bother with the project identifier,
# so it will automatically be missing because not displayed into # it is made equal to the project name, the validation logic will take
# the form # care of the slugification to create a project id.
# Thus we fill it with the same value as the filled name,
# the validation will take care of the slug
if not form.id.data: if not form.id.data:
form.id.data = form.name.data form.id.data = form.name.data
if form.validate(): if form.validate():