attacher plusieurs fichiers a une offre

This commit is contained in:
Arthur ZHU 2021-12-28 21:20:50 +01:00
parent 38d4746f74
commit 8dee5842e1
8 changed files with 521 additions and 222 deletions

View File

@ -9,17 +9,21 @@ bp = Blueprint("entreprises", __name__)
LOGS_LEN = 10 LOGS_LEN = 10
@bp.app_template_filter() @bp.app_template_filter()
def format_prenom(s): def format_prenom(s):
return sco_etud.format_prenom(s) return sco_etud.format_prenom(s)
@bp.app_template_filter() @bp.app_template_filter()
def format_nom(s): def format_nom(s):
return sco_etud.format_nom(s) return sco_etud.format_nom(s)
@bp.app_template_filter() @bp.app_template_filter()
def get_nomcomplet(s): def get_nomcomplet(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()
from app.entreprises import routes from app.entreprises import routes

View File

@ -4,7 +4,7 @@ from markupsafe import Markup
import requests, re import requests, re
from wtforms import StringField, SubmitField, TextAreaField, SelectField, HiddenField from wtforms import StringField, SubmitField, TextAreaField, SelectField, HiddenField
from wtforms.fields.html5 import EmailField, DateField from wtforms.fields.html5 import EmailField, DateField
from flask_wtf.file import FileField, FileAllowed from flask_wtf.file import FileField, FileAllowed, FileRequired
from wtforms.validators import ValidationError, DataRequired, Email from wtforms.validators import ValidationError, DataRequired, Email
from app.entreprises.models import Entreprise, EntrepriseContact from app.entreprises.models import Entreprise, EntrepriseContact
from app.models import Identite from app.models import Identite
@ -12,20 +12,55 @@ from app.auth.models import User
from app.scodoc import sco_etud from app.scodoc import sco_etud
from sqlalchemy import text from sqlalchemy import text
DATA_REQUIRED_ERROR_MESSAGE = "Ce champ est requis" CHAMP_REQUIS = "Ce champ est requis"
class EntrepriseCreationForm(FlaskForm): class EntrepriseCreationForm(FlaskForm):
siret = StringField("SIRET", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)], render_kw={"placeholder": "Numéro composé de 14 chiffres", "maxlength": "14"}) siret = StringField(
nom_entreprise = StringField("Nom de l'entreprise", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) "SIRET",
adresse = StringField("Adresse de l'entreprise", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) validators=[DataRequired(message=CHAMP_REQUIS)],
codepostal = StringField("Code postal de l'entreprise", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) render_kw={"placeholder": "Numéro composé de 14 chiffres", "maxlength": "14"},
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)], render_kw={"style": "margin-bottom: 50px;"}) nom_entreprise = StringField(
"Nom de l'entreprise",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
adresse = StringField(
"Adresse de l'entreprise",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
codepostal = StringField(
"Code postal de l'entreprise",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
ville = StringField(
"Ville de l'entreprise",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
pays = StringField(
"Pays de l'entreprise",
validators=[DataRequired(message=CHAMP_REQUIS)],
render_kw={"style": "margin-bottom: 50px;"},
)
nom_contact = StringField("Nom du contact", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) nom_contact = StringField(
prenom_contact = StringField("Prénom du contact", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) "Nom du contact", validators=[DataRequired(message=CHAMP_REQUIS)]
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")]) prenom_contact = StringField(
"Prénom du contact",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
telephone = StringField(
"Téléphone du contact",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
mail = EmailField(
"Mail du contact",
validators=[
DataRequired(message=CHAMP_REQUIS),
Email(message="Adresse e-mail invalide"),
],
)
poste = StringField("Poste du contact", validators=[]) poste = StringField("Poste du contact", validators=[])
service = StringField("Service du contact", validators=[]) service = StringField("Service du contact", validators=[])
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"})
@ -34,46 +69,82 @@ class EntrepriseCreationForm(FlaskForm):
siret = siret.data.strip() siret = siret.data.strip()
if re.match("^\d{14}$", siret) == None: if re.match("^\d{14}$", siret) == None:
raise ValidationError("Format incorrect") raise ValidationError("Format incorrect")
req = requests.get(f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}") req = requests.get(
f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}"
)
if req.status_code != 200: if req.status_code != 200:
raise ValidationError("SIRET inexistant") raise ValidationError("SIRET inexistant")
entreprise = Entreprise.query.filter_by(siret=siret).first() entreprise = Entreprise.query.filter_by(siret=siret).first()
if entreprise is not None: if entreprise is not None:
lien = f"<a href=\"/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}\">ici</a>" lien = f'<a href="/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}">ici</a>'
raise ValidationError(Markup(f"Entreprise déjà présent, lien vers la fiche : {lien}")) raise ValidationError(
Markup(f"Entreprise déjà présent, lien vers la fiche : {lien}")
)
class EntrepriseModificationForm(FlaskForm): class EntrepriseModificationForm(FlaskForm):
siret = StringField("SIRET", validators=[], render_kw={"disabled": ""}) siret = StringField("SIRET", validators=[], render_kw={"disabled": ""})
nom = StringField("Nom de l'entreprise", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) nom = StringField(
adresse = StringField("Adresse", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) "Nom de l'entreprise",
codepostal = StringField("Code postal", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) validators=[DataRequired(message=CHAMP_REQUIS)],
ville = StringField("Ville", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) )
pays = StringField("Pays", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) adresse = StringField("Adresse", validators=[DataRequired(message=CHAMP_REQUIS)])
codepostal = StringField(
"Code postal", validators=[DataRequired(message=CHAMP_REQUIS)]
)
ville = StringField("Ville", validators=[DataRequired(message=CHAMP_REQUIS)])
pays = StringField("Pays", validators=[DataRequired(message=CHAMP_REQUIS)])
submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"})
class OffreCreationForm(FlaskForm): class OffreCreationForm(FlaskForm):
intitule = StringField("Intitulé", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) intitule = StringField("Intitulé", validators=[DataRequired(message=CHAMP_REQUIS)])
description = TextAreaField("Description", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) description = TextAreaField(
type_offre = SelectField("Type de l'offre", choices=[('Stage'), ('Alternance')], validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) "Description", validators=[DataRequired(message=CHAMP_REQUIS)]
missions = TextAreaField("Missions", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) )
duree = StringField("Durée", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) type_offre = SelectField(
fichier = FileField("Fichier", validators=[FileAllowed(['pdf', 'docx'], 'Fichier .pdf ou .docx uniquement')]) "Type de l'offre",
choices=[("Stage"), ("Alternance")],
validators=[DataRequired(message=CHAMP_REQUIS)],
)
missions = TextAreaField(
"Missions", validators=[DataRequired(message=CHAMP_REQUIS)]
)
duree = StringField("Durée", validators=[DataRequired(message=CHAMP_REQUIS)])
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"})
class OffreModificationForm(FlaskForm): class OffreModificationForm(FlaskForm):
intitule = StringField("Intitulé", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) intitule = StringField("Intitulé", validators=[DataRequired(message=CHAMP_REQUIS)])
description = TextAreaField("Description", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) description = TextAreaField(
type_offre = SelectField("Type de l'offre", choices=[('Stage'), ('Alternance')], validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) "Description", validators=[DataRequired(message=CHAMP_REQUIS)]
missions = TextAreaField("Missions", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) )
duree = StringField("Durée", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) type_offre = SelectField(
"Type de l'offre",
choices=[("Stage"), ("Alternance")],
validators=[DataRequired(message=CHAMP_REQUIS)],
)
missions = TextAreaField(
"Missions", validators=[DataRequired(message=CHAMP_REQUIS)]
)
duree = StringField("Durée", validators=[DataRequired(message=CHAMP_REQUIS)])
submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"})
class ContactCreationForm(FlaskForm): class ContactCreationForm(FlaskForm):
hidden_entreprise_id = HiddenField() hidden_entreprise_id = HiddenField()
nom = StringField("Nom", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) nom = StringField("Nom", validators=[DataRequired(message=CHAMP_REQUIS)])
prenom = StringField("Prénom", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) prenom = StringField("Prénom", validators=[DataRequired(message=CHAMP_REQUIS)])
telephone = StringField("Téléphone", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) telephone = StringField(
mail = EmailField("Mail", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE), Email(message="Adresse e-mail invalide")]) "Téléphone", validators=[DataRequired(message=CHAMP_REQUIS)]
)
mail = EmailField(
"Mail",
validators=[
DataRequired(message=CHAMP_REQUIS),
Email(message="Adresse e-mail invalide"),
],
)
poste = StringField("Poste", validators=[]) poste = StringField("Poste", validators=[])
service = StringField("Service", validators=[]) service = StringField("Service", validators=[])
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"})
@ -86,7 +157,7 @@ class ContactCreationForm(FlaskForm):
contact = EntrepriseContact.query.filter_by( contact = EntrepriseContact.query.filter_by(
entreprise_id=self.hidden_entreprise_id.data, entreprise_id=self.hidden_entreprise_id.data,
nom=self.nom.data, nom=self.nom.data,
prenom = self.prenom.data prenom=self.prenom.data,
).first() ).first()
if contact is not None: if contact is not None:
@ -96,20 +167,40 @@ class ContactCreationForm(FlaskForm):
return True return True
class ContactModificationForm(FlaskForm): class ContactModificationForm(FlaskForm):
nom = StringField("Nom", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) nom = StringField("Nom", validators=[DataRequired(message=CHAMP_REQUIS)])
prenom = StringField("Prénom", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) prenom = StringField("Prénom", validators=[DataRequired(message=CHAMP_REQUIS)])
telephone = StringField("Téléphone", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) telephone = StringField(
mail = EmailField("Mail", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE), Email(message="Adresse e-mail invalide")]) "Téléphone", validators=[DataRequired(message=CHAMP_REQUIS)]
)
mail = EmailField(
"Mail",
validators=[
DataRequired(message=CHAMP_REQUIS),
Email(message="Adresse e-mail invalide"),
],
)
poste = StringField("Poste", validators=[]) poste = StringField("Poste", validators=[])
service = StringField("Service", validators=[]) service = StringField("Service", validators=[])
submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"})
class HistoriqueCreationForm(FlaskForm): class HistoriqueCreationForm(FlaskForm):
etudiant = StringField("Étudiant", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)], render_kw={"placeholder": "Tapez le nom de l'étudiant puis selectionnez"}) etudiant = StringField(
type_offre = SelectField("Type de l'offre", choices=[('Stage'), ('Alternance')], validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) "Étudiant",
date_debut = DateField("Date début", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) validators=[DataRequired(message=CHAMP_REQUIS)],
date_fin = DateField("Date fin", 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=CHAMP_REQUIS)],
)
date_debut = DateField(
"Date début", validators=[DataRequired(message=CHAMP_REQUIS)]
)
date_fin = DateField("Date fin", validators=[DataRequired(message=CHAMP_REQUIS)])
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"})
def validate(self): def validate(self):
@ -125,21 +216,47 @@ class HistoriqueCreationForm(FlaskForm):
def validate_etudiant(self, etudiant): def validate_etudiant(self, etudiant):
etudiant_data = etudiant.data.upper().strip() etudiant_data = etudiant.data.upper().strip()
stm = text("SELECT id, CONCAT(nom, ' ', prenom) as nom_prenom FROM Identite WHERE CONCAT(nom, ' ', prenom)=:nom_prenom") stm = text(
etudiant = Identite.query.from_statement(stm).params(nom_prenom=etudiant_data).first() "SELECT id, CONCAT(nom, ' ', prenom) as nom_prenom FROM Identite WHERE CONCAT(nom, ' ', prenom)=:nom_prenom"
)
etudiant = (
Identite.query.from_statement(stm).params(nom_prenom=etudiant_data).first()
)
if etudiant is None: if etudiant is None:
raise ValidationError("Champ incorrect (selectionnez dans la liste)") raise ValidationError("Champ incorrect (selectionnez dans la liste)")
class EnvoiOffreForm(FlaskForm): class EnvoiOffreForm(FlaskForm):
responsable = StringField("Responsable de formation", validators=[DataRequired(message=DATA_REQUIRED_ERROR_MESSAGE)]) responsable = StringField(
"Responsable de formation",
validators=[DataRequired(message=CHAMP_REQUIS)],
)
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"})
def validate_responsable(self, responsable): def validate_responsable(self, responsable):
responsable_data = responsable.data.upper().strip() responsable_data = 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") stm = text(
responsable = User.query.from_statement(stm).params(responsable_data=responsable_data).first() "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()
)
if responsable is None: if responsable is None:
raise ValidationError("Champ incorrect (selectionnez dans la liste)") raise ValidationError("Champ incorrect (selectionnez dans la liste)")
class AjoutFichierForm(FlaskForm):
fichier = FileField(
"Fichier",
validators=[
FileRequired(message=CHAMP_REQUIS),
FileAllowed(["pdf", "docx"], "Fichier .pdf ou .docx uniquement"),
],
)
submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"})
class SuppressionConfirmationForm(FlaskForm): class SuppressionConfirmationForm(FlaskForm):
submit = SubmitField("Supprimer", render_kw={"style": "margin-bottom: 10px;"}) submit = SubmitField("Supprimer", render_kw={"style": "margin-bottom: 10px;"})

