diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index b644e2bc2..e6235eaef 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 7c7415187..d7a3fac0b 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 301e31051..f5615a435 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 7659250ba..7e7792065 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 000000000..7bb32a44a --- /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 %} +