From 178016b682c3767754454f0f44f02b4b78cb6e57 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Fri, 24 Dec 2021 16:07:36 +0100 Subject: [PATCH 1/5] =?UTF-8?q?ajout=20page=20offres,=20insertion=20en=20b?= =?UTF-8?q?ase=20des=20diff=C3=A9rents=20objets,=20enrichissement=20des=20?= =?UTF-8?q?champs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/entreprises/forms.py | 4 +++- app/entreprises/routes.py | 32 +++++++++++++++++++++---- app/templates/entreprises/contacts.html | 1 - app/templates/entreprises/offres.html | 28 ++++++++++++++++++++++ 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 app/templates/entreprises/offres.html diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index d2f3762b..502db347 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -67,11 +67,13 @@ class OffreModificationForm(FlaskForm): submit = SubmitField("Modifier") class ContactCreationForm(FlaskForm): + hidden_entreprise_id = HiddenField() nom = StringField("Nom", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) 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")]) - hidden_entreprise_id = HiddenField() + poste = StringField("Poste", validators=[]) + service = StringField("Service", validators=[]) submit = SubmitField("Envoyer") def validate(self): diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 1791199e..7570c806 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -21,7 +21,8 @@ from app.entreprises.models import ( EntrepriseOffre, EntrepriseContact, EntrepriseLog, - EntrepriseEtudiant + EntrepriseEtudiant, + EntrepriseEnvoiOffre ) from app.models import ( Identite @@ -52,10 +53,16 @@ def fiche_entreprise(id): contacts = entreprise.contacts logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).filter_by(object=id).limit(LOGS_LEN).all() historique = db.session.query(EntrepriseEtudiant, Identite).order_by(EntrepriseEtudiant.date_debut.desc()).\ - filter_by(entreprise_id=id).\ + filter(EntrepriseEtudiant.entreprise_id == id).\ join(Identite, Identite.id == EntrepriseEtudiant.etudid).all() return render_template("entreprises/fiche_entreprise.html", title=("Fiche entreprise"), entreprise=entreprise, contacts=contacts, offres=offres, logs=logs, historique=historique) +@bp.route("/offres", methods=["GET"]) +def offres(): + offres_recus = db.session.query(EntrepriseEnvoiOffre, EntrepriseOffre).filter(EntrepriseEnvoiOffre.user_id == current_user.id).\ + join(EntrepriseOffre, EntrepriseOffre.id == EntrepriseEnvoiOffre.offre_id).all() + return render_template("entreprises/offres.html", title=("Offres"), offres_recus=offres_recus) + @bp.route("/add_entreprise", methods=["GET", "POST"]) def add_entreprise(): form = EntrepriseCreationForm() @@ -245,7 +252,9 @@ def add_contact(id): nom=form.nom.data.strip(), prenom=form.prenom.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() ) log = EntrepriseLog( authenticated_user = current_user.user_name, @@ -268,6 +277,8 @@ def edit_contact(id): contact.prenom = form.prenom.data.strip() contact.telephone = form.telephone.data.strip() contact.mail = form.mail.data.strip() + contact.poste = form.poste.data.strip() + contact.service = form.service.data.strip() log = EntrepriseLog( authenticated_user = current_user.user_name, object = contact.entreprise_id, @@ -282,6 +293,8 @@ def edit_contact(id): form.prenom.data = contact.prenom form.telephone.data = contact.telephone form.mail.data = contact.mail + form.poste.data = contact.poste + form.service.data = contact.service return render_template("entreprises/form.html", title=("Modification contact"), form=form) @bp.route("/delete_contact/", methods=["GET", "POST"]) @@ -335,9 +348,20 @@ def add_historique(id): @bp.route("/envoyer_offre/", methods=["GET", "POST"]) def envoyer_offre(id): + offre = EntrepriseOffre.query.filter_by(id=id).first_or_404() form = EnvoiOffreForm() if form.validate_on_submit(): - print("tmp") # faire l'envoie du mail + responsable_data = form.responsable.data.upper().strip() + stm = text("SELECT id, UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')')) FROM \"user\" WHERE UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')'))=:responsable_data") + responsable = User.query.from_statement(stm).params(responsable_data=responsable_data).first() + envoi_offre = EntrepriseEnvoiOffre( + user_id = responsable.id, + offre_id = offre.id + ) + db.session.add(envoi_offre) + db.session.commit() + flash(f"L'offre a été envoyé à {responsable.get_nomplogin()}.") + return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise_id)) return render_template("entreprises/envoi_offre_form.html", title=("Envoyer une offre"), form=form) @bp.route("/etudiants") diff --git a/app/templates/entreprises/contacts.html b/app/templates/entreprises/contacts.html index 7f143333..fdf2a747 100644 --- a/app/templates/entreprises/contacts.html +++ b/app/templates/entreprises/contacts.html @@ -36,7 +36,6 @@
{% else %}
Aucun contact présent dans la base
-
{% endif %} diff --git a/app/templates/entreprises/offres.html b/app/templates/entreprises/offres.html new file mode 100644 index 00000000..717364b9 --- /dev/null +++ b/app/templates/entreprises/offres.html @@ -0,0 +1,28 @@ +{% extends 'base.html' %} + +{% block app_content %} +
+

