import contacts, nettoyage

This commit is contained in:
Arthur ZHU 2022-02-10 21:17:22 +01:00
parent c983ed6d99
commit 7858baa841
12 changed files with 254 additions and 97 deletions

View File

@ -22,7 +22,7 @@ def format_nom(s):
@bp.app_template_filter() @bp.app_template_filter()
def get_nomcomplet(s): def get_nomcomplet_by_username(s):
user = User.query.filter_by(user_name=s).first() user = User.query.filter_by(user_name=s).first()
return user.get_nomcomplet() return user.get_nomcomplet()

View File

@ -366,7 +366,7 @@ class ValidationConfirmationForm(FlaskForm):
submit = SubmitField("Valider", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Valider", render_kw={"style": "margin-bottom: 10px;"})
class ImportEntreprisesForm(FlaskForm): class ImportForm(FlaskForm):
fichier = FileField( fichier = FileField(
"Fichier", "Fichier",
validators=[ validators=[

View File

@ -29,7 +29,7 @@ class Entreprise(db.Model):
"siret": self.siret, "siret": self.siret,
"nom": self.nom, "nom": self.nom,
"adresse": self.adresse, "adresse": self.adresse,
"codepostal": self.codepostal, "code_postal": self.codepostal,
"ville": self.ville, "ville": self.ville,
"pays": self.pays, "pays": self.pays,
} }
@ -56,23 +56,7 @@ class EntrepriseContact(db.Model):
"mail": self.mail, "mail": self.mail,
"poste": self.poste, "poste": self.poste,
"service": self.service, "service": self.service,
} "entreprise_id": self.entreprise_id,
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,
} }

View File

