From 7858baa841bb42f70d5e32b95a4e8f799ae06625 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Thu, 10 Feb 2022 21:17:22 +0100 Subject: [PATCH] import contacts, nettoyage --- app/entreprises/__init__.py | 2 +- app/entreprises/forms.py | 2 +- app/entreprises/models.py | 20 +- app/entreprises/routes.py | 234 +++++++++++++----- app/templates/entreprises/contacts.html | 10 +- app/templates/entreprises/entreprises.html | 4 +- .../entreprises/entreprises_validation.html | 2 +- .../entreprises/fiche_entreprise.html | 2 +- .../entreprises/import_contacts.html | 59 +++++ .../entreprises/import_entreprises.html | 12 + app/templates/entreprises/logs.html | 2 +- .../entreprises/logs_entreprise.html | 2 +- 12 files changed, 254 insertions(+), 97 deletions(-) create mode 100644 app/templates/entreprises/import_contacts.html diff --git a/app/entreprises/__init__.py b/app/entreprises/__init__.py index 34dcaed8..b211670b 100644 --- a/app/entreprises/__init__.py +++ b/app/entreprises/__init__.py @@ -22,7 +22,7 @@ def format_nom(s): @bp.app_template_filter() -def get_nomcomplet(s): +def get_nomcomplet_by_username(s): user = User.query.filter_by(user_name=s).first() return user.get_nomcomplet() diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 19c562ab..4b0db01c 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -366,7 +366,7 @@ class ValidationConfirmationForm(FlaskForm): submit = SubmitField("Valider", render_kw={"style": "margin-bottom: 10px;"}) -class ImportEntreprisesForm(FlaskForm): +class ImportForm(FlaskForm): fichier = FileField( "Fichier", validators=[ diff --git a/app/entreprises/models.py b/app/entreprises/models.py index a91dc09b..9a99f4bc 100644 --- a/app/entreprises/models.py +++ b/app/entreprises/models.py @@ -29,7 +29,7 @@ class Entreprise(db.Model): "siret": self.siret, "nom": self.nom, "adresse": self.adresse, - "codepostal": self.codepostal, + "code_postal": self.codepostal, "ville": self.ville, "pays": self.pays, } @@ -56,23 +56,7 @@ class EntrepriseContact(db.Model): "mail": self.mail, "poste": self.poste, "service": self.service, - } - - def to_dict_export(self): - entreprise = Entreprise.query.get(self.entreprise_id) - return { - "nom": self.nom, - "prenom": self.prenom, - "telephone": self.telephone, - "mail": self.mail, - "poste": self.poste, - "service": self.service, - "siret": entreprise.siret, - "nom_entreprise": entreprise.nom, - "adresse_entreprise": entreprise.adresse, - "codepostal": entreprise.codepostal, - "ville": entreprise.ville, - "pays": entreprise.pays, + "entreprise_id": self.entreprise_id, } diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index d5c34597..a78460f5 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -25,7 +25,7 @@ from app.entreprises.forms import ( EnvoiOffreForm, AjoutFichierForm, ValidationConfirmationForm, - ImportEntreprisesForm, + ImportForm, ) from app.entreprises import bp from app.entreprises.models import ( @@ -130,30 +130,30 @@ def fiche_entreprise(id): les offres de l'entreprise. """ entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404() - offres = entreprise.offres offres_with_files = [] - for offre in offres: - if date.today() > offre.expiration_date: - break - files = [] - path = os.path.join( - Config.SCODOC_VAR_DIR, - "entreprises", - f"{offre.entreprise_id}", - f"{offre.id}", - ) - if os.path.exists(path): - for dir in glob.glob( - f"{path}/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]" - ): - for file in glob.glob(f"{dir}/*"): - file = [os.path.basename(dir), os.path.basename(file)] - files.append(file) - offre_depts = EntrepriseOffreDepartement.query.filter_by( - offre_id=offre.id - ).all() - offres_with_files.append([offre, files, offre_depts]) - contacts = entreprise.contacts + for offre in entreprise.offres: + if date.today() < offre.expiration_date: + files = [] + path = os.path.join( + Config.SCODOC_VAR_DIR, + "entreprises", + f"{offre.entreprise_id}", + f"{offre.id}", + ) + if os.path.exists(path): + for dir in glob.glob( + f"{path}/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]" + ): + for file in glob.glob(f"{dir}/*"): + file = [os.path.basename(dir), os.path.basename(file)] + files.append(file) + offre_depts = EntrepriseOffreDepartement.query.filter_by( + offre_id=offre.id + ).all() + offres_with_files.append([offre, files, offre_depts]) + contacts = [] + for contact in entreprise.contacts: + contacts.append(contact) logs = ( EntrepriseLog.query.order_by(EntrepriseLog.date.desc()) .filter_by(object=id) @@ -800,8 +800,8 @@ def json_etudiants(): term = request.args.get("term").strip() etudiants = Identite.query.filter(Identite.nom.ilike(f"%{term}%")).all() list = [] - content = {} for etudiant in etudiants: + content = {} value = f"{sco_etud.format_nom(etudiant.nom)} {sco_etud.format_prenom(etudiant.prenom)}" if etudiant.inscription_courante() is not None: content = { @@ -812,7 +812,6 @@ def json_etudiants(): else: content = {"id": f"{etudiant.id}", "value": value} list.append(content) - content = {} return jsonify(results=list) @@ -829,12 +828,12 @@ def json_responsables(): User.nom.ilike(f"%{term}%"), User.nom.is_not(None), User.prenom.is_not(None) ).all() list = [] - content = {} + for responsable in responsables: + content = {} value = f"{responsable.get_nomplogin()}" content = {"id": f"{responsable.id}", "value": value, "info": ""} list.append(content) - content = {} return jsonify(results=list) @@ -846,13 +845,13 @@ def export_entreprises(): """ entreprises = Entreprise.query.filter_by(visible=True).all() if entreprises: - keys = ["siret", "nom", "adresse", "ville", "codepostal", "pays"] + keys = ["siret", "nom", "adresse", "ville", "code_postal", "pays"] titles = keys[:] L = [ [entreprise.to_dict().get(k, "") for k in keys] for entreprise in entreprises ] - title = "entreprises" + title = "Entreprises" xlsx = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title) filename = title return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE) @@ -863,6 +862,9 @@ def export_entreprises(): @bp.route("/get_import_entreprises_file_sample") @permission_required(Permission.RelationsEntreprisesExport) def get_import_entreprises_file_sample(): + """ + Permet de récupérer un fichier pour pouvoir importer des entreprises + """ keys = [ "siret", "nom_entreprise", @@ -900,7 +902,7 @@ def import_entreprises(): """ Permet d'importer des entreprises a l'aide d'un fichier excel (.xlsx) """ - form = ImportEntreprisesForm() + form = ImportForm() if form.validate_on_submit(): path = os.path.join(Config.SCODOC_VAR_DIR, "tmp") file = form.fichier.data @@ -912,7 +914,14 @@ def import_entreprises(): entreprises_import = [] siret_list = [] ligne = 0 - + titles = ["siret", "nom_entreprise", "adresse", "ville", "codepostal", "pays"] + if data[1][0] != titles: + flash("Veuillez utilisez la feuille excel à remplir") + return render_template( + "entreprises/import_entreprises.html", + title=("Importation entreprises"), + form=form, + ) for entreprise_data in data[1][1:]: ligne += 1 if ( @@ -972,30 +981,6 @@ def export_contacts(): .filter_by(visible=True) .all() ) - if contacts: - keys = ["nom", "prenom", "telephone", "mail", "poste", "service"] - titles = keys[:] - L = [[contact.to_dict().get(k, "") for k in keys] for contact in contacts] - title = "contacts" - xlsx = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title) - filename = title - return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE) - else: - abort(404) - - -@bp.route("/export_contacts_bis") -@permission_required(Permission.RelationsEntreprisesExport) -def export_contacts_bis(): - """ - Permet d'exporter la liste des contacts avec leur entreprise sous format excel (.xlsx) - """ - contacts = ( - db.session.query(EntrepriseContact) - .join(Entreprise, EntrepriseContact.entreprise_id == Entreprise.id) - .filter_by(visible=True) - .all() - ) if contacts: keys = [ "nom", @@ -1004,18 +989,11 @@ def export_contacts_bis(): "mail", "poste", "service", - "nom_entreprise", - "siret", - "adresse_entreprise", - "ville", - "codepostal", - "pays", + "entreprise_id", ] titles = keys[:] - L = [ - [contact.to_dict_export().get(k, "") for k in keys] for contact in contacts - ] - title = "contacts" + L = [[contact.to_dict().get(k, "") for k in keys] for contact in contacts] + title = "Contacts" xlsx = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title) filename = title return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE) @@ -1023,6 +1001,130 @@ def export_contacts_bis(): abort(404) +@bp.route("/get_import_contacts_file_sample") +@permission_required(Permission.RelationsEntreprisesExport) +def get_import_contacts_file_sample(): + """ + Permet de récupérer un fichier pour pouvoir importer des contacts + """ + keys = [ + "nom", + "prenom", + "telephone", + "mail", + "poste", + "service", + "entreprise_id", + ] + titles = keys[:] + title = "ImportContacts" + xlsx = sco_excel.excel_simple_table(titles=titles, sheet_name="Contacts") + filename = title + return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE) + + +def verif_contact_data(contact_data): + # champ nom, prenom et entreprise_id obligatoire + if contact_data[0] == "" or contact_data[1] == "" or contact_data[6] == "": + return False + + entreprise = Entreprise.query.filter_by(id=contact_data[6]).first() + if entreprise is None: + return False + + contact = EntrepriseContact.query.filter_by( + nom=contact_data[0], prenom=contact_data[1], entreprise_id=contact_data[6] + ).first() + if contact is not None: + return False + + # 1 moyen de contact + if contact_data[2] == "" and contact_data[3] == "": + return False + + return True + + +@bp.route("/import_contacts", methods=["GET", "POST"]) +@permission_required(Permission.RelationsEntreprisesExport) +def import_contacts(): + """ + Permet d'importer des contacts a l'aide d'un fichier excel (.xlsx) + """ + form = ImportForm() + if form.validate_on_submit(): + path = os.path.join(Config.SCODOC_VAR_DIR, "tmp") + file = form.fichier.data + filename = secure_filename(file.filename) + file_path = os.path.join(path, filename) + file.save(file_path) + data = sco_excel.excel_file_to_list(file_path) + os.remove(file_path) + contacts_import = [] + contact_list = [] + ligne = 0 + titles = [ + "nom", + "prenom", + "telephone", + "mail", + "poste", + "service", + "entreprise_id", + ] + if data[1][0] != titles: + flash("Veuillez utilisez la feuille excel à remplir") + return render_template( + "entreprises/import_contacts.html", + title=("Importation contacts"), + form=form, + ) + for contact_data in data[1][1:]: + ligne += 1 + if ( + verif_contact_data(contact_data) + and (contact_data[0], contact_data[1], contact_data[6]) + not in contact_list + ): + contact_list.append((contact_data[0], contact_data[1], contact_data[6])) + contact = EntrepriseContact( + nom=contact_data[0], + prenom=contact_data[1], + telephone=contact_data[2], + mail=contact_data[3], + poste=contact_data[4], + service=contact_data[5], + entreprise_id=contact_data[6], + ) + contacts_import.append(contact) + else: + flash(f"Erreur lors de l'importation (ligne {ligne})") + return render_template( + "entreprises/import_contacts.html", + title=("Importation contacts"), + form=form, + ) + + if len(contacts_import) > 0: + for contact in contacts_import: + db.session.add(contact) + db.session.commit() + flash(f"Importation réussie de {len(contacts_import)} contact(s)") + return render_template( + "entreprises/import_contacts.html", + title=("Importation Contacts"), + form=form, + contacts_import=contacts_import, + ) + else: + flash('Feuille "Contacts" vide') + return render_template( + "entreprises/import_contacts.html", + title=("Importation contacts"), + form=form, + ) + + @bp.route( "/get_offre_file////" ) diff --git a/app/templates/entreprises/contacts.html b/app/templates/entreprises/contacts.html index ee92442c..7ee982f7 100644 --- a/app/templates/entreprises/contacts.html +++ b/app/templates/entreprises/contacts.html @@ -9,20 +9,20 @@

Dernières opérations Voir tout

    {% for log in logs %} -
  • {{ log.date.strftime('%d %b %Hh%M') }}{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}
  • +
  • {{ log.date.strftime('%d %b %Hh%M') }}{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}
  • {% endfor %}
{% endif %} - {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) and contacts.items %}
- {% if contacts %} + {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) %} + Importer des contacts + {% endif %} + {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) and contacts.items %} Exporter la liste des contacts - Exporter la liste des contacts avec leur entreprise {% endif %}
- {% endif %}

Liste des contacts

diff --git a/app/templates/entreprises/entreprises.html b/app/templates/entreprises/entreprises.html index a820a38d..3219c78d 100644 --- a/app/templates/entreprises/entreprises.html +++ b/app/templates/entreprises/entreprises.html @@ -9,7 +9,7 @@

Dernières opérations Voir tout

    {% for log in logs %} -
  • {{ log.date.strftime('%d %b %Hh%M') }}{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}
  • +
  • {{ log.date.strftime('%d %b %Hh%M') }}{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}
  • {% endfor %}
@@ -33,7 +33,7 @@

Liste des entreprises

{% if entreprises.items %}
- +
diff --git a/app/templates/entreprises/entreprises_validation.html b/app/templates/entreprises/entreprises_validation.html index 60c6a194..90c787a5 100644 --- a/app/templates/entreprises/entreprises_validation.html +++ b/app/templates/entreprises/entreprises_validation.html @@ -9,7 +9,7 @@

Dernières opérations

    {% for log in logs %} -
  • {{ log.date.strftime('%d %b %Hh%M') }}{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}
  • +
  • {{ log.date.strftime('%d %b %Hh%M') }}{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}
  • {% endfor %}
diff --git a/app/templates/entreprises/fiche_entreprise.html b/app/templates/entreprises/fiche_entreprise.html index 443afea4..2d530e70 100644 --- a/app/templates/entreprises/fiche_entreprise.html +++ b/app/templates/entreprises/fiche_entreprise.html @@ -9,7 +9,7 @@ {% for log in logs %}
  • {{ log.date.strftime('%d %b %Hh%M') }} - {{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }} + {{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}
  • {% endfor %} diff --git a/app/templates/entreprises/import_contacts.html b/app/templates/entreprises/import_contacts.html new file mode 100644 index 00000000..a1812293 --- /dev/null +++ b/app/templates/entreprises/import_contacts.html @@ -0,0 +1,59 @@ +{# -*- mode: jinja-html -*- #} +{% extends 'base.html' %} +{% import 'bootstrap/wtf.html' as wtf %} + +{% block styles %} +{{super()}} +{% endblock %} + +{% block app_content %} +

    Importation contacts

    +
    + +
    +
    +
    + {{ wtf.quick_form(form, novalidate=True) }} +
    +
    + + {% if not contacts_import %} +
    SIRET Nom
    + + + + + + + + +
    AttributTypeDescription
    nomtextnom du contact
    prenomtextprenom du contact
    telephonetexttelephone du contact
    mailtextmail du contact
    postetextposte du contact
    servicetextservice dans lequel travaille le contact
    entreprise_idintegerl'id de l'entreprise
    + {% endif %} + + {% if contacts_import %} +
    Importation de {{ contacts_import|length }} contact(s)
    + {% for contact in contacts_import %} +
    +
    + Nom : {{ contact.nom }}
    + Prénom : {{ contact.prenom }}
    + {% if contact.telephone %} + Téléphone : {{ contact.telephone }}
    + {% endif %} + {% if contact.mail %} + Mail : {{ contact.mail }}
    + {% endif %} + {% if contact.poste %} + Poste : {{ contact.poste }}
    + {% endif %} + {% if contact.service %} + Service : {{ contact.service }}
    + {% endif %} + lien vers l'entreprise +
    +
    + {% endfor %} + {% endif %} +{% endblock %} \ No newline at end of file diff --git a/app/templates/entreprises/import_entreprises.html b/app/templates/entreprises/import_entreprises.html index 7d64196b..21aeb6a0 100644 --- a/app/templates/entreprises/import_entreprises.html +++ b/app/templates/entreprises/import_entreprises.html @@ -19,6 +19,18 @@
    + {% if not entreprises_import %} + + + + + + + + +
    AttributTypeDescription
    sirettextsiret de l'entreprise
    nomtextnom de l'entreprise
    adressetextadresse de l'entreprise
    villetextville de l'entreprise
    code_postaltextcode postal de l'entreprise
    paystextpays de l'entreprise
    + {% endif %} + {% if entreprises_import %}
    Importation de {{ entreprises_import|length }} entreprise(s)
    {% for entreprise in entreprises_import %} diff --git a/app/templates/entreprises/logs.html b/app/templates/entreprises/logs.html index 8bdc9c9d..c6cba95f 100644 --- a/app/templates/entreprises/logs.html +++ b/app/templates/entreprises/logs.html @@ -7,7 +7,7 @@ {% if logs.items %}
      {% for log in logs.items %} -
    • {{ log.date.strftime('%d %b %Hh%M') }}{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}
    • +
    • {{ log.date.strftime('%d %b %Hh%M') }}{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}
    • {% endfor %}
    diff --git a/app/templates/entreprises/logs_entreprise.html b/app/templates/entreprises/logs_entreprise.html index 7d213187..cebea040 100644 --- a/app/templates/entreprises/logs_entreprise.html +++ b/app/templates/entreprises/logs_entreprise.html @@ -7,7 +7,7 @@ {% if logs.items %}
      {% for log in logs.items %} -
    • {{ log.date.strftime('%d %b %Hh%M') }}{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}
    • +
    • {{ log.date.strftime('%d %b %Hh%M') }}{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}
    • {% endfor %}