View File

@ -1,5 +1,6 @@
from app import db from app import db
class Entreprise(db.Model): class Entreprise(db.Model):
__tablename__ = "entreprises" __tablename__ = "entreprises"
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
@ -9,23 +10,36 @@ class Entreprise(db.Model):
codepostal = db.Column(db.Text) codepostal = db.Column(db.Text)
ville = db.Column(db.Text) ville = db.Column(db.Text)
pays = db.Column(db.Text) pays = db.Column(db.Text)
contacts = db.relationship('EntrepriseContact', backref='entreprise', lazy='dynamic', cascade="all, delete-orphan") contacts = db.relationship(
offres = db.relationship('EntrepriseOffre', backref='entreprise', lazy='dynamic', cascade="all, delete-orphan") "EntrepriseContact",
backref="entreprise",
lazy="dynamic",
cascade="all, delete-orphan",
)
offres = db.relationship(
"EntrepriseOffre",
backref="entreprise",
lazy="dynamic",
cascade="all, delete-orphan",
)
def to_dict_export(self): def to_dict(self):
return { return {
"siret": self.siret, "siret": self.siret,
"nom": self.nom, "nom": self.nom,
"adresse": self.adresse, "adresse": self.adresse,
"codepostal": self.codepostal, "codepostal": self.codepostal,
"ville": self.ville, "ville": self.ville,
"pays": self.pays "pays": self.pays,
} }
class EntrepriseContact(db.Model): class EntrepriseContact(db.Model):
__tablename__ = "entreprise_contact" __tablename__ = "entreprise_contact"
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
entreprise_id = db.Column(db.Integer, db.ForeignKey("entreprises.id", ondelete="cascade")) entreprise_id = db.Column(
db.Integer, db.ForeignKey("entreprises.id", ondelete="cascade")
)
nom = db.Column(db.Text) nom = db.Column(db.Text)
prenom = db.Column(db.Text) prenom = db.Column(db.Text)
telephone = db.Column(db.Text) telephone = db.Column(db.Text)
@ -33,27 +47,30 @@ class EntrepriseContact(db.Model):
poste = db.Column(db.Text) poste = db.Column(db.Text)
service = db.Column(db.Text) service = db.Column(db.Text)
def to_dict_export(self): def to_dict(self):
return { return {
"nom": self.nom, "nom": self.nom,
"prenom": self.prenom, "prenom": self.prenom,
"telephone": self.telephone, "telephone": self.telephone,
"mail": self.mail, "mail": self.mail,
"poste": self.poste, "poste": self.poste,
"service": self.service "service": self.service,
} }
class EntrepriseOffre(db.Model): class EntrepriseOffre(db.Model):
__tablename__ = "entreprise_offre" __tablename__ = "entreprise_offre"
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
entreprise_id = db.Column(db.Integer, db.ForeignKey("entreprises.id", ondelete="cascade")) 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()) date_ajout = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
intitule = db.Column(db.Text) intitule = db.Column(db.Text)
description = db.Column(db.Text) description = db.Column(db.Text)
type_offre = db.Column(db.Text) type_offre = db.Column(db.Text)
missions = db.Column(db.Text) missions = db.Column(db.Text)
duree = db.Column(db.Text) duree = db.Column(db.Text)
filename = db.Column(db.Text)
class EntrepriseLog(db.Model): class EntrepriseLog(db.Model):
__tablename__ = "entreprise_log" __tablename__ = "entreprise_log"
@ -63,6 +80,7 @@ class EntrepriseLog(db.Model):
object = db.Column(db.Integer) object = db.Column(db.Integer)
text = db.Column(db.Text) text = db.Column(db.Text)
class EntrepriseEtudiant(db.Model): class EntrepriseEtudiant(db.Model):
__tablename__ = "entreprise_etudiant" __tablename__ = "entreprise_etudiant"
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
@ -74,9 +92,11 @@ class EntrepriseEtudiant(db.Model):
formation_text = db.Column(db.Text) formation_text = db.Column(db.Text)
formation_scodoc = db.Column(db.Integer) formation_scodoc = db.Column(db.Integer)
class EntrepriseEnvoiOffre(db.Model): class EntrepriseEnvoiOffre(db.Model):
__tablename__ = "entreprise_envoi_offre" __tablename__ = "entreprise_envoi_offre"
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("user.id")) sender_id = db.Column(db.Integer, db.ForeignKey("user.id"))
receiver_id = db.Column(db.Integer, db.ForeignKey("user.id"))
offre_id = db.Column(db.Integer, db.ForeignKey("entreprise_offre.id")) offre_id = db.Column(db.Integer, db.ForeignKey("entreprise_offre.id"))
date_envoi = db.Column(db.DateTime(timezone=True), server_default=db.func.now()) date_envoi = db.Column(db.DateTime(timezone=True), server_default=db.func.now())