@ -25,7 +25,7 @@ from app.entreprises.forms import (
EnvoiOffreForm, EnvoiOffreForm,
AjoutFichierForm, AjoutFichierForm,
ValidationConfirmationForm, ValidationConfirmationForm,
ImportEntreprisesForm, ImportForm,
) )
from app.entreprises import bp from app.entreprises import bp
from app.entreprises.models import ( from app.entreprises.models import (
@ -130,30 +130,30 @@ def fiche_entreprise(id):
les offres de l'entreprise. les offres de l'entreprise.
""" """
entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404() entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404()
offres = entreprise.offres
offres_with_files = [] offres_with_files = []
for offre in offres: for offre in entreprise.offres:
if date.today() > offre.expiration_date: if date.today() < offre.expiration_date:
break files = []
files = [] path = os.path.join(
path = os.path.join( Config.SCODOC_VAR_DIR,
Config.SCODOC_VAR_DIR, "entreprises",
"entreprises", f"{offre.entreprise_id}",
f"{offre.entreprise_id}", f"{offre.id}",
f"{offre.id}", )
) if os.path.exists(path):
if os.path.exists(path): for dir in glob.glob(
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]"
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}/*"):
for file in glob.glob(f"{dir}/*"): file = [os.path.basename(dir), os.path.basename(file)]
file = [os.path.basename(dir), os.path.basename(file)] files.append(file)
files.append(file) offre_depts = EntrepriseOffreDepartement.query.filter_by(
offre_depts = EntrepriseOffreDepartement.query.filter_by( offre_id=offre.id
offre_id=offre.id ).all()
).all() offres_with_files.append([offre, files, offre_depts])
offres_with_files.append([offre, files, offre_depts]) contacts = []
contacts = entreprise.contacts for contact in entreprise.contacts:
contacts.append(contact)
logs = ( logs = (
EntrepriseLog.query.order_by(EntrepriseLog.date.desc()) EntrepriseLog.query.order_by(EntrepriseLog.date.desc())
.filter_by(object=id) .filter_by(object=id)
@ -800,8 +800,8 @@ def json_etudiants():
term = request.args.get("term").strip() term = request.args.get("term").strip()
etudiants = Identite.query.filter(Identite.nom.ilike(f"%{term}%")).all() etudiants = Identite.query.filter(Identite.nom.ilike(f"%{term}%")).all()
list = [] list = []
content = {}
for etudiant in etudiants: for etudiant in etudiants:
content = {}
value = f"{sco_etud.format_nom(etudiant.nom)} {sco_etud.format_prenom(etudiant.prenom)}" value = f"{sco_etud.format_nom(etudiant.nom)} {sco_etud.format_prenom(etudiant.prenom)}"
if etudiant.inscription_courante() is not None: if etudiant.inscription_courante() is not None:
content = { content = {
@ -812,7 +812,6 @@ def json_etudiants():
else: else:
content = {"id": f"{etudiant.id}", "value": value} content = {"id": f"{etudiant.id}", "value": value}
list.append(content) list.append(content)
content = {}
return jsonify(results=list) 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) User.nom.ilike(f"%{term}%"), User.nom.is_not(None), User.prenom.is_not(None)
).all() ).all()
list = [] list = []
content = {}
for responsable in responsables: for responsable in responsables:
content = {}
value = f"{responsable.get_nomplogin()}" value = f"{responsable.get_nomplogin()}"
content = {"id": f"{responsable.id}", "value": value, "info": ""} content = {"id": f"{responsable.id}", "value": value, "info": ""}
list.append(content) list.append(content)
content = {}
return jsonify(results=list) return jsonify(results=list)
@ -846,13 +845,13 @@ def export_entreprises():
""" """
entreprises = Entreprise.query.filter_by(visible=True).all() entreprises = Entreprise.query.filter_by(visible=True).all()
if entreprises: if entreprises:
keys = ["siret", "nom", "adresse", "ville", "codepostal", "pays"] keys = ["siret", "nom", "adresse", "ville", "code_postal", "pays"]
titles = keys[:] titles = keys[:]
L = [ L = [
[entreprise.to_dict().get(k, "") for k in keys] [entreprise.to_dict().get(k, "") for k in keys]
for entreprise in entreprises for entreprise in entreprises
] ]
title = "entreprises" title = "Entreprises"
xlsx = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title) xlsx = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title)
filename = title filename = title
return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE) 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") @bp.route("/get_import_entreprises_file_sample")
@permission_required(Permission.RelationsEntreprisesExport) @permission_required(Permission.RelationsEntreprisesExport)
def get_import_entreprises_file_sample(): def get_import_entreprises_file_sample():
"""
Permet de récupérer un fichier pour pouvoir importer des entreprises
"""
keys = [ keys = [
"siret", "siret",
"nom_entreprise", "nom_entreprise",
@ -900,7 +902,7 @@ def import_entreprises():
""" """
Permet d'importer des entreprises a l'aide d'un fichier excel (.xlsx) Permet d'importer des entreprises a l'aide d'un fichier excel (.xlsx)
""" """
form = ImportEntreprisesForm() form = ImportForm()
if form.validate_on_submit(): if form.validate_on_submit():
path = os.path.join(Config.SCODOC_VAR_DIR, "tmp") path = os.path.join(Config.SCODOC_VAR_DIR, "tmp")
file = form.fichier.data file = form.fichier.data
@ -912,7 +914,14 @@ def import_entreprises():
entreprises_import = [] entreprises_import = []
siret_list = [] siret_list = []
ligne = 0 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:]: for entreprise_data in data[1][1:]:
ligne += 1 ligne += 1
if ( if (
@ -972,30 +981,6 @@ def export_contacts():
.filter_by(visible=True) .filter_by(visible=True)
.all() .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: if contacts:
keys = [ keys = [
"nom", "nom",
@ -1004,18 +989,11 @@ def export_contacts_bis():
"mail", "mail",
"poste", "poste",
"service", "service",
"nom_entreprise", "entreprise_id",
"siret",
"adresse_entreprise",
"ville",
"codepostal",
"pays",
] ]
titles = keys[:] titles = keys[:]
L = [ L = [[contact.to_dict().get(k, "") for k in keys] for contact in contacts]
[contact.to_dict_export().get(k, "") for k in keys] for contact in contacts title = "Contacts"
]
title = "contacts"
xlsx = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title) xlsx = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title)
filename = title filename = title
return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE) return scu.send_file(xlsx, filename, scu.XLSX_SUFFIX, scu.XLSX_MIMETYPE)
@ -1023,6 +1001,130 @@ def export_contacts_bis():
abort(404) 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( @bp.route(
"/get_offre_file/<int:entreprise_id>/<int:offre_id>/<string:filedir>/<string:filename>" "/get_offre_file/<int:entreprise_id>/<int:offre_id>/<string:filedir>/<string:filename>"
) )

View File

@ -9,20 +9,20 @@
<h3>Dernières opérations <a href="{{ url_for('entreprises.logs') }}">Voir tout</a></h3> <h3>Dernières opérations <a href="{{ url_for('entreprises.logs') }}">Voir tout</a></h3>
<ul> <ul>
{% for log in logs %} {% for log in logs %}
<li><span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span><span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}</span></li> <li><span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span><span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}</span></li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>
{% endif %} {% endif %}
{% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) and contacts.items %}
<div class="container boutons"> <div class="container boutons">
{% if contacts %} {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) %}
<a class="btn btn-default" href="{{ url_for('entreprises.import_contacts') }}">Importer des contacts</a>
{% endif %}
{% if current_user.has_permission(current_user.Permission.RelationsEntreprisesExport, None) and contacts.items %}
<a class="btn btn-default" href="{{ url_for('entreprises.export_contacts') }}">Exporter la liste des contacts</a> <a class="btn btn-default" href="{{ url_for('entreprises.export_contacts') }}">Exporter la liste des contacts</a>
<a class="btn btn-default" href="{{ url_for('entreprises.export_contacts_bis') }}">Exporter la liste des contacts avec leur entreprise</a>
{% endif %} {% endif %}
</div> </div>
{% endif %}
<div class="container"> <div class="container">
<h1>Liste des contacts</h1> <h1>Liste des contacts</h1>

View File

@ -9,7 +9,7 @@
<h3>Dernières opérations <a href="{{ url_for('entreprises.logs') }}">Voir tout</a></h3> <h3>Dernières opérations <a href="{{ url_for('entreprises.logs') }}">Voir tout</a></h3>
<ul> <ul>
{% for log in logs %} {% for log in logs %}
<li><span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span><span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}</span></li> <li><span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span><span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}</span></li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>
@ -33,7 +33,7 @@
<h1>Liste des entreprises</h1> <h1>Liste des entreprises</h1>
{% if entreprises.items %} {% if entreprises.items %}
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-bordered table-hover"> <table class="table table-bordered table-hover" style="margin-bottom:60px;">
<tr> <tr>
<th>SIRET</th> <th>SIRET</th>
<th>Nom</th> <th>Nom</th>

View File

@ -9,7 +9,7 @@
<h3>Dernières opérations</h3> <h3>Dernières opérations</h3>
<ul> <ul>
{% for log in logs %} {% for log in logs %}
<li><span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span><span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}</span></li> <li><span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span><span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}</span></li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>

View File

@ -9,7 +9,7 @@
{% for log in logs %} {% for log in logs %}
<li> <li>
<span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span> <span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span>
<span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}</span> <span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}</span>
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>

View File

@ -0,0 +1,59 @@
{# -*- mode: jinja-html -*- #}
{% extends 'base.html' %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block styles %}
{{super()}}
{% endblock %}
{% block app_content %}
<h1>Importation contacts</h1>
<br>
<div>
<a href="{{ url_for('entreprises.get_import_contacts_file_sample') }}">Obtenir la feuille excel à remplir</a>
</div>
<br>
<div class="row">
<div class="col-md-4">
{{ wtf.quick_form(form, novalidate=True) }}
</div>
</div>
{% if not contacts_import %}
<table class="table">
<thead><tr><td><b>Attribut</b></td><td><b>Type</b></td><td><b>Description</b></td></tr></thead>
<tr><td>nom</td><td>text</td><td>nom du contact</td></tr>
<tr><td>prenom</td><td>text</td><td>prenom du contact</td></tr>
<tr><td>telephone</td><td>text</td><td>telephone du contact</td></tr>
<tr><td>mail</td><td>text</td><td>mail du contact</td></tr>
<tr><td>poste</td><td>text</td><td>poste du contact</td></tr>
<tr><td>service</td><td>text</td><td>service dans lequel travaille le contact</td></tr>
<tr><td>entreprise_id</td><td>integer</td><td>l'id de l'entreprise</td></tr>
</table>
{% endif %}
{% if contacts_import %}
<br><div>Importation de {{ contacts_import|length }} contact(s)</div>
{% for contact in contacts_import %}
<div class="contact">
<div>
Nom : {{ contact.nom }}<br>
Prénom : {{ contact.prenom }}<br>
{% if contact.telephone %}
Téléphone : {{ contact.telephone }}<br>
{% endif %}
{% if contact.mail %}
Mail : {{ contact.mail }}<br>
{% endif %}
{% if contact.poste %}
Poste : {{ contact.poste }}<br>
{% endif %}
{% if contact.service %}
Service : {{ contact.service }}<br>
{% endif %}
<a href="{{ url_for('entreprises.fiche_entreprise', id=contact.entreprise_id )}}">lien vers l'entreprise</a>
</div>
</div>
{% endfor %}
{% endif %}
{% endblock %}

View File

@ -19,6 +19,18 @@
</div> </div>
</div> </div>
{% if not entreprises_import %}
<table class="table">
<thead><tr><td><b>Attribut</b></td><td><b>Type</b></td><td><b>Description</b></td></tr></thead>
<tr><td>siret</td><td>text</td><td>siret de l'entreprise</td></tr>
<tr><td>nom</td><td>text</td><td>nom de l'entreprise</td></tr>
<tr><td>adresse</td><td>text</td><td>adresse de l'entreprise</td></tr>
<tr><td>ville</td><td>text</td><td>ville de l'entreprise</td></tr>
<tr><td>code_postal</td><td>text</td><td>code postal de l'entreprise</td></tr>
<tr><td>pays</td><td>text</td><td>pays de l'entreprise</td></tr>
</table>
{% endif %}
{% if entreprises_import %} {% if entreprises_import %}
<br><div>Importation de {{ entreprises_import|length }} entreprise(s)</div> <br><div>Importation de {{ entreprises_import|length }} entreprise(s)</div>
{% for entreprise in entreprises_import %} {% for entreprise in entreprises_import %}

View File

@ -7,7 +7,7 @@
{% if logs.items %} {% if logs.items %}
<ul> <ul>
{% for log in logs.items %} {% for log in logs.items %}
<li><span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span><span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}</span></li> <li><span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span><span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}</span></li>
{% endfor %} {% endfor %}
</ul> </ul>

View File

@ -7,7 +7,7 @@
{% if logs.items %} {% if logs.items %}
<ul> <ul>
{% for log in logs.items %} {% for log in logs.items %}
<li><span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span><span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet }}</span></li> <li><span style="margin-right: 10px;">{{ log.date.strftime('%d %b %Hh%M') }}</span><span>{{ log.text|safe }} par {{ log.authenticated_user|get_nomcomplet_by_username }}</span></li>
{% endfor %} {% endfor %}
</ul> </ul>