From 8d64bf3b87477b3683e0379a79706b9236a58243 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Tue, 5 Apr 2022 23:01:38 +0200 Subject: [PATCH] formulaire dynamique envoi offre --- app/entreprises/forms.py | 32 +++++--------- app/entreprises/routes.py | 9 ++-- app/templates/entreprises/contacts.html | 2 + .../entreprises/envoi_offre_form.html | 44 +++++++++++++++++-- .../entreprises/fiche_entreprise.html | 2 +- 5 files changed, 58 insertions(+), 31 deletions(-) diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 3ab5fb1f..67a60025 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -40,6 +40,7 @@ from wtforms import ( SelectMultipleField, DateField, BooleanField, + FieldList, ) from wtforms.validators import ValidationError, DataRequired, Email, Optional from wtforms.widgets import ListWidget, CheckboxInput @@ -424,19 +425,13 @@ class StageApprentissageModificationForm(FlaskForm): class EnvoiOffreForm(FlaskForm): - responsable_1 = _build_string_field( - "Responsable de formation (*)", - render_kw={"placeholder": "Tapez le nom du responsable de formation"}, - ) - responsable_2 = _build_string_field( - "Responsable de formation", - required=False, - render_kw={"placeholder": "Tapez le nom du responsable de formation"}, - ) - responsable_3 = _build_string_field( - "Responsable de formation", - required=False, - render_kw={"placeholder": "Tapez le nom du responsable de formation"}, + responsables = FieldList( + _build_string_field( + "Responsable (*)", + render_kw={"placeholder": "Tapez le nom du responsable de formation"}, + ), + min_entries=1, + max_entries=5, ) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE) @@ -445,12 +440,9 @@ class EnvoiOffreForm(FlaskForm): if not FlaskForm.validate(self): validate = False - for field in self: - if ( - field.name in ["responsable_1", "responsable_2", "responsable_3"] - and field.data.strip() - ): - responsable_data = field.data.upper().strip() + for entry in self.responsables.entries: + if entry.data: + responsable_data = entry.data.upper().strip() stm = text( "SELECT id, UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')')) FROM \"user\" WHERE UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')'))=:responsable_data" ) @@ -460,7 +452,7 @@ class EnvoiOffreForm(FlaskForm): .first() ) if responsable is None: - field.errors.append("Champ incorrect (selectionnez dans la liste)") + entry.errors.append("Champ incorrect (selectionnez dans la liste)") validate = False return validate diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 30ae040f..bf78bd5a 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -987,12 +987,9 @@ def envoyer_offre(id): ) form = EnvoiOffreForm() if form.validate_on_submit(): - for field in form: - if ( - field.name in ["responsable_1", "responsable_2", "responsable_3"] - and field.data - ): - responsable_data = field.data.upper().strip() + for responsable in form.responsables.entries: + if responsable.data.strip(): + responsable_data = responsable.data.upper().strip() stm = text( "SELECT id, UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')')) FROM \"user\" WHERE UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')'))=:responsable_data" ) diff --git a/app/templates/entreprises/contacts.html b/app/templates/entreprises/contacts.html index cb01d4ed..2896627e 100644 --- a/app/templates/entreprises/contacts.html +++ b/app/templates/entreprises/contacts.html @@ -11,7 +11,9 @@ {% block app_content %}

Liste des contacts

+ {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %} Ajouter contact + {% endif %} diff --git a/app/templates/entreprises/envoi_offre_form.html b/app/templates/entreprises/envoi_offre_form.html index 61f50a7e..c964bfc4 100644 --- a/app/templates/entreprises/envoi_offre_form.html +++ b/app/templates/entreprises/envoi_offre_form.html @@ -16,7 +16,20 @@

(*) champs requis

- {{ wtf.quick_form(form, novalidate=True) }} + + {{ form.hidden_tag() }} + {{ form.responsables.label }}
+ {% for subfield in form.responsables %} + {% if subfield.errors %} + {% for error in subfield.errors %} +

{{ error }}

+ {% endfor %} + {% endif %} + {% endfor %} + {{ form.responsables }} + + + @@ -30,9 +43,32 @@ minchars: 2, timeout: 60000 }; - var as_responsables_1 = new bsn.AutoSuggest('responsable_1', responsables_options); - var as_responsables_2 = new bsn.AutoSuggest('responsable_2', responsables_options); - var as_responsables_3 = new bsn.AutoSuggest('responsable_3', responsables_options); + + let allResponsablesFieldWrapper = document.getElementById('responsables'); + let allResponsablesField = allResponsablesFieldWrapper.getElementsByTagName('input'); + for(let i = 0; i < allResponsablesField.length; i++) { + new bsn.AutoSuggest(allResponsablesField[i].id, responsables_options); + document.getElementById(allResponsablesField[i].id).classList.add("form-control"); + } + + let addResponsableFieldBtn = document.getElementById('add-responsable-field'); + addResponsableFieldBtn.addEventListener('click', function(e){ + e.preventDefault(); + let allResponsablesFieldWrapper = document.getElementById('responsables'); + let allResponsablesField = allResponsablesFieldWrapper.getElementsByTagName('input'); + if(allResponsablesField.length > 4) { + return; + } + let responsableInputIds = [] + for(let i = 0; i < allResponsablesField.length; i++) { + responsableInputIds.push(parseInt(allResponsablesField[i].name.split('-')[1])); + } + let newFieldName = `responsables-${Math.max(...responsableInputIds) + 1}`; + allResponsablesFieldWrapper.insertAdjacentHTML('beforeend',` +
  • + `); + var as_r = new bsn.AutoSuggest(newFieldName, responsables_options); + }); } {% endblock %} \ No newline at end of file diff --git a/app/templates/entreprises/fiche_entreprise.html b/app/templates/entreprises/fiche_entreprise.html index a146e5dd..7bb61137 100644 --- a/app/templates/entreprises/fiche_entreprise.html +++ b/app/templates/entreprises/fiche_entreprise.html @@ -43,8 +43,8 @@ SupprimerAjouter offreAjouter correspondant - Ajouter contact {% endif %} + Liste contactsVoir les offres expirées