From ab7a68c74bb805e10b21e2d0810c8c7e98c3806a Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Mon, 7 Feb 2022 18:39:32 +0100 Subject: [PATCH] debut import excel entreprises --- app/entreprises/forms.py | 13 +++++- app/entreprises/routes.py | 42 +++++++++++++++++++ app/templates/entreprises/entreprises.html | 3 ++ app/templates/entreprises/form.html | 2 - .../entreprises/import_entreprises.html | 21 ++++++++++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 app/templates/entreprises/import_entreprises.html diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index b644e2bc..e6235eae 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -338,7 +338,7 @@ class AjoutFichierForm(FlaskForm): FileAllowed(["pdf", "docx"], "Fichier .pdf ou .docx uniquement"), ], ) - submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) + submit = SubmitField("Ajouter", render_kw={"style": "margin-bottom: 10px;"}) class SuppressionConfirmationForm(FlaskForm): @@ -347,3 +347,14 @@ class SuppressionConfirmationForm(FlaskForm): class ValidationConfirmationForm(FlaskForm): submit = SubmitField("Valider", render_kw={"style": "margin-bottom: 10px;"}) + + +class ImportEntreprisesForm(FlaskForm): + fichier = FileField( + "Fichier", + validators=[ + FileRequired(message=CHAMP_REQUIS), + FileAllowed(["xlsx"], "Fichier .xlsx uniquement"), + ], + ) + submit = SubmitField("Importer", render_kw={"style": "margin-bottom: 10px;"}) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 7c741518..d7a3fac0 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -23,6 +23,7 @@ from app.entreprises.forms import ( EnvoiOffreForm, AjoutFichierForm, ValidationConfirmationForm, + ImportEntreprisesForm, ) from app.entreprises import bp from app.entreprises.models import ( @@ -809,6 +810,47 @@ def export_entreprises(): abort(404) +@bp.route("/get_import_entreprises_file_sample") +@permission_required(Permission.RelationsEntreprisesExport) +def get_import_entreprises_file_sample(): + keys = [ + "siret", + "nom_entreprise", + "adresse", + "ville", + "codepostal", + "pays", + "nom_contact", + "prenom_contact", + "telephone", + "mail", + "poste", + "service", + ] + titles = keys[:] + title = "ImportEntreprises" + xlsx = sco_excel.excel_simple_table(titles=titles, sheet_name="Entreprises") + filename = title + return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE) + + +@bp.route("/import_entreprises", methods=["GET", "POST"]) +@permission_required(Permission.RelationsEntreprisesExport) +def import_entreprises(): + form = ImportEntreprisesForm() + if form.validate_on_submit(): + path = os.path.join(Config.SCODOC_VAR_DIR, "tmp") + file = form.fichier.data + filename = secure_filename(file.filename) + file.save(os.path.join(path, filename)) + # print(sco_excel.excel_file_to_list(filename)) + return render_template( + "entreprises/import_entreprises.html", + title=("Importation entreprises"), + form=form, + ) + + @bp.route("/export_contacts") @permission_required(Permission.RelationsEntreprisesExport) def export_contacts(): diff --git a/app/templates/entreprises/entreprises.html b/app/templates/entreprises/entreprises.html index 301e3105..f5615a43 100644 --- a/app/templates/entreprises/entreprises.html +++ b/app/templates/entreprises/entreprises.html @@ -19,6 +19,9 @@ {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %} Ajouter une entreprise {% endif %} + {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) %} + Importer des entreprises + {% endif %} {% if entreprises %} {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) %} Exporter la liste des entreprises diff --git a/app/templates/entreprises/form.html b/app/templates/entreprises/form.html index 7659250b..7e779206 100644 --- a/app/templates/entreprises/form.html +++ b/app/templates/entreprises/form.html @@ -4,8 +4,6 @@ {% block styles %} {{super()}} - - {% endblock %} {% block app_content %} diff --git a/app/templates/entreprises/import_entreprises.html b/app/templates/entreprises/import_entreprises.html new file mode 100644 index 00000000..7bb32a44 --- /dev/null +++ b/app/templates/entreprises/import_entreprises.html @@ -0,0 +1,21 @@ +{# -*- mode: jinja-html -*- #} +{% extends 'base.html' %} +{% import 'bootstrap/wtf.html' as wtf %} + +{% block styles %} +{{super()}} +{% endblock %} + +{% block app_content %} +

{{ title }}

+
+
+ Obtenir la feuille excel à remplir +
+
+
+
+ {{ wtf.quick_form(form, novalidate=True) }} +
+
+{% endblock %} \ No newline at end of file