From 5684f57ca5c0388ed4d7b580a460d66448a5af18 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Mon, 27 Dec 2021 11:48:58 +0100 Subject: [PATCH] export liste contacts --- app/entreprises/forms.py | 29 +++++---- app/entreprises/models.py | 17 ++++- app/entreprises/routes.py | 64 +++++++++++++------ app/templates/entreprises/contacts.html | 6 +- app/templates/entreprises/entreprises.html | 1 - ..._creation_table_relations_entreprrises.py} | 4 +- 6 files changed, 83 insertions(+), 38 deletions(-) rename migrations/versions/{f3b62d64efa3_creation_tables_relations_entreprises.py => 2dfafee725ae_creation_table_relations_entreprrises.py} (99%) diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 502db347d..1965c9cba 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -1,8 +1,7 @@ from flask import flash -from markupsafe import Markup -from flask.app import Flask -import requests, re from flask_wtf import FlaskForm +from markupsafe import Markup +import requests, re from wtforms import StringField, SubmitField, TextAreaField, SelectField, FileField, HiddenField from wtforms.fields.html5 import EmailField, DateField from wtforms.validators import ValidationError, DataRequired, Email @@ -20,13 +19,15 @@ class EntrepriseCreationForm(FlaskForm): adresse = StringField("Adresse de l'entreprise", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) codepostal = StringField("Code postal de l'entreprise", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) ville = StringField("Ville de l'entreprise", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) - pays = StringField("Pays de l'entreprise", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) + pays = StringField("Pays de l'entreprise", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)], render_kw={"style": "margin-bottom: 50px;"}) nom_contact = StringField("Nom du contact", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) prenom_contact = StringField("Prénom du contact", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) telephone = StringField("Téléphone du contact", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) mail = EmailField("Mail du contact", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE), Email(message="Adresse e-mail invalide")]) - submit = SubmitField("Envoyer") + poste = StringField("Poste du contact", validators=[]) + service = StringField("Service du contact", validators=[]) + submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) def validate_siret(self, siret): siret = siret.data.strip() @@ -47,7 +48,7 @@ class EntrepriseModificationForm(FlaskForm): codepostal = StringField("Code postal", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) ville = StringField("Ville", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) pays = StringField("Pays", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) - submit = SubmitField("Modifier") + submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"}) class OffreCreationForm(FlaskForm): intitule = StringField("Intitulé", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) @@ -56,7 +57,7 @@ class OffreCreationForm(FlaskForm): missions = TextAreaField("Missions", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) duree = StringField("Durée", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) ficher = FileField("Fichier", validators=[]) - submit = SubmitField("Envoyer") + submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) class OffreModificationForm(FlaskForm): intitule = StringField("Intitulé", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) @@ -64,7 +65,7 @@ class OffreModificationForm(FlaskForm): type_offre = SelectField("Type de l'offre", choices=[('Stage'), ('Alternance')], validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) missions = TextAreaField("Missions", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) duree = StringField("Durée", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) - submit = SubmitField("Modifier") + submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"}) class ContactCreationForm(FlaskForm): hidden_entreprise_id = HiddenField() @@ -74,7 +75,7 @@ class ContactCreationForm(FlaskForm): mail = EmailField("Mail", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE), Email(message="Adresse e-mail invalide")]) poste = StringField("Poste", validators=[]) service = StringField("Service", validators=[]) - submit = SubmitField("Envoyer") + submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) def validate(self): rv = FlaskForm.validate(self) @@ -99,14 +100,16 @@ class ContactModificationForm(FlaskForm): prenom = StringField("Prénom", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) telephone = StringField("Téléphone", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) mail = EmailField("Mail", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE), Email(message="Adresse e-mail invalide")]) - submit = SubmitField("Modifier") + poste = StringField("Poste", validators=[]) + service = StringField("Service", validators=[]) + submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"}) class HistoriqueCreationForm(FlaskForm): etudiant = StringField("Étudiant", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)], render_kw={"placeholder": "Tapez le nom de l'étudiant puis selectionnez"}) type_offre = SelectField("Type de l'offre", choices=[('Stage'), ('Alternance')], validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) date_debut = DateField("Date début", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) date_fin = DateField("Date fin", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) - submit = SubmitField("Envoyer") + submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) def validate(self): rv = FlaskForm.validate(self) @@ -128,7 +131,7 @@ class HistoriqueCreationForm(FlaskForm): class EnvoiOffreForm(FlaskForm): responsable = StringField("Responsable de formation", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) - submit = SubmitField("Envoyer") + submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) def validate_responsable(self, responsable): responsable_data = responsable.data.upper().strip() @@ -138,4 +141,4 @@ class EnvoiOffreForm(FlaskForm): raise ValidationError("Champ incorrect (selectionnez dans la liste)") class SuppressionConfirmationForm(FlaskForm): - submit = SubmitField("Supprimer") \ No newline at end of file + submit = SubmitField("Supprimer", render_kw={"style": "margin-bottom: 10px;"}) \ No newline at end of file diff --git a/app/entreprises/models.py b/app/entreprises/models.py index d88a88127..f31c11af8 100644 --- a/app/entreprises/models.py +++ b/app/entreprises/models.py @@ -12,30 +12,41 @@ class Entreprise(db.Model): contacts = db.relationship('EntrepriseContact', backref='entreprise', lazy='dynamic', cascade="all, delete-orphan") offres = db.relationship('EntrepriseOffre', backref='entreprise', lazy='dynamic', cascade="all, delete-orphan") - def to_dict(self): + def to_dict_export(self): return { "siret": self.siret, "nom": self.nom, "adresse": self.adresse, "codepostal": self.codepostal, + "ville": self.ville, "pays": self.pays } class EntrepriseContact(db.Model): __tablename__ = "entreprise_contact" id = db.Column(db.Integer, primary_key=True) - entreprise_id = db.Column(db.Integer, db.ForeignKey("entreprises.id")) + entreprise_id = db.Column(db.Integer, db.ForeignKey("entreprises.id", ondelete="cascade")) nom = db.Column(db.Text) prenom = db.Column(db.Text) telephone = db.Column(db.Text) mail = db.Column(db.Text) poste = db.Column(db.Text) service = db.Column(db.Text) + + def to_dict_export(self): + return { + "nom": self.nom, + "prenom": self.prenom, + "telephone": self.telephone, + "mail": self.mail, + "poste": self.poste, + "service": self.service + } class EntrepriseOffre(db.Model): __tablename__ = "entreprise_offre" id = db.Column(db.Integer, primary_key=True) - entreprise_id = db.Column(db.Integer, db.ForeignKey("entreprises.id")) + entreprise_id = db.Column(db.Integer, db.ForeignKey("entreprises.id", ondelete="cascade")) date_ajout = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) intitule = db.Column(db.Text) description = db.Column(db.Text) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 118e7e032..b85c9938f 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -1,9 +1,9 @@ -from flask import render_template, redirect, url_for, request, flash, send_file +from flask import render_template, redirect, url_for, request, flash, send_file, abort from flask.json import jsonify from flask_login import current_user + from app.decorators import permission_required -from app.scodoc.sco_permissions import Permission from app.entreprises import LOGS_LEN from app.entreprises.forms import ( EntrepriseCreationForm, @@ -30,9 +30,11 @@ from app.models import ( ) from app.auth.models import User from app.scodoc.sco_find_etud import search_etud_by_name -from app import db +from app.scodoc.sco_permissions import Permission from app.scodoc import sco_etud, sco_excel import app.scodoc.sco_utils as scu + +from app import db from sqlalchemy import text @bp.route("/", methods=["GET"]) @@ -85,7 +87,9 @@ def add_entreprise(): nom=form.nom_contact.data.strip(), prenom=form.prenom_contact.data.strip(), telephone=form.telephone.data.strip(), - mail=form.mail.data.strip() + mail=form.mail.data.strip(), + poste=form.poste.data.strip(), + service=form.service.data.strip() ) db.session.add(contact) nom_entreprise = f"{entreprise.nom}" @@ -409,17 +413,41 @@ def json_responsables(): @bp.route("/export_entreprises") def export_entreprises(): entreprises = Entreprise.query.all() - keys=[ - "siret", - "nom", - "adresse", - "ville", - "codepostal", - "pays" - ] - titles = keys[:] - L = [[entreprise.to_dict().get(k, "") for k in keys] for entreprise in 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) \ No newline at end of file + if entreprises: + keys=[ + "siret", + "nom", + "adresse", + "ville", + "codepostal", + "pays" + ] + titles = keys[:] + L = [[entreprise.to_dict_export().get(k, "") for k in keys] for entreprise in 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) + else: + abort(404) + +@bp.route("/export_contacts") +def export_contacts(): + contacts = EntrepriseContact.query.all() + if contacts: + keys=[ + "nom", + "prenom", + "telephone", + "mail", + "poste", + "service" + ] + titles = keys[:] + L = [[contact.to_dict_export().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) \ No newline at end of file diff --git a/app/templates/entreprises/contacts.html b/app/templates/entreprises/contacts.html index fdf2a747b..87112710f 100644 --- a/app/templates/entreprises/contacts.html +++ b/app/templates/entreprises/contacts.html @@ -33,10 +33,14 @@ {% endfor %} -
{% else %}
Aucun contact présent dans la base
{% endif %} +
+ {% if contacts %} + Exporter la liste des contacts + {% endif %} +
{% endblock %} \ No newline at end of file diff --git a/app/templates/entreprises/entreprises.html b/app/templates/entreprises/entreprises.html index 62d1fc11a..a52f75805 100644 --- a/app/templates/entreprises/entreprises.html +++ b/app/templates/entreprises/entreprises.html @@ -47,7 +47,6 @@ {% endfor %} -
{% else %}
Aucune entreprise présent dans la base

