374 lines
16 KiB
Python
374 lines
16 KiB
Python
from flask import render_template, redirect, url_for, request, flash
|
|
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.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,
|
|
EntrepriseHistory
|
|
)
|
|
from app.models import (
|
|
Identite
|
|
)
|
|
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 sqlalchemy import text
|
|
|
|
@bp.route("/", methods=["GET", "POST"])
|
|
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("/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()
|
|
)
|
|
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/form.html", title=("Ajout entreprise + contact"), form=form)
|
|
|
|
@bp.route("/edit_entreprise/<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/<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("/fiche_entreprise/<id>", methods=["GET", "POST"])
|
|
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(EntrepriseHistory, Identite).order_by(EntrepriseHistory.date_debut.desc()).\
|
|
filter_by(entreprise_id=id).\
|
|
join(Identite, Identite.id == EntrepriseHistory.etudid).all()
|
|
return render_template("entreprises/fiche_entreprise.html", title=("Fiche entreprise"), entreprise=entreprise, contacts=contacts, offres=offres, logs=logs, historique=historique)
|
|
|
|
@bp.route("/add_offre/<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()
|
|
)
|
|
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/<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/<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/<id>", methods=["GET", "POST"])
|
|
def add_contact(id):
|
|
entreprise = Entreprise.query.filter_by(id=id).first_or_404()
|
|
form = ContactCreationForm()
|
|
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()
|
|
)
|
|
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/<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()
|
|
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
|
|
return render_template("entreprises/form.html", title=("Modification contact"), form=form)
|
|
|
|
@bp.route("/delete_contact/<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/<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 = EntrepriseHistory(
|
|
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/form.html", title=("Ajout historique"), 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("/envoyer_offre/<id>", methods=["GET", "POST"])
|
|
def envoyer_offre(id):
|
|
form = EnvoiOffreForm()
|
|
if form.validate_on_submit():
|
|
print("tmp") # faire l'envoie du mail
|
|
return render_template("entreprises/form.html", title=("Envoyer une offre"), form=form)
|
|
|
|
@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) |