From a7ef5d81c528f21cb05b13ac3a4e5761b9393672 Mon Sep 17 00:00:00 2001
From: Arthur ZHU
Date: Mon, 27 Dec 2021 19:00:38 +0100
Subject: [PATCH] =?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
-