Skip to content

Commit

Permalink
Fix t_roles: add option to select no organism
Browse files Browse the repository at this point in the history
Fix #130.
  • Loading branch information
jpm-cbna committed Apr 13, 2021
1 parent 0f12e97 commit 7e9020f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion app/t_roles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,29 @@ class MultiCheckboxField(SelectMultipleField):
widget = widgets.ListWidget(prefix_label=False)
option_widget = widgets.CheckboxInput()

def coerce_for_select(value):
"""Permet à un champ de type SelectField contenant des id (=integer)
d'accepter une valeur vide permettant de définir le champ à NULL dans
la base de données.
Utiliser "coerce_for_select" à la place de la valeur "int" du
paramètre "coerce" d'un champ SelectField().
Ajouter ensuite :
- une entrée au paramètre "choices" qui contiendra :
choices=[("", "-- Selectionnez une valeur...")]
- une entrée au paramètre "validators" :
validators=[validators.Optional()]
"""
if value == "":
return None
else:
return int(value)

class Utilisateur(FlaskForm):
active = BooleanField('Actif', default = True, false_values=(False, 'false'))
nom_role = StringField('Nom', validators=[DataRequired(message="Le nom de l'utilisateur est obligatoire")])
prenom_role = StringField('Prenom')
desc_role = TextAreaField('Description')
id_organisme = SelectField('Organisme', coerce=int, choices=[], default=-1)
id_organisme = SelectField('Organisme', choices=[], coerce=coerce_for_select, default="", validators=[validators.Optional()])
a_groupe = SelectMultipleField('', choices=[], coerce=int)
identifiant = StringField('Identifiant')
pass_plus = PasswordField('Mot de passe')
Expand Down
2 changes: 1 addition & 1 deletion app/t_roles/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from pypnusershub import routes as fnauth
from pypnusershub.db.models import check_and_encrypt_password


from app.t_roles import forms as t_rolesforms
from app.models import TRoles, Bib_Organismes, CorRoles
from app.utils.utils_all import strigify_dict
Expand Down Expand Up @@ -131,6 +130,7 @@ def addorupdate(id_role=None):
form.id_organisme.choices = Bib_Organismes.choixSelect(
"id_organisme", "nom_organisme", order_by="nom_organisme"
)
form.id_organisme.choices.insert(0, ("", "-- Selectionnez un organisme..."))
form.a_groupe.choices = TRoles.choix_group("id_role", "nom_role", aucun=None)

if id_role is not None:
Expand Down

0 comments on commit 7e9020f

Please sign in to comment.