Améliore form. logos (validation des noms) + messages flash

This commit is contained in:
Emmanuel Viennet 2022-02-26 10:09:14 +01:00
parent 9bc5f27b16
commit aa609aa0cf
4 changed files with 38 additions and 28 deletions

View File

@ -30,17 +30,15 @@ Formulaires configuration logos
Contrib @jmp, dec 21 Contrib @jmp, dec 21
""" """
import re
from flask import flash, url_for, redirect, render_template from flask import flash, url_for, redirect, render_template
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed from flask_wtf.file import FileField, FileAllowed
from wtforms import SelectField, SubmitField, FormField, validators, FieldList from wtforms import SubmitField, FormField, validators, FieldList
from wtforms import ValidationError
from wtforms.fields.simple import StringField, HiddenField from wtforms.fields.simple import StringField, HiddenField
from app import AccessDenied
from app.models import Departement from app.models import Departement
from app.models import ScoDocSiteConfig
from app.scodoc import sco_logos, html_sco_header from app.scodoc import sco_logos, html_sco_header
from app.scodoc import sco_utils as scu from app.scodoc import sco_utils as scu
from app.scodoc.sco_config_actions import ( from app.scodoc.sco_config_actions import (
@ -49,10 +47,11 @@ from app.scodoc.sco_config_actions import (
LogoInsert, LogoInsert,
) )
from flask_login import current_user
from app.scodoc import sco_utils as scu
from app.scodoc.sco_logos import find_logo from app.scodoc.sco_logos import find_logo
JAVASCRIPTS = html_sco_header.BOOTSTRAP_MULTISELECT_JS + [] JAVASCRIPTS = html_sco_header.BOOTSTRAP_MULTISELECT_JS + []
CSSSTYLES = html_sco_header.BOOTSTRAP_MULTISELECT_CSS CSSSTYLES = html_sco_header.BOOTSTRAP_MULTISELECT_CSS
@ -111,6 +110,15 @@ def dept_key_to_id(dept_key):
return dept_key return dept_key
def logo_name_validator(message=None):
def validate_logo_name(form, field):
name = field.data if field.data else ""
if not scu.is_valid_filename(name):
raise ValidationError(message)
return validate_logo_name
class AddLogoForm(FlaskForm): class AddLogoForm(FlaskForm):
"""Formulaire permettant l'ajout d'un logo (dans un département)""" """Formulaire permettant l'ajout d'un logo (dans un département)"""
@ -118,11 +126,7 @@ class AddLogoForm(FlaskForm):
name = StringField( name = StringField(
label="Nom", label="Nom",
validators=[ validators=[
validators.regexp( logo_name_validator("Nom de logo invalide (alphanumérique, _)"),
r"^[a-zA-Z0-9-_]*$",
re.IGNORECASE,
"Ne doit comporter que lettres, chiffres, _ ou -",
),
validators.Length( validators.Length(
max=20, message="Un nom ne doit pas dépasser 20 caractères" max=20, message="Un nom ne doit pas dépasser 20 caractères"
), ),
@ -373,11 +377,11 @@ def config_logos():
if action: if action:
action.execute() action.execute()
flash(action.message) flash(action.message)
return redirect( return redirect(url_for("scodoc.configure_logos"))
url_for( else:
"scodoc.configure_logos", if not form.validate():
) scu.flash_errors(form)
)
return render_template( return render_template(
"config_logos.html", "config_logos.html",
scodoc_dept=None, scodoc_dept=None,

View File

@ -50,7 +50,7 @@ import pydot
import requests import requests
from flask import g, request from flask import g, request
from flask import url_for, make_response, jsonify from flask import flash, url_for, make_response, jsonify
from config import Config from config import Config
from app import log from app import log
@ -616,6 +616,16 @@ def bul_filename(sem, etud, format):
return filename return filename
def flash_errors(form):
"""Flashes form errors (version sommaire)"""
for field, errors in form.errors.items():
flash(
"Erreur: voir le champs %s" % (getattr(form, field).label.text,),
"warning",
)
# see https://getbootstrap.com/docs/4.0/components/alerts/
def sendCSVFile(data, filename): # DEPRECATED utiliser send_file def sendCSVFile(data, filename): # DEPRECATED utiliser send_file
"""publication fichier CSV.""" """publication fichier CSV."""
return send_file(data, filename=filename, mime=CSV_MIMETYPE, attached=True) return send_file(data, filename=filename, mime=CSV_MIMETYPE, attached=True)

View File

@ -57,12 +57,10 @@
{% block content %} {% block content %}
<div class="container"> <div class="container">
{% with messages = get_flashed_messages() %} {% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %} {% for category, message in messages %}
{% for message in messages %} <div class="alert alert-info alert-{{ category }}" role="alert">{{ message }}</div>
<div class="alert alert-info" role="alert">{{ message }}</div>
{% endfor %} {% endfor %}
{% endif %}
{% endwith %} {% endwith %}
{# application content needs to be provided in the app_content block #} {# application content needs to be provided in the app_content block #}

View File

@ -23,12 +23,10 @@
<div id="gtrcontent" class="gtrcontent"> <div id="gtrcontent" class="gtrcontent">
<div class="container"> <div class="container">
{% with messages = get_flashed_messages() %} {% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %} {% for category, message in messages %}
{% for message in messages %} <div class="alert alert-info alert-{{ category }}" role="alert">{{ message }}</div>
<div class="alert alert-info" role="alert">{{ message }}</div>
{% endfor %} {% endfor %}
{% endif %}
{% endwith %} {% endwith %}
</div> </div>
{% if sco.sem %} {% if sco.sem %}