474 lines
20 KiB
Python
474 lines
20 KiB
Python
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
|
|
|
|
from app.decorators import permission_required
|
|
|
|
from app.entreprises import LOGS_LEN
|
|
from app.entreprises.forms import (
|
|
EntrepriseCreationForm,
|
|
EntrepriseModificationForm,
|
|
SuppressionConfirmationForm,
|
|
OffreCreationForm,
|
|
OffreModificationForm,
|
|
ContactCreationForm,
|
|
ContactModificationForm,
|
|
HistoriqueCreationForm,
|
|
EnvoiOffreForm
|
|
)
|
|
from app.entreprises import bp
|
|
from app.entreprises.models import (
|
|
Entreprise,
|
|
EntrepriseOffre,
|
|
EntrepriseContact,
|
|
EntrepriseLog,
|
|
EntrepriseEtudiant,
|
|
EntrepriseEnvoiOffre
|
|
)
|
|
from app.models import (
|
|
Identite
|
|
)
|
|
from app.auth.models import User
|
|
from app.scodoc.sco_find_etud import search_etud_by_name
|
|
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
|
|
from werkzeug.utils import secure_filename, send_from_directory
|
|
|
|
@bp.route("/", methods=["GET"])
|
|
def index():
|
|
entreprises = Entreprise.query.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)
|
|
|
|
@bp.route("/contacts", methods=["GET"])
|
|
def contacts():
|
|
contacts = 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()
|
|
return render_template("entreprises/contacts.html", title=("Contacts"), contacts=contacts, logs=logs)
|
|
|
|
@bp.route("/fiche_entreprise/<int:id>", methods=["GET"])
|
|
def fiche_entreprise(id):
|
|
entreprise = Entreprise.query.filter_by(id=id).first_or_404()
|
|
offres = entreprise.offres
|
|
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(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()
|
|
if form.validate_on_submit():
|
|
entreprise = Entreprise(
|
|
nom=form.nom_entreprise.data.strip(),
|
|
siret=form.siret.data.strip(),
|
|
adresse=form.adresse.data.strip(),
|
|
codepostal=form.codepostal.data.strip(),
|
|
ville=form.ville.data.strip(),
|
|
pays=form.pays.data.strip()
|
|
)
|
|
db.session.add(entreprise)
|
|
db.session.commit()
|
|
db.session.refresh(entreprise)
|
|
contact = EntrepriseContact(
|
|
entreprise_id=entreprise.id,
|
|
nom=form.nom_contact.data.strip(),
|
|
prenom=form.prenom_contact.data.strip(),
|
|
telephone=form.telephone.data.strip(),
|
|
mail=form.mail.data.strip(),
|
|
poste=form.poste.data.strip(),
|
|
service=form.service.data.strip()
|
|
)
|
|
db.session.add(contact)
|
|
nom_entreprise = f"<a href=/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}>{entreprise.nom}</a>"
|
|
log = EntrepriseLog(
|
|
authenticated_user = current_user.user_name,
|
|
text = f"{nom_entreprise} - Création de la fiche entreprise ({entreprise.nom}) avec un contact",
|
|
)
|
|
db.session.add(log)
|
|
db.session.commit()
|
|
flash("L'entreprise a été ajouté à la liste.")
|
|
return redirect(url_for("entreprises.index"))
|
|
return render_template("entreprises/ajout_entreprise.html", title=("Ajout entreprise + contact"), form=form)
|
|
|
|
@bp.route("/edit_entreprise/<int:id>", methods=["GET", "POST"])
|
|
def edit_entreprise(id):
|
|
entreprise = Entreprise.query.filter_by(id=id).first_or_404()
|
|
form = EntrepriseModificationForm()
|
|
if form.validate_on_submit():
|
|
nom_entreprise = f"<a href=/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}>{form.nom.data.strip()}</a>"
|
|
if entreprise.nom != form.nom.data.strip():
|
|
log = EntrepriseLog(
|
|
authenticated_user = current_user.user_name,
|
|
object = entreprise.id,
|
|
text = f"{nom_entreprise} - Modification du nom (ancien nom : {entreprise.nom})",
|
|
)
|
|
entreprise.nom = form.nom.data.strip()
|
|
db.session.add(log)
|
|
if entreprise.adresse != form.adresse.data.strip():
|
|
log = EntrepriseLog(
|
|
authenticated_user = current_user.user_name,
|
|
object = entreprise.id,
|
|
text = f"{nom_entreprise} - Modification de l'adresse (ancienne adresse : {entreprise.adresse})",
|
|
)
|
|
entreprise.adresse = form.adresse.data.strip()
|
|
db.session.add(log)
|
|
if entreprise.codepostal != form.codepostal.data.strip():
|
|
log = EntrepriseLog(
|
|
authenticated_user = current_user.user_name,
|
|
object = entreprise.id,
|
|
text = f"{nom_entreprise} - Modification du code postal (ancien code postal : {entreprise.codepostal})",
|
|
)
|
|
entreprise.codepostal = form.codepostal.data.strip()
|
|
db.session.add(log)
|
|
if entreprise.ville != form.ville.data.strip():
|
|
log = EntrepriseLog(
|
|
authenticated_user = current_user.user_name,
|
|
object = entreprise.id,
|
|
text = f"{nom_entreprise} - Modification de la ville (ancienne ville : {entreprise.ville})",
|
|
)
|
|
entreprise.ville = form.ville.data.strip()
|
|
db.session.add(log)
|
|
if entreprise.pays != form.pays.data.strip():
|
|
log = EntrepriseLog(
|
|
authenticated_user = current_user.user_name,
|
|
object = entreprise.id,
|
|
text = f"{nom_entreprise} - Modification du pays (ancien pays : {entreprise.pays})",
|
|
)
|
|
entreprise.pays = form.pays.data.strip()
|
|
db.session.add(log)
|
|
db.session.commit()
|
|
flash("L'entreprise a été modifié.")
|
|
return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id))
|
|
elif request.method == 'GET':
|
|
form.siret.data = entreprise.siret
|
|
form.nom.data = entreprise.nom
|
|
form.adresse.data = entreprise.adresse
|
|
form.codepostal.data = entreprise.codepostal
|
|
form.ville.data = entreprise.ville
|
|
form.pays.data = entreprise.pays
|
|
return render_template("entreprises/form.html", title=("Modification entreprise"), form=form)
|
|
|
|
@bp.route("/delete_entreprise/<int:id>", methods=["GET", "POST"])
|
|
def delete_entreprise(id):
|
|
entreprise = Entreprise.query.filter_by(id=id).first_or_404()
|
|
form = SuppressionConfirmationForm()
|
|
if form.validate_on_submit():
|
|
db.session.delete(entreprise)
|
|
log = EntrepriseLog(
|
|
authenticated_user = current_user.user_name,
|
|
object = entreprise.id,
|
|
text = f"Suppression de la fiche entreprise ({entreprise.nom})",
|
|
)
|
|
db.session.add(log)
|
|
db.session.commit()
|
|
flash("L'entreprise a été supprimé de la liste.")
|
|
return redirect(url_for("entreprises.index"))
|
|
return render_template("entreprises/delete_confirmation.html", title=("Supression entreprise"), form=form)
|
|
|
|
@bp.route("/add_offre/<int:id>", methods=["GET", "POST"])
|
|
def add_offre(id):
|
|
entreprise = Entreprise.query.filter_by(id=id).first_or_404()
|
|
form = OffreCreationForm()
|
|
if form.validate_on_submit():
|
|
offre = EntrepriseOffre(
|
|
entreprise_id=entreprise.id,
|
|
intitule=form.intitule.data.strip(),
|
|
description=form.description.data.strip(),
|
|
type_offre=form.type_offre.data.strip(),
|
|
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.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/<int:id>", methods=["GET", "POST"])
|
|
def edit_offre(id):
|
|
offre = EntrepriseOffre.query.filter_by(id=id).first_or_404()
|
|
form = OffreModificationForm()
|
|
if form.validate_on_submit():
|
|
offre.intitule = form.intitule.data.strip()
|
|
offre.description = form.description.data.strip()
|
|
offre.type_offre = form.type_offre.data.strip()
|
|
offre.missions = form.missions.data.strip()
|
|
offre.duree = form.duree.data.strip()
|
|
log = EntrepriseLog(
|
|
authenticated_user = current_user.user_name,
|
|
object = offre.entreprise_id,
|
|
text = "Modification d'une offre",
|
|
)
|
|
db.session.add(log)
|
|
db.session.commit()
|
|
flash("L'offre a été modifié.")
|
|
return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise.id))
|
|
elif request.method == 'GET':
|
|
form.intitule.data = offre.intitule
|
|
form.description.data = offre.description
|
|
form.type_offre.data = offre.type_offre
|
|
form.missions.data = offre.missions
|
|
form.duree.data = offre.duree
|
|
return render_template("entreprises/form.html", title=("Modification offre"), form=form)
|
|
|
|
@bp.route("/delete_offre/<int:id>", methods=["GET", "POST"])
|
|
def delete_offre(id):
|
|
offre = EntrepriseOffre.query.filter_by(id=id).first_or_404()
|
|
entreprise_id = offre.entreprise.id
|
|
form = SuppressionConfirmationForm()
|
|
if form.validate_on_submit():
|
|
db.session.delete(offre)
|
|
log = EntrepriseLog(
|
|
authenticated_user = current_user.user_name,
|
|
object = offre.entreprise_id,
|
|
text = "Suppression d'une offre",
|
|
)
|
|
db.session.add(log)
|
|
db.session.commit()
|
|
flash("L'offre a été supprimé de la fiche entreprise.")
|
|
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/<int:id>", methods=["GET", "POST"])
|
|
def add_contact(id):
|
|
entreprise = Entreprise.query.filter_by(id=id).first_or_404()
|
|
form = ContactCreationForm(hidden_entreprise_id=entreprise.id)
|
|
if form.validate_on_submit():
|
|
contact = EntrepriseContact(
|
|
entreprise_id=entreprise.id,
|
|
nom=form.nom.data.strip(),
|
|
prenom=form.prenom.data.strip(),
|
|
telephone=form.telephone.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,
|
|
object = entreprise.id,
|
|
text = "Création d'un contact",
|
|
)
|
|
db.session.add(log)
|
|
db.session.add(contact)
|
|
db.session.commit()
|
|
flash("Le contact a été ajouté à la fiche entreprise.")
|
|
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/<int:id>", methods=["GET", "POST"])
|
|
def edit_contact(id):
|
|
contact = EntrepriseContact.query.filter_by(id=id).first_or_404()
|
|
form = ContactModificationForm()
|
|
if form.validate_on_submit():
|
|
contact.nom = form.nom.data.strip()
|
|
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,
|
|
text = "Modification d'un contact",
|
|
)
|
|
db.session.add(log)
|
|
db.session.commit()
|
|
flash("Le contact a été modifié.")
|
|
return redirect(url_for("entreprises.fiche_entreprise", id=contact.entreprise.id))
|
|
elif request.method == 'GET':
|
|
form.nom.data = contact.nom
|
|
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/<int:id>", methods=["GET", "POST"])
|
|
def delete_contact(id):
|
|
contact = EntrepriseContact.query.filter_by(id=id).first_or_404()
|
|
entreprise_id = contact.entreprise.id
|
|
form = SuppressionConfirmationForm()
|
|
if form.validate_on_submit():
|
|
contact_count = EntrepriseContact.query.filter_by(entreprise_id=contact.entreprise.id).count()
|
|
if contact_count == 1:
|
|
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))
|
|
else:
|
|
db.session.delete(contact)
|
|
log = EntrepriseLog(
|
|
authenticated_user = current_user.user_name,
|
|
object = contact.entreprise_id,
|
|
text = "Suppression d'un contact",
|
|
)
|
|
db.session.add(log)
|
|
db.session.commit()
|
|
flash("Le contact a été supprimé de la fiche entreprise.")
|
|
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/<int:id>", methods=["GET", "POST"])
|
|
def add_historique(id):
|
|
entreprise = Entreprise.query.filter_by(id=id).first_or_404()
|
|
form = HistoriqueCreationForm()
|
|
if form.validate_on_submit():
|
|
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")
|
|
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(
|
|
entreprise_id = entreprise.id,
|
|
etudid = etudiant.id,
|
|
type_offre = form.type_offre.data.strip(),
|
|
date_debut = form.date_debut.data,
|
|
date_fin = form.date_fin.data,
|
|
formation_text = formation.formsemestre.titre
|
|
if formation else None,
|
|
formation_scodoc = formation.formsemestre.formsemestre_id
|
|
if formation else None
|
|
)
|
|
db.session.add(historique)
|
|
db.session.commit()
|
|
flash("L'étudiant a été ajouté sur la fiche entreprise.")
|
|
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/<int:id>", methods=["GET", "POST"])
|
|
def envoyer_offre(id):
|
|
offre = EntrepriseOffre.query.filter_by(id=id).first_or_404()
|
|
form = EnvoiOffreForm()
|
|
if form.validate_on_submit():
|
|
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")
|
|
def json_etudiants():
|
|
term = request.args.get('term').strip()
|
|
etudiants = Identite.query.filter(Identite.nom.ilike(f"%{term}%")).all()
|
|
list = []
|
|
content = {}
|
|
for etudiant in etudiants:
|
|
value = f"{sco_etud.format_nom(etudiant.nom)} {sco_etud.format_prenom(etudiant.prenom)}"
|
|
if etudiant.inscription_courante() is not None:
|
|
content = {
|
|
"id": f"{etudiant.id}",
|
|
"value": value,
|
|
"info": f"{etudiant.inscription_courante().formsemestre.titre}"
|
|
}
|
|
else:
|
|
content = {
|
|
"id": f"{etudiant.id}",
|
|
"value": value
|
|
}
|
|
list.append(content)
|
|
content = {}
|
|
return jsonify(results=list)
|
|
|
|
@bp.route("/responsables")
|
|
def json_responsables():
|
|
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()
|
|
list = []
|
|
content = {}
|
|
for responsable in responsables:
|
|
value = f"{responsable.get_nomplogin()}"
|
|
content = {
|
|
"id": f"{responsable.id}",
|
|
"value": value,
|
|
"info": ""
|
|
}
|
|
list.append(content)
|
|
content = {}
|
|
return jsonify(results=list)
|
|
|
|
@bp.route("/export_entreprises")
|
|
def export_entreprises():
|
|
entreprises = Entreprise.query.all()
|
|
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)
|
|
|
|
@bp.route("/download_offre/<int:entreprise_id>/<int:offre_id>/<string:filename>")
|
|
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) |