View File

@ -1,5 +1,8 @@
import os import os
from config import Config from config import Config
from datetime import datetime
import glob
import shutil
from flask import render_template, redirect, url_for, request, flash, send_file, abort from flask import render_template, redirect, url_for, request, flash, send_file, abort
from flask.json import jsonify from flask.json import jsonify
@ -17,7 +20,8 @@ from app.entreprises.forms import (
ContactCreationForm, ContactCreationForm,
ContactModificationForm, ContactModificationForm,
HistoriqueCreationForm, HistoriqueCreationForm,
EnvoiOffreForm EnvoiOffreForm,
AjoutFichierForm,
) )
from app.entreprises import bp from app.entreprises import bp
from app.entreprises.models import ( from app.entreprises.models import (
@ -26,11 +30,9 @@ from app.entreprises.models import (
EntrepriseContact, EntrepriseContact,
EntrepriseLog, EntrepriseLog,
EntrepriseEtudiant, EntrepriseEtudiant,
EntrepriseEnvoiOffre EntrepriseEnvoiOffre,
)
from app.models import (
Identite
) )
from app.models import Identite
from app.auth.models import User from app.auth.models import User
from app.scodoc.sco_find_etud import search_etud_by_name from app.scodoc.sco_find_etud import search_etud_by_name
from app.scodoc.sco_permissions import Permission from app.scodoc.sco_permissions import Permission
@ -41,35 +43,88 @@ from app import db
from sqlalchemy import text from sqlalchemy import text
from werkzeug.utils import secure_filename, send_from_directory from werkzeug.utils import secure_filename, send_from_directory
@bp.route("/", methods=["GET"]) @bp.route("/", methods=["GET"])
def index(): def index():
entreprises = Entreprise.query.all() entreprises = Entreprise.query.all()
logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all() logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all()
return render_template("entreprises/entreprises.html", title=("Entreprises"), entreprises=entreprises, logs=logs) return render_template(
"entreprises/entreprises.html",
title=("Entreprises"),
entreprises=entreprises,
logs=logs,
)
@bp.route("/contacts", methods=["GET"]) @bp.route("/contacts", methods=["GET"])
def contacts(): def contacts():
contacts = db.session.query(EntrepriseContact, Entreprise).\ contacts = (
join(Entreprise, EntrepriseContact.entreprise_id == Entreprise.id).all() db.session.query(EntrepriseContact, Entreprise)
.join(Entreprise, EntrepriseContact.entreprise_id == Entreprise.id)
.all()
)
logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all() logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all()
return render_template("entreprises/contacts.html", title=("Contacts"), contacts=contacts, logs=logs) return render_template(
"entreprises/contacts.html", title=("Contacts"), contacts=contacts, logs=logs
)
@bp.route("/fiche_entreprise/<int:id>", methods=["GET"]) @bp.route("/fiche_entreprise/<int:id>", methods=["GET"])
def fiche_entreprise(id): def fiche_entreprise(id):
entreprise = Entreprise.query.filter_by(id=id).first_or_404() entreprise = Entreprise.query.filter_by(id=id).first_or_404()
offres = entreprise.offres offres = entreprise.offres
offres_with_files = []
for offre in offres:
path = os.path.join(
Config.SCODOC_VAR_DIR, "entreprises", f"{entreprise.id}", f"{offre.id}"
)
if os.path.exists(path):
files = []
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)
offres_with_files.append([offre, files])
print(offres_with_files)
contacts = entreprise.contacts contacts = entreprise.contacts
logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).filter_by(object=id).limit(LOGS_LEN).all() logs = (
historique = db.session.query(EntrepriseEtudiant, Identite).order_by(EntrepriseEtudiant.date_debut.desc()).\ EntrepriseLog.query.order_by(EntrepriseLog.date.desc())
filter(EntrepriseEtudiant.entreprise_id == id).\ .filter_by(object=id)
join(Identite, Identite.id == EntrepriseEtudiant.etudid).all() .limit(LOGS_LEN)
return render_template("entreprises/fiche_entreprise.html", title=("Fiche entreprise"), entreprise=entreprise, contacts=contacts, offres=offres, logs=logs, historique=historique) .all()
)
historique = (
db.session.query(EntrepriseEtudiant, Identite)
.order_by(EntrepriseEtudiant.date_debut.desc())
.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_with_files,
logs=logs,
historique=historique,
)
@bp.route("/offres", methods=["GET"]) @bp.route("/offres", methods=["GET"])
def offres(): def offres():
offres_recus = db.session.query(EntrepriseEnvoiOffre, EntrepriseOffre).filter(EntrepriseEnvoiOffre.user_id == current_user.id).\ offres_recus = (
join(EntrepriseOffre, EntrepriseOffre.id == EntrepriseEnvoiOffre.offre_id).all() db.session.query(EntrepriseEnvoiOffre, EntrepriseOffre)
return render_template("entreprises/offres.html", title=("Offres"), offres_recus=offres_recus) .filter(EntrepriseEnvoiOffre.receiver_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"]) @bp.route("/add_entreprise", methods=["GET", "POST"])
def add_entreprise(): def add_entreprise():
@ -81,7 +136,7 @@ def add_entreprise():
adresse=form.adresse.data.strip(), adresse=form.adresse.data.strip(),
codepostal=form.codepostal.data.strip(), codepostal=form.codepostal.data.strip(),
ville=form.ville.data.strip(), ville=form.ville.data.strip(),
pays=form.pays.data.strip() pays=form.pays.data.strip(),
) )
db.session.add(entreprise) db.session.add(entreprise)
db.session.commit() db.session.commit()
@ -93,7 +148,7 @@ def add_entreprise():
telephone=form.telephone.data.strip(), telephone=form.telephone.data.strip(),
mail=form.mail.data.strip(), mail=form.mail.data.strip(),
poste=form.poste.data.strip(), poste=form.poste.data.strip(),
service=form.service.data.strip() service=form.service.data.strip(),
) )
db.session.add(contact) db.session.add(contact)
nom_entreprise = f"<a href=/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}>{entreprise.nom}</a>" nom_entreprise = f"<a href=/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}>{entreprise.nom}</a>"
@ -105,7 +160,12 @@ def add_entreprise():
db.session.commit() db.session.commit()
flash("L'entreprise a été ajouté à la liste.") flash("L'entreprise a été ajouté à la liste.")
return redirect(url_for("entreprises.index")) return redirect(url_for("entreprises.index"))
return render_template("entreprises/ajout_entreprise.html", title=("Ajout entreprise + contact"), form=form) return render_template(
"entreprises/ajout_entreprise.html",
title=("Ajout entreprise + contact"),
form=form,
)
@bp.route("/edit_entreprise/<int:id>", methods=["GET", "POST"]) @bp.route("/edit_entreprise/<int:id>", methods=["GET", "POST"])
def edit_entreprise(id): def edit_entreprise(id):
@ -156,14 +216,17 @@ def edit_entreprise(id):
db.session.commit() db.session.commit()
flash("L'entreprise a été modifié.") flash("L'entreprise a été modifié.")
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id))
elif request.method == 'GET': elif request.method == "GET":
form.siret.data = entreprise.siret form.siret.data = entreprise.siret
form.nom.data = entreprise.nom form.nom.data = entreprise.nom
form.adresse.data = entreprise.adresse form.adresse.data = entreprise.adresse
form.codepostal.data = entreprise.codepostal form.codepostal.data = entreprise.codepostal
form.ville.data = entreprise.ville form.ville.data = entreprise.ville
form.pays.data = entreprise.pays form.pays.data = entreprise.pays
return render_template("entreprises/form.html", title=("Modification entreprise"), form=form) return render_template(
"entreprises/form.html", title=("Modification entreprise"), form=form
)
@bp.route("/delete_entreprise/<int:id>", methods=["GET", "POST"]) @bp.route("/delete_entreprise/<int:id>", methods=["GET", "POST"])
def delete_entreprise(id): def delete_entreprise(id):
@ -180,7 +243,12 @@ def delete_entreprise(id):
db.session.commit() db.session.commit()
flash("L'entreprise a été supprimé de la liste.") flash("L'entreprise a été supprimé de la liste.")
return redirect(url_for("entreprises.index")) return redirect(url_for("entreprises.index"))
return render_template("entreprises/delete_confirmation.html", title=("Supression entreprise"), form=form) return render_template(
"entreprises/delete_confirmation.html",
title=("Supression entreprise"),
form=form,
)
@bp.route("/add_offre/<int:id>", methods=["GET", "POST"]) @bp.route("/add_offre/<int:id>", methods=["GET", "POST"])
def add_offre(id): def add_offre(id):
@ -193,30 +261,21 @@ def add_offre(id):
description=form.description.data.strip(), description=form.description.data.strip(),
type_offre=form.type_offre.data.strip(), type_offre=form.type_offre.data.strip(),
missions=form.missions.data.strip(), missions=form.missions.data.strip(),
duree=form.duree.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.filename = f"{filename}"
db.session.commit()
log = EntrepriseLog( log = EntrepriseLog(
authenticated_user=current_user.user_name, authenticated_user=current_user.user_name,
object=entreprise.id, object=entreprise.id,
text="Création d'une offre", text="Création d'une offre",
) )
db.session.add(offre)
db.session.add(log) db.session.add(log)
db.session.commit() db.session.commit()
flash("L'offre a été ajouté à la fiche entreprise.") flash("L'offre a été ajouté à la fiche entreprise.")
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id))
return render_template("entreprises/form.html", title=("Ajout offre"), form=form) return render_template("entreprises/form.html", title=("Ajout offre"), form=form)
@bp.route("/edit_offre/<int:id>", methods=["GET", "POST"]) @bp.route("/edit_offre/<int:id>", methods=["GET", "POST"])
def edit_offre(id): def edit_offre(id):
offre = EntrepriseOffre.query.filter_by(id=id).first_or_404() offre = EntrepriseOffre.query.filter_by(id=id).first_or_404()
@ -236,13 +295,16 @@ def edit_offre(id):
db.session.commit() db.session.commit()
flash("L'offre a été modifié.") flash("L'offre a été modifié.")
return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise.id)) return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise.id))
elif request.method == 'GET': elif request.method == "GET":
form.intitule.data = offre.intitule form.intitule.data = offre.intitule
form.description.data = offre.description form.description.data = offre.description
form.type_offre.data = offre.type_offre form.type_offre.data = offre.type_offre
form.missions.data = offre.missions form.missions.data = offre.missions
form.duree.data = offre.duree form.duree.data = offre.duree
return render_template("entreprises/form.html", title=("Modification offre"), form=form) return render_template(
"entreprises/form.html", title=("Modification offre"), form=form
)
@bp.route("/delete_offre/<int:id>", methods=["GET", "POST"]) @bp.route("/delete_offre/<int:id>", methods=["GET", "POST"])
def delete_offre(id): def delete_offre(id):
@ -260,7 +322,10 @@ def delete_offre(id):
db.session.commit() db.session.commit()
flash("L'offre a été supprimé de la fiche entreprise.") flash("L'offre a été supprimé de la fiche entreprise.")
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id))
return render_template("entreprises/delete_confirmation.html", title=("Supression offre"), form=form) return render_template(
"entreprises/delete_confirmation.html", title=("Supression offre"), form=form
)
@bp.route("/add_contact/<int:id>", methods=["GET", "POST"]) @bp.route("/add_contact/<int:id>", methods=["GET", "POST"])
def add_contact(id): def add_contact(id):
@ -274,7 +339,7 @@ def add_contact(id):
telephone=form.telephone.data.strip(), telephone=form.telephone.data.strip(),
mail=form.mail.data.strip(), mail=form.mail.data.strip(),
poste=form.poste.data.strip(), poste=form.poste.data.strip(),
service=form.service.data.strip() service=form.service.data.strip(),
) )
log = EntrepriseLog( log = EntrepriseLog(
authenticated_user=current_user.user_name, authenticated_user=current_user.user_name,
@ -288,6 +353,7 @@ def add_contact(id):
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id))
return render_template("entreprises/form.html", title=("Ajout contact"), form=form) return render_template("entreprises/form.html", title=("Ajout contact"), form=form)
@bp.route("/edit_contact/<int:id>", methods=["GET", "POST"]) @bp.route("/edit_contact/<int:id>", methods=["GET", "POST"])
def edit_contact(id): def edit_contact(id):
contact = EntrepriseContact.query.filter_by(id=id).first_or_404() contact = EntrepriseContact.query.filter_by(id=id).first_or_404()
@ -307,15 +373,20 @@ def edit_contact(id):
db.session.add(log) db.session.add(log)
db.session.commit() db.session.commit()
flash("Le contact a été modifié.") flash("Le contact a été modifié.")
return redirect(url_for("entreprises.fiche_entreprise", id=contact.entreprise.id)) return redirect(
elif request.method == 'GET': url_for("entreprises.fiche_entreprise", id=contact.entreprise.id)
)
elif request.method == "GET":
form.nom.data = contact.nom form.nom.data = contact.nom
form.prenom.data = contact.prenom form.prenom.data = contact.prenom
form.telephone.data = contact.telephone form.telephone.data = contact.telephone
form.mail.data = contact.mail form.mail.data = contact.mail
form.poste.data = contact.poste form.poste.data = contact.poste
form.service.data = contact.service form.service.data = contact.service
return render_template("entreprises/form.html", title=("Modification contact"), form=form) return render_template(
"entreprises/form.html", title=("Modification contact"), form=form
)
@bp.route("/delete_contact/<int:id>", methods=["GET", "POST"]) @bp.route("/delete_contact/<int:id>", methods=["GET", "POST"])
def delete_contact(id): def delete_contact(id):
@ -323,9 +394,13 @@ def delete_contact(id):
entreprise_id = contact.entreprise.id entreprise_id = contact.entreprise.id
form = SuppressionConfirmationForm() form = SuppressionConfirmationForm()
if form.validate_on_submit(): if form.validate_on_submit():
contact_count = EntrepriseContact.query.filter_by(entreprise_id=contact.entreprise.id).count() contact_count = EntrepriseContact.query.filter_by(
entreprise_id=contact.entreprise.id
).count()
if contact_count == 1: if contact_count == 1:
flash("Le contact n'a pas été supprimé de la fiche entreprise. (1 contact minimum)") flash(
"Le contact n'a pas été supprimé de la fiche entreprise. (1 contact minimum)"
)
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id))
else: else:
db.session.delete(contact) db.session.delete(contact)
@ -338,7 +413,10 @@ def delete_contact(id):
db.session.commit() db.session.commit()
flash("Le contact a été supprimé de la fiche entreprise.") flash("Le contact a été supprimé de la fiche entreprise.")
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise_id))
return render_template("entreprises/delete_confirmation.html", title=("Supression contact"), form=form) return render_template(
"entreprises/delete_confirmation.html", title=("Supression contact"), form=form
)
@bp.route("/add_historique/<int:id>", methods=["GET", "POST"]) @bp.route("/add_historique/<int:id>", methods=["GET", "POST"])
def add_historique(id): def add_historique(id):
@ -346,25 +424,36 @@ def add_historique(id):
form = HistoriqueCreationForm() form = HistoriqueCreationForm()
if form.validate_on_submit(): if form.validate_on_submit():
etudiant_nomcomplet = form.etudiant.data.upper().strip() etudiant_nomcomplet = form.etudiant.data.upper().strip()
stm = text("SELECT id, CONCAT(nom, ' ', prenom) as nom_prenom FROM Identite WHERE CONCAT(nom, ' ', prenom)=:nom_prenom") stm = text(
etudiant = Identite.query.from_statement(stm).params(nom_prenom=etudiant_nomcomplet).first() "SELECT id, CONCAT(nom, ' ', prenom) as nom_prenom FROM Identite WHERE CONCAT(nom, ' ', prenom)=:nom_prenom"
formation = etudiant.inscription_courante_date(form.date_debut.data, form.date_fin.data) )
etudiant = (
Identite.query.from_statement(stm)
.params(nom_prenom=etudiant_nomcomplet)
.first()
)
formation = etudiant.inscription_courante_date(
form.date_debut.data, form.date_fin.data
)
historique = EntrepriseEtudiant( historique = EntrepriseEtudiant(
entreprise_id=entreprise.id, entreprise_id=entreprise.id,
etudid=etudiant.id, etudid=etudiant.id,
type_offre=form.type_offre.data.strip(), type_offre=form.type_offre.data.strip(),
date_debut=form.date_debut.data, date_debut=form.date_debut.data,
date_fin=form.date_fin.data, date_fin=form.date_fin.data,
formation_text = formation.formsemestre.titre formation_text=formation.formsemestre.titre if formation else None,
if formation else None,
formation_scodoc=formation.formsemestre.formsemestre_id formation_scodoc=formation.formsemestre.formsemestre_id
if formation else None if formation
else None,
) )
db.session.add(historique) db.session.add(historique)
db.session.commit() db.session.commit()
flash("L'étudiant a été ajouté sur la fiche entreprise.") flash("L'étudiant a été ajouté sur la fiche entreprise.")
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id))
return render_template("entreprises/ajout_historique.html", title=("Ajout historique"), form=form) return render_template(
"entreprises/ajout_historique.html", title=("Ajout historique"), form=form
)
@bp.route("/envoyer_offre/<int:id>", methods=["GET", "POST"]) @bp.route("/envoyer_offre/<int:id>", methods=["GET", "POST"])
def envoyer_offre(id): def envoyer_offre(id):
@ -372,21 +461,29 @@ def envoyer_offre(id):
form = EnvoiOffreForm() form = EnvoiOffreForm()
if form.validate_on_submit(): if form.validate_on_submit():
responsable_data = form.responsable.data.upper().strip() 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") stm = text(
responsable = User.query.from_statement(stm).params(responsable_data=responsable_data).first() "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( envoi_offre = EntrepriseEnvoiOffre(
user_id = responsable.id, sender_id=current_user.id, receiver_id=responsable.id, offre_id=offre.id
offre_id = offre.id
) )
db.session.add(envoi_offre) db.session.add(envoi_offre)
db.session.commit() db.session.commit()
flash(f"L'offre a été envoyé à {responsable.get_nomplogin()}.") flash(f"L'offre a été envoyé à {responsable.get_nomplogin()}.")
return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise_id)) 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) return render_template(
"entreprises/envoi_offre_form.html", title=("Envoyer une offre"), form=form
)
@bp.route("/etudiants") @bp.route("/etudiants")
def json_etudiants(): 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 = {} content = {}
@ -396,48 +493,41 @@ def json_etudiants():
content = { content = {
"id": f"{etudiant.id}", "id": f"{etudiant.id}",
"value": value, "value": value,
"info": f"{etudiant.inscription_courante().formsemestre.titre}" "info": f"{etudiant.inscription_courante().formsemestre.titre}",
} }
else: else:
content = { content = {"id": f"{etudiant.id}", "value": value}
"id": f"{etudiant.id}",
"value": value
}
list.append(content) list.append(content)
content = {} content = {}
return jsonify(results=list) return jsonify(results=list)
@bp.route("/responsables") @bp.route("/responsables")
def json_responsables(): def json_responsables():
term = request.args.get('term').strip() term = request.args.get("term").strip()
responsables = User.query.filter(User.nom.ilike(f"%{term}%"), User.nom.is_not(None), User.prenom.is_not(None)).all() responsables = User.query.filter(
User.nom.ilike(f"%{term}%"), User.nom.is_not(None), User.prenom.is_not(None)
).all()
list = [] list = []
content = {} content = {}
for responsable in responsables: for responsable in responsables:
value = f"{responsable.get_nomplogin()}" value = f"{responsable.get_nomplogin()}"
content = { content = {"id": f"{responsable.id}", "value": value, "info": ""}
"id": f"{responsable.id}",
"value": value,
"info": ""
}
list.append(content) list.append(content)
content = {} content = {}
return jsonify(results=list) return jsonify(results=list)
@bp.route("/export_entreprises") @bp.route("/export_entreprises")
def export_entreprises(): def export_entreprises():
entreprises = Entreprise.query.all() entreprises = Entreprise.query.all()
if entreprises: if entreprises:
keys=[ keys = ["siret", "nom", "adresse", "ville", "codepostal", "pays"]
"siret",
"nom",
"adresse",
"ville",
"codepostal",
"pays"
]
titles = keys[:] titles = keys[:]
L = [[entreprise.to_dict_export().get(k, "") for k in keys] for entreprise in entreprises] L = [
[entreprise.to_dict_export().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) xlsx = sco_excel.excel_simple_table(titles=titles, lines=L, sheet_name=title)
filename = title filename = title
@ -445,20 +535,16 @@ def export_entreprises():
else: else:
abort(404) abort(404)
@bp.route("/export_contacts") @bp.route("/export_contacts")
def export_contacts(): def export_contacts():
contacts = EntrepriseContact.query.all() contacts = EntrepriseContact.query.all()
if contacts: if contacts:
keys=[ keys = ["nom", "prenom", "telephone", "mail", "poste", "service"]
"nom",
"prenom",
"telephone",
"mail",
"poste",
"service"
]
titles = keys[:] titles = keys[:]
L = [[contact.to_dict_export().get(k, "") for k in keys] for contact in contacts] L = [
[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
@ -466,9 +552,80 @@ def export_contacts():
else: else:
abort(404) abort(404)
@bp.route("/download_offre/<int:entreprise_id>/<int:offre_id>/<string:filename>")
def download_offre(entreprise_id, offre_id, filename): @bp.route(
if os.path.isfile(os.path.join(Config.SCODOC_VAR_DIR, "entreprises", f"{entreprise_id}", f"{offre_id}", f"{filename}")): "/get_offre_file/<int:entreprise_id>/<int:offre_id>/<string:filedir>/<string:filename>"
return send_file(os.path.join(Config.SCODOC_VAR_DIR, "entreprises", f"{entreprise_id}", f"{offre_id}", f"{filename}"), as_attachment=True) )
def get_offre_file(entreprise_id, offre_id, filedir, filename):
if os.path.isfile(
os.path.join(
Config.SCODOC_VAR_DIR,
"entreprises",
f"{entreprise_id}",
f"{offre_id}",
f"{filedir}",
f"{filename}",
)
):
return send_file(
os.path.join(
Config.SCODOC_VAR_DIR,
"entreprises",
f"{entreprise_id}",
f"{offre_id}",
f"{filedir}",
f"{filename}",
),
as_attachment=True,
)
else: else:
abort(404) abort(404)
@bp.route("/add_offre_file/<int:offre_id>", methods=["GET", "POST"])
def add_offre_file(offre_id):
offre = EntrepriseOffre.query.filter_by(id=offre_id).first_or_404()
form = AjoutFichierForm()
if form.validate_on_submit():
date = f"{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}"
path = os.path.join(
Config.SCODOC_VAR_DIR,
"entreprises",
f"{offre.entreprise_id}",
f"{offre.id}",
f"{date}",
)
os.makedirs(path)
file = form.fichier.data
filename = secure_filename(file.filename)
file.save(os.path.join(path, filename))
flash("Le fichier a été ajouté a l'offre.")
return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise_id))
return render_template(
"entreprises/form.html", title=("Ajout fichier à une offre"), form=form
)
@bp.route("/delete_offre_file/<int:offre_id>/<string:filedir>", methods=["GET", "POST"])
def delete_offre_file(offre_id, filedir):
offre = EntrepriseOffre.query.filter_by(id=offre_id).first_or_404()
form = SuppressionConfirmationForm()
if form.validate_on_submit():
path = os.path.join(
Config.SCODOC_VAR_DIR,
"entreprises",
f"{offre.entreprise_id}",
f"{offre_id}",
f"{filedir}",
)
if os.path.isdir(path):
shutil.rmtree(path)
flash("Le fichier relié à l'offre a été supprimé.")
return redirect(
url_for("entreprises.fiche_entreprise", id=offre.entreprise_id)
)
return render_template(
"entreprises/delete_confirmation.html",
title=("Suppression fichier d'une offre"),
form=form,
)

View File

@ -1,18 +1,20 @@
<div> <div>
<p> <p>
Intitulé : {{ offre.intitule }}<br> Intitulé : {{ offre[0].intitule }}<br>
Description : {{ offre.description }}<br> Description : {{ offre[0].description }}<br>
Type de l'offre : {{ offre.type_offre }}<br> Type de l'offre : {{ offre[0].type_offre }}<br>
Missions : {{ offre.missions }}<br> Missions : {{ offre[0].missions }}<br>
Durée : {{ offre.duree }}<br> Durée : {{ offre[0].duree }}<br>
{% if offre.filename %} {% for fichier in offre[1] %}
<a href="{{ url_for('entreprises.download_offre', entreprise_id=entreprise.id, offre_id=offre.id, filename=offre.filename) }}">{{ offre.filename }}</a> <a href="{{ url_for('entreprises.get_offre_file', entreprise_id=entreprise.id, offre_id=offre[0].id, filedir=fichier[0], filename=fichier[1] )}}">{{ fichier[1] }}</a>
{% endif %} <a href="{{ url_for('entreprises.delete_offre_file', offre_id=offre[0].id, filedir=fichier[0] )}}" style="margin-left: 5px;"><img title="Supprimer fichier" alt="supprimer" width="10" height="9" border="0" src="/ScoDoc/static/icons/delete_small_img.png" /></a><br>
{% endfor %}
<a href="{{ url_for('entreprises.add_offre_file', offre_id=offre[0].id) }}">Ajoutez un fichier</a>
</p> </p>
<div style="margin-bottom: 10px;"> <div style="margin-bottom: 10px;">
<a class="btn btn-primary" href="{{ url_for('entreprises.edit_offre', id=offre.id) }}">Modifier l'offre</a> <a class="btn btn-primary" href="{{ url_for('entreprises.edit_offre', id=offre[0].id) }}">Modifier l'offre</a>
<a class="btn btn-danger" href="{{ url_for('entreprises.delete_offre', id=offre.id) }}">Supprimer l'offre</a> <a class="btn btn-danger" href="{{ url_for('entreprises.delete_offre', id=offre[0].id) }}">Supprimer l'offre</a>
<a class="btn btn-primary" href="{{ url_for('entreprises.envoyer_offre', id=offre.id) }}">Envoyer l'offre</a> <a class="btn btn-primary" href="{{ url_for('entreprises.envoyer_offre', id=offre[0].id) }}">Envoyer l'offre</a>
</div> </div>
</div> </div>

View File

@ -52,7 +52,7 @@
<br> <br>
</div> </div>
{% endif %} {% endif %}
<div> <div style="margin-bottom: 20px;">
<a class="btn btn-default" href="{{ url_for('entreprises.add_entreprise') }}">Ajouter une entreprise</a> <a class="btn btn-default" href="{{ url_for('entreprises.add_entreprise') }}">Ajouter une entreprise</a>
{% if entreprises %} {% if entreprises %}
<a class="btn btn-default" href="{{ url_for('entreprises.export_entreprises') }}">Exporter la liste des entreprises</a> <a class="btn btn-default" href="{{ url_for('entreprises.export_entreprises') }}">Exporter la liste des entreprises</a>

View File

@ -55,7 +55,7 @@
{% if offres %} {% if offres %}
<div> <div>
{% for offre in offres %} {% for offre in offres %}
Offre {{loop.index}} (ajouté le {{offre.date_ajout.strftime('%d/%m/%Y') }}) Offre {{loop.index}} (ajouté le {{offre[0].date_ajout.strftime('%d/%m/%Y') }})
{% include 'entreprises/_offre.html' %} {% include 'entreprises/_offre.html' %}
{% endfor %} {% endfor %}
</div> </div>

View File

@ -49,7 +49,6 @@ def upgrade():
sa.Column('type_offre', sa.Text(), nullable=True), sa.Column('type_offre', sa.Text(), nullable=True),
sa.Column('missions', sa.Text(), nullable=True), sa.Column('missions', sa.Text(), nullable=True),
sa.Column('duree', sa.Text(), nullable=True), sa.Column('duree', sa.Text(), nullable=True),
sa.Column('filename', sa.Text(), nullable=True),
sa.ForeignKeyConstraint(['entreprise_id'], ['entreprises.id'], ondelete='cascade'), sa.ForeignKeyConstraint(['entreprise_id'], ['entreprises.id'], ondelete='cascade'),
sa.PrimaryKeyConstraint('id') sa.PrimaryKeyConstraint('id')
) )