diff --git a/migrations/versions/f3b62d64efa3_creation_tables_relations_entreprises.py b/migrations/versions/2dfafee725ae_creation_table_relations_entreprrises.py similarity index 99% rename from migrations/versions/f3b62d64efa3_creation_tables_relations_entreprises.py rename to migrations/versions/2dfafee725ae_creation_table_relations_entreprrises.py index 7d629e280..126147c27 100644 --- a/migrations/versions/f3b62d64efa3_creation_tables_relations_entreprises.py +++ b/migrations/versions/2dfafee725ae_creation_table_relations_entreprrises.py @@ -36,7 +36,7 @@ def upgrade(): sa.Column('date_fin', sa.Date(), nullable=True), sa.Column('formation_text', sa.Text(), nullable=True), sa.Column('formation_scodoc', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['entreprise_id'], ['entreprises.id'], ), + sa.ForeignKeyConstraint(['entreprise_id'], ['entreprises.id'], ondelete='cascade'), sa.PrimaryKeyConstraint('id') ) @@ -49,7 +49,7 @@ def upgrade(): sa.Column('type_offre', sa.Text(), nullable=True), sa.Column('missions', sa.Text(), nullable=True), sa.Column('duree', sa.Text(), nullable=True), - sa.ForeignKeyConstraint(['entreprise_id'], ['entreprises.id'], ), + sa.ForeignKeyConstraint(['entreprise_id'], ['entreprises.id'], ondelete='cascade'), sa.PrimaryKeyConstraint('id') )