{{ title }}

+ {% if offres_recus %} +
+
+ {% for offre in offres_recus %} +
+

+ Date envoi : {{ offre[0].date_envoi.strftime('%d/%m/%Y %H:%M') }}
+ Intitulé : {{ offre[1].intitule }}
+ Description : {{ offre[1].description }}
+ Type de l'offre : {{ offre[1].type_offre }}
+ Missions : {{ offre[1].missions }}
+ Durée : {{ offre[1].duree }}
+

+
+ {% endfor %} +
+
+ {% else %} +
Aucune offre reçue
+
+ {% endif %} +
+{% endblock %} \ No newline at end of file From 2a6d63cf03db2002d849e1527607f4940feffe99 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Fri, 24 Dec 2021 18:10:19 +0100 Subject: [PATCH 2/5] export liste entreprises --- app/entreprises/models.py | 9 +++++++ app/entreprises/routes.py | 28 ++++++++++++++++++---- app/templates/entreprises/entreprises.html | 7 +++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app/entreprises/models.py b/app/entreprises/models.py index ec963be2..d88a8812 100644 --- a/app/entreprises/models.py +++ b/app/entreprises/models.py @@ -12,6 +12,15 @@ 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): + return { + "siret": self.siret, + "nom": self.nom, + "adresse": self.adresse, + "codepostal": self.codepostal, + "pays": self.pays + } + class EntrepriseContact(db.Model): __tablename__ = "entreprise_contact" id = db.Column(db.Integer, primary_key=True) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 7570c806..118e7e03 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -1,9 +1,10 @@ -from flask import render_template, redirect, url_for, request, flash +from flask import render_template, redirect, url_for, request, flash, send_file from flask.json import jsonify from flask_login import current_user from app.decorators import permission_required -from app.entreprises import LOGS_LEN + from app.scodoc.sco_permissions import Permission +from app.entreprises import LOGS_LEN from app.entreprises.forms import ( EntrepriseCreationForm, EntrepriseModificationForm, @@ -30,7 +31,8 @@ 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 import sco_etud +from app.scodoc import sco_etud, sco_excel +import app.scodoc.sco_utils as scu from sqlalchemy import text @bp.route("/", methods=["GET"]) @@ -402,4 +404,22 @@ def json_responsables(): } list.append(content) content = {} - return jsonify(results=list) \ No newline at end of file + return jsonify(results=list) + +@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 diff --git a/app/templates/entreprises/entreprises.html b/app/templates/entreprises/entreprises.html index a33dd7e7..62d1fc11 100644 --- a/app/templates/entreprises/entreprises.html +++ b/app/templates/entreprises/entreprises.html @@ -53,6 +53,11 @@
{% endif %} - Ajouter une entreprise +
+ Ajouter une entreprise + {% if entreprises %} + Exporter la liste des entreprises + {% endif %} +
{% endblock %} \ No newline at end of file From 5684f57ca5c0388ed4d7b580a460d66448a5af18 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Mon, 27 Dec 2021 11:48:58 +0100 Subject: [PATCH 3/5] 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 502db347..1965c9cb 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 d88a8812..f31c11af 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 118e7e03..b85c9938 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 fdf2a747..87112710 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 62d1fc11..a52f7580 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 7d629e28..126147c2 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') ) From a7ef5d81c528f21cb05b13ac3a4e5761b9393672 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Mon, 27 Dec 2021 19:00:38 +0100 Subject: [PATCH 4/5] =?UTF-8?q?attacher=20un=20fichier=20(pdf,=20docx)=20?= =?UTF-8?q?=C3=A0=20une=20offre=20(stock=C3=A9s=20sur=20disque)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/entreprises/forms.py | 7 +-- app/entreprises/models.py | 1 + app/entreprises/routes.py | 45 ++++++++++++++----- app/templates/entreprises/_offre.html | 3 ++ app/templates/entreprises/entreprises.html | 2 +- ...e_creation_table_relations_entreprrises.py | 1 + 6 files changed, 43 insertions(+), 16 deletions(-) diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 1965c9cb..1ce49cc0 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -2,8 +2,9 @@ from flask import flash from flask_wtf import FlaskForm from markupsafe import Markup import requests, re -from wtforms import StringField, SubmitField, TextAreaField, SelectField, FileField, HiddenField +from wtforms import StringField, SubmitField, TextAreaField, SelectField, HiddenField from wtforms.fields.html5 import EmailField, DateField +from flask_wtf.file import FileField, FileAllowed from wtforms.validators import ValidationError, DataRequired, Email from app.entreprises.models import Entreprise, EntrepriseContact from app.models import Identite @@ -14,7 +15,7 @@ from sqlalchemy import text DATA_REQUIRED_ERROR_MESSAGE = "Ce champ est requis" class EntrepriseCreationForm(FlaskForm): - siret = StringField("SIRET", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)], render_kw={"placeholder": "Numéro composé de 14 chiffres"}) + siret = StringField("SIRET", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)], render_kw={"placeholder": "Numéro composé de 14 chiffres", "maxlength": "14"}) nom_entreprise = StringField("Nom de l'entreprise", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) 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)]) @@ -56,7 +57,7 @@ class OffreCreationForm(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)]) - ficher = FileField("Fichier", validators=[]) + fichier = FileField("Fichier", validators=[FileAllowed(['pdf', 'docx'], 'Fichier .pdf ou .docx uniquement')]) submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) class OffreModificationForm(FlaskForm): diff --git a/app/entreprises/models.py b/app/entreprises/models.py index f31c11af..3e2211e2 100644 --- a/app/entreprises/models.py +++ b/app/entreprises/models.py @@ -53,6 +53,7 @@ class EntrepriseOffre(db.Model): type_offre = db.Column(db.Text) missions = db.Column(db.Text) duree = db.Column(db.Text) + filename = db.Column(db.Text) class EntrepriseLog(db.Model): __tablename__ = "entreprise_log" diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index b85c9938..db11ff33 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -1,3 +1,6 @@ +import os +from config import Config + from flask import render_template, redirect, url_for, request, flash, send_file, abort from flask.json import jsonify from flask_login import current_user @@ -36,6 +39,7 @@ import app.scodoc.sco_utils as scu from app import db from sqlalchemy import text +from werkzeug.utils import secure_filename, send_from_directory @bp.route("/", methods=["GET"]) def index(): @@ -50,7 +54,7 @@ def contacts(): logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all() return render_template("entreprises/contacts.html", title=("Contacts"), contacts=contacts, logs=logs) -@bp.route("/fiche_entreprise/", methods=["GET"]) +@bp.route("/fiche_entreprise/", methods=["GET"]) def fiche_entreprise(id): entreprise = Entreprise.query.filter_by(id=id).first_or_404() offres = entreprise.offres @@ -103,7 +107,7 @@ def add_entreprise(): return redirect(url_for("entreprises.index")) return render_template("entreprises/ajout_entreprise.html", title=("Ajout entreprise + contact"), form=form) -@bp.route("/edit_entreprise/", methods=["GET", "POST"]) +@bp.route("/edit_entreprise/", methods=["GET", "POST"]) def edit_entreprise(id): entreprise = Entreprise.query.filter_by(id=id).first_or_404() form = EntrepriseModificationForm() @@ -161,7 +165,7 @@ def edit_entreprise(id): form.pays.data = entreprise.pays return render_template("entreprises/form.html", title=("Modification entreprise"), form=form) -@bp.route("/delete_entreprise/", methods=["GET", "POST"]) +@bp.route("/delete_entreprise/", methods=["GET", "POST"]) def delete_entreprise(id): entreprise = Entreprise.query.filter_by(id=id).first_or_404() form = SuppressionConfirmationForm() @@ -178,7 +182,7 @@ def delete_entreprise(id): return redirect(url_for("entreprises.index")) return render_template("entreprises/delete_confirmation.html", title=("Supression entreprise"), form=form) -@bp.route("/add_offre/", methods=["GET", "POST"]) +@bp.route("/add_offre/", methods=["GET", "POST"]) def add_offre(id): entreprise = Entreprise.query.filter_by(id=id).first_or_404() form = OffreCreationForm() @@ -191,19 +195,29 @@ def add_offre(id): missions=form.missions.data.strip(), duree=form.duree.data.strip() ) + db.session.add(offre) + db.session.commit() + if form.fichier.data is not None: + db.session.refresh(offre) + path = os.path.join(Config.SCODOC_VAR_DIR, "entreprises", f"{entreprise.id}", f"{offre.id}") + os.makedirs(path) + file = form.fichier.data + filename = secure_filename(file.filename) + file.save(os.path.join(path, filename)) + offre.offre_filename = f"{filename}" + db.session.commit() log = EntrepriseLog( authenticated_user = current_user.user_name, object = entreprise.id, text = "Création d'une offre", ) db.session.add(log) - db.session.add(offre) db.session.commit() flash("L'offre a été ajouté à la fiche entreprise.") return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) return render_template("entreprises/form.html", title=("Ajout offre"), form=form) -@bp.route("/edit_offre/", methods=["GET", "POST"]) +@bp.route("/edit_offre/", methods=["GET", "POST"]) def edit_offre(id): offre = EntrepriseOffre.query.filter_by(id=id).first_or_404() form = OffreModificationForm() @@ -230,7 +244,7 @@ def edit_offre(id): form.duree.data = offre.duree return render_template("entreprises/form.html", title=("Modification offre"), form=form) -@bp.route("/delete_offre/", methods=["GET", "POST"]) +@bp.route("/delete_offre/", methods=["GET", "POST"]) def delete_offre(id): offre = EntrepriseOffre.query.filter_by(id=id).first_or_404() entreprise_id = offre.entreprise.id @@ -248,7 +262,7 @@ def delete_offre(id): return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id)) return render_template("entreprises/delete_confirmation.html", title=("Supression offre"), form=form) -@bp.route("/add_contact/", methods=["GET", "POST"]) +@bp.route("/add_contact/", methods=["GET", "POST"]) def add_contact(id): entreprise = Entreprise.query.filter_by(id=id).first_or_404() form = ContactCreationForm(hidden_entreprise_id=entreprise.id) @@ -274,7 +288,7 @@ def add_contact(id): return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) return render_template("entreprises/form.html", title=("Ajout contact"), form=form) -@bp.route("/edit_contact/", methods=["GET", "POST"]) +@bp.route("/edit_contact/", methods=["GET", "POST"]) def edit_contact(id): contact = EntrepriseContact.query.filter_by(id=id).first_or_404() form = ContactModificationForm() @@ -303,7 +317,7 @@ def edit_contact(id): form.service.data = contact.service return render_template("entreprises/form.html", title=("Modification contact"), form=form) -@bp.route("/delete_contact/", methods=["GET", "POST"]) +@bp.route("/delete_contact/", methods=["GET", "POST"]) def delete_contact(id): contact = EntrepriseContact.query.filter_by(id=id).first_or_404() entreprise_id = contact.entreprise.id @@ -326,7 +340,7 @@ def delete_contact(id): return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id)) return render_template("entreprises/delete_confirmation.html", title=("Supression contact"), form=form) -@bp.route("/add_historique/", methods=["GET", "POST"]) +@bp.route("/add_historique/", methods=["GET", "POST"]) def add_historique(id): entreprise = Entreprise.query.filter_by(id=id).first_or_404() form = HistoriqueCreationForm() @@ -352,7 +366,7 @@ def add_historique(id): return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) return render_template("entreprises/ajout_historique.html", title=("Ajout historique"), form=form) -@bp.route("/envoyer_offre/", methods=["GET", "POST"]) +@bp.route("/envoyer_offre/", methods=["GET", "POST"]) def envoyer_offre(id): offre = EntrepriseOffre.query.filter_by(id=id).first_or_404() form = EnvoiOffreForm() @@ -449,5 +463,12 @@ def export_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("/download_offre///") +def download_offre(entreprise_id, offre_id, filename): + if os.path.isfile(os.path.join(Config.SCODOC_VAR_DIR, "entreprises", f"{entreprise_id}", f"{offre_id}", f"{filename}")): + return send_file(os.path.join(Config.SCODOC_VAR_DIR, "entreprises", f"{entreprise_id}", f"{offre_id}", f"{filename}"), as_attachment=True) else: abort(404) \ No newline at end of file diff --git a/app/templates/entreprises/_offre.html b/app/templates/entreprises/_offre.html index e64f3c16..581ad7af 100644 --- a/app/templates/entreprises/_offre.html +++ b/app/templates/entreprises/_offre.html @@ -5,6 +5,9 @@ Type de l'offre : {{ offre.type_offre }}
Missions : {{ offre.missions }}
Durée : {{ offre.duree }}
+ {% if offre.offre_filename %} + Téléchargez le fichier de l'offre + {% endif %}

diff --git a/app/templates/entreprises/entreprises.html b/app/templates/entreprises/entreprises.html index a52f7580..85854efb 100644 --- a/app/templates/entreprises/entreprises.html +++ b/app/templates/entreprises/entreprises.html @@ -38,7 +38,7 @@ Action -