diff --git a/app/entreprises/__init__.py b/app/entreprises/__init__.py index 44fd128e..121b7e3c 100644 --- a/app/entreprises/__init__.py +++ b/app/entreprises/__init__.py @@ -10,6 +10,7 @@ from app.models import Departement bp = Blueprint("entreprises", __name__) LOGS_LEN = 5 +SIRET_PROVISOIRE_START = "xx" @bp.app_template_filter() diff --git a/app/entreprises/app_relations_entreprises.py b/app/entreprises/app_relations_entreprises.py index b5b14dab..c34c602b 100644 --- a/app/entreprises/app_relations_entreprises.py +++ b/app/entreprises/app_relations_entreprises.py @@ -239,7 +239,8 @@ def get_excel_book_are(export: bool = False): ) correspondants = ( db.session.query(EntrepriseCorrespondant) - .join(Entreprise, EntrepriseCorrespondant.entreprise_id == Entreprise.id) + .join(EntrepriseSite, EntrepriseCorrespondant.site_id == EntrepriseSite.id) + .join(Entreprise, EntrepriseSite.entreprise_id == Entreprise.id) .filter_by(visible=True) .all() ) @@ -347,14 +348,15 @@ def check_entreprise_import(entreprise_data): if EntreprisePreferences.get_check_siret(): if re.match("^\d{14}$", siret) is None: return False - try: - req = requests.get( - f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}" - ) - if req.status_code != 200: + else: + try: + req = requests.get( + f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret}" + ) + if req.status_code != 200: + return False + except requests.ConnectionError: return False - except requests.ConnectionError: - return False return True @@ -391,7 +393,6 @@ def check_sites_import(m): sites_import.append(site_import) else: correspondant_import = EntrepriseCorrespondant( - entreprise_id=entreprise.id, civilite=site_data["civilite"], nom=site_data["nom"], prenom=site_data["prenom"], @@ -414,7 +415,6 @@ def check_sites_import(m): if site_data["civilite"] != "": correspondant_import = EntrepriseCorrespondant( - entreprise_id=entreprise.id, site_id=site.id, civilite=site_data["civilite"], nom=site_data["nom"], @@ -527,7 +527,7 @@ def check_correspondant_import(correspondant_data): return False # civilite entre H ou F - if correspondant_data["civilite"] not in ["H", "F"]: + if correspondant_data["civilite"].upper() not in ["H", "F"]: return False if ( @@ -544,13 +544,14 @@ def check_correspondant_import(correspondant_data): return False # correspondant possède le meme nom et prénom dans la meme entreprise - correspondant = EntrepriseCorrespondant.query.filter_by( - nom=correspondant_data["nom"], - prenom=correspondant_data["prenom"], - entreprise_id=entreprise.id, - ).first() - if correspondant is not None: - return False + if correspondant_data["id_site"] != "": + correspondant = EntrepriseCorrespondant.query.filter_by( + nom=correspondant_data["nom"], + prenom=correspondant_data["prenom"], + site_id=correspondant_data["id_site"], + ).first() + if correspondant is not None: + return False return True diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 61d517b7..c0560e45 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -28,6 +28,7 @@ import re import requests from datetime import datetime +from flask import url_for from flask_wtf import FlaskForm from flask_wtf.file import FileField, FileAllowed, FileRequired from markupsafe import Markup @@ -62,8 +63,10 @@ from app.entreprises.models import ( EntrepriseSite, EntrepriseTaxeApprentissage, ) +from app import db from app.models import Identite, Departement from app.auth.models import User +from app.entreprises import SIRET_PROVISOIRE_START CHAMP_REQUIS = "Ce champ est requis" SUBMIT_MARGE = {"style": "margin-bottom: 10px;"} @@ -86,9 +89,11 @@ class EntreprisesFilterForm(FlaskForm): class EntrepriseCreationForm(FlaskForm): - siret = _build_string_field( - "SIRET (*)", + siret = StringField( + "SIRET", + validators=[Optional()], render_kw={"placeholder": "Numéro composé de 14 chiffres"}, + description="Laissez vide pour générer un SIRET provisoire", ) association = BooleanField("Association") nom_entreprise = _build_string_field("Nom de l'entreprise (*)") @@ -99,7 +104,7 @@ class EntrepriseCreationForm(FlaskForm): civilite = SelectField( "Civilité du correspondant", - choices=[("M", "Monsieur"), ("F", "Madame")], + choices=[("H", "Monsieur"), ("F", "Madame")], validators=[DataRequired(message=CHAMP_REQUIS)], ) nom_correspondant = _build_string_field("Nom du correspondant", required=False) @@ -123,6 +128,39 @@ class EntrepriseCreationForm(FlaskForm): if not FlaskForm.validate(self): validate = False + if EntreprisePreferences.get_check_siret() and self.siret.data != "": + siret_data = self.siret.data.strip().replace(" ", "") + self.siret.data = siret_data + if re.match("^\d{14}$", siret_data) is None: + self.siret.errors.append("Format incorrect") + validate = False + else: + try: + req = requests.get( + f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret_data}" + ) + if req.status_code != 200: + self.siret.errors.append("SIRET inexistant") + validate = False + except requests.ConnectionError: + self.siret.errors.append( + "Impossible de vérifier l'existance du SIRET" + ) + validate = False + entreprise = Entreprise.query.filter_by(siret=siret_data).first() + if entreprise is not None: + if entreprise.visible is True: + lien = f"ici" + self.siret.errors.append( + Markup( + f"Entreprise déjà présent, lien vers la fiche : {lien}" + ) + ) + validate = False + else: + self.siret.errors.append("Entreprise en phase de validation") + validate = False + if ( self.nom_correspondant.data.strip() or self.prenom_correspondant.data.strip() @@ -147,30 +185,13 @@ class EntrepriseCreationForm(FlaskForm): return validate - def validate_siret(self, siret): - if EntreprisePreferences.get_check_siret(): - siret_data = siret.data.strip().replace(" ", "") - self.siret.data = siret_data - if re.match("^\d{14}$", siret_data) is None: - raise ValidationError("Format incorrect") - try: - req = requests.get( - f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret_data}" - ) - if req.status_code != 200: - raise ValidationError("SIRET inexistant") - except requests.ConnectionError: - raise ValidationError("Impossible de vérifier l'existance du SIRET") - entreprise = Entreprise.query.filter_by(siret=siret_data).first() - if entreprise is not None: - lien = f'ici' - raise ValidationError( - Markup(f"Entreprise déjà présent, lien vers la fiche : {lien}") - ) - class EntrepriseModificationForm(FlaskForm): siret = StringField("SIRET (*)") + new_siret = StringField( + "Modification du SIRET provisoire (*)", + description="Activé uniquement pour les entreprises avec SIRET provisoire", + ) association = BooleanField("Association") nom = _build_string_field("Nom de l'entreprise (*)") adresse = _build_string_field("Adresse (*)") @@ -182,6 +203,37 @@ class EntrepriseModificationForm(FlaskForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.siret.render_kw = {"disabled": ""} + if self.siret.data.startswith(SIRET_PROVISOIRE_START) is True: + self.new_siret.validators = [Optional()] + else: + self.new_siret.render_kw = {"disabled": ""} + + def validate_new_siret(self, new_siret): + if EntreprisePreferences.get_check_siret() and new_siret.data is not None: + siret_data = new_siret.data.strip().replace(" ", "") + self.new_siret.data = siret_data + if re.match("^\d{14}$", siret_data) is None: + raise ValidationError("Format incorrect") + else: + try: + req = requests.get( + f"https://entreprise.data.gouv.fr/api/sirene/v1/siret/{siret_data}" + ) + if req.status_code != 200: + raise ValidationError("SIRET inexistant") + except requests.ConnectionError: + raise ValidationError("Impossible de vérifier l'existance du SIRET") + entreprise = Entreprise.query.filter_by(siret=siret_data).first() + if entreprise is not None: + if entreprise.visible is True: + lien = f'ici' + raise ValidationError( + Markup( + f"Entreprise déjà présent, lien vers la fiche : {lien}" + ) + ) + else: + raise ValidationError("Entreprise en phase de validation") class SiteCreationForm(FlaskForm): @@ -274,9 +326,10 @@ class OffreCreationForm(FlaskForm): self.correspondant.choices = [("", "")] + [ (correspondant.id, f"{correspondant.nom} {correspondant.prenom}") - for correspondant in EntrepriseCorrespondant.query.filter_by( - entreprise_id=self.hidden_entreprise_id.data - ) + for correspondant in db.session.query(EntrepriseCorrespondant) + .join(EntrepriseSite, EntrepriseCorrespondant.site_id == EntrepriseSite.id) + .filter(EntrepriseSite.entreprise_id == self.hidden_entreprise_id.data) + .all() ] self.depts.choices = [ @@ -320,9 +373,10 @@ class OffreModificationForm(FlaskForm): self.correspondant.choices = [("", "")] + [ (correspondant.id, f"{correspondant.nom} {correspondant.prenom}") - for correspondant in EntrepriseCorrespondant.query.filter_by( - entreprise_id=self.hidden_entreprise_id.data - ) + for correspondant in db.session.query(EntrepriseCorrespondant) + .join(EntrepriseSite, EntrepriseCorrespondant.site_id == EntrepriseSite.id) + .filter(EntrepriseSite.entreprise_id == self.hidden_entreprise_id.data) + .all() ] self.depts.choices = [ @@ -393,7 +447,7 @@ class CorrespondantCreationForm(FlaskForm): class CorrespondantsCreationForm(FlaskForm): - hidden_entreprise_id = HiddenField() + hidden_site_id = HiddenField() correspondants = FieldList(FormField(CorrespondantCreationForm), min_entries=1) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE) @@ -418,7 +472,7 @@ class CorrespondantsCreationForm(FlaskForm): (entry.nom.data.strip(), entry.prenom.data.strip()) ) correspondant = EntrepriseCorrespondant.query.filter_by( - entreprise_id=self.hidden_entreprise_id.data, + site_id=self.hidden_site_id.data, nom=entry.nom.data, prenom=entry.prenom.data, ).first() @@ -433,7 +487,7 @@ class CorrespondantsCreationForm(FlaskForm): class CorrespondantModificationForm(FlaskForm): hidden_correspondant_id = HiddenField() - hidden_entreprise_id = HiddenField() + hidden_site_id = HiddenField() civilite = SelectField( "Civilité (*)", choices=[("H", "Monsieur"), ("F", "Madame")], @@ -459,7 +513,7 @@ class CorrespondantModificationForm(FlaskForm): correspondant = EntrepriseCorrespondant.query.filter( EntrepriseCorrespondant.id != self.hidden_correspondant_id.data, - EntrepriseCorrespondant.entreprise_id == self.hidden_entreprise_id.data, + EntrepriseCorrespondant.site_id == self.hidden_site_id.data, EntrepriseCorrespondant.nom == self.nom.data, EntrepriseCorrespondant.prenom == self.prenom.data, ).first() diff --git a/app/entreprises/models.py b/app/entreprises/models.py index 4edbe458..0ad22f25 100644 --- a/app/entreprises/models.py +++ b/app/entreprises/models.py @@ -4,7 +4,8 @@ from app import db class Entreprise(db.Model): __tablename__ = "are_entreprises" id = db.Column(db.Integer, primary_key=True) - siret = db.Column(db.Text) + siret = db.Column(db.Text, index=True, unique=True) + siret_provisoire = db.Column(db.Boolean, default=False) nom = db.Column(db.Text) adresse = db.Column(db.Text) codepostal = db.Column(db.Text) @@ -79,9 +80,6 @@ class EntrepriseSite(db.Model): class EntrepriseCorrespondant(db.Model): __tablename__ = "are_correspondants" id = db.Column(db.Integer, primary_key=True) - entreprise_id = db.Column( - db.Integer, db.ForeignKey("are_entreprises.id", ondelete="cascade") - ) site_id = db.Column(db.Integer, db.ForeignKey("are_sites.id", ondelete="cascade")) civilite = db.Column(db.String(1)) nom = db.Column(db.Text) @@ -262,3 +260,19 @@ class EntreprisePreferences(db.Model): else: cs.value = check_siret db.session.commit() + + +def entreprises_reset_database(): + db.session.query(EntrepriseContact).delete() + db.session.query(EntrepriseStageApprentissage).delete() + db.session.query(EntrepriseTaxeApprentissage).delete() + db.session.query(EntrepriseCorrespondant).delete() + db.session.query(EntrepriseSite).delete() + db.session.query(EntrepriseEnvoiOffre).delete() + db.session.query(EntrepriseEnvoiOffreEtudiant).delete() + db.session.query(EntrepriseOffreDepartement).delete() + db.session.query(EntrepriseOffre).delete() + db.session.query(Entreprise).delete() + db.session.query(EntrepriseHistorique).delete() + db.session.query(EntreprisePreferences).delete() + db.session.commit() diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 6a831d86..7e16ee5b 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -10,7 +10,7 @@ from flask_login import current_user from app.decorators import permission_required -from app.entreprises import LOGS_LEN +from app.entreprises import LOGS_LEN, SIRET_PROVISOIRE_START from app.entreprises.forms import ( ActivationConfirmationForm, CorrespondantsCreationForm, @@ -157,18 +157,18 @@ def correspondants(): ) -@bp.route("/fiche_entreprise/", methods=["GET"]) +@bp.route("/fiche_entreprise/", methods=["GET"]) @permission_required(Permission.RelationsEntreprisesView) -def fiche_entreprise(id): +def fiche_entreprise(entreprise_id): """ Permet d'afficher la fiche entreprise d'une entreprise avec une liste des dernières opérations et l'historique des étudiants ayant réaliser un stage ou une alternance dans cette entreprise. La fiche entreprise comporte les informations de l'entreprise, les correspondants de l'entreprise et les offres de l'entreprise. """ - entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( - description=f"fiche entreprise {id} inconnue" - ) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=True + ).first_or_404(description=f"fiche entreprise {entreprise_id} inconnue") offres_with_files = [] depts = are.get_depts() for offre in entreprise.offres: @@ -185,19 +185,19 @@ def fiche_entreprise(id): sites = entreprise.sites[:] logs = ( EntrepriseHistorique.query.order_by(EntrepriseHistorique.date.desc()) - .filter(EntrepriseHistorique.entreprise_id == id) + .filter(EntrepriseHistorique.entreprise_id == entreprise.id) .limit(LOGS_LEN) .all() ) stages_apprentissages = ( db.session.query(EntrepriseStageApprentissage, Identite) .order_by(EntrepriseStageApprentissage.date_debut.desc()) - .filter(EntrepriseStageApprentissage.entreprise_id == id) + .filter(EntrepriseStageApprentissage.entreprise_id == entreprise.id) .join(Identite, Identite.id == EntrepriseStageApprentissage.etudid) .all() ) taxes = ( - EntrepriseTaxeApprentissage.query.filter_by(entreprise_id=id) + EntrepriseTaxeApprentissage.query.filter_by(entreprise_id=entreprise.id) .order_by(EntrepriseTaxeApprentissage.annee.desc()) .all() ) @@ -213,16 +213,16 @@ def fiche_entreprise(id): ) -@bp.route("/fiche_entreprise//logs", methods=["GET"]) +@bp.route("/fiche_entreprise//logs", methods=["GET"]) @permission_required(Permission.RelationsEntreprisesView) -def logs_entreprise(id): +def logs_entreprise(entreprise_id): """ Permet d'afficher les logs d'une entreprise """ page = request.args.get("page", 1, type=int) - entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( - description=f"logs fiche entreprise {id} inconnu" - ) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=True + ).first_or_404(description=f"logs fiche entreprise {entreprise_id} inconnu") logs = ( EntrepriseHistorique.query.order_by(EntrepriseHistorique.date.desc()) .filter(EntrepriseHistorique.entreprise_id == entreprise.id) @@ -236,14 +236,16 @@ def logs_entreprise(id): ) -@bp.route("/fiche_entreprise_validation/", methods=["GET"]) +@bp.route("/fiche_entreprise_validation/", methods=["GET"]) @permission_required(Permission.RelationsEntreprisesValidate) -def fiche_entreprise_validation(id): +def fiche_entreprise_validation(entreprise_id): """ Permet d'afficher la fiche entreprise d'une entreprise a valider """ - entreprise = Entreprise.query.filter_by(id=id, visible=False).first_or_404( - description=f"fiche entreprise (validation) {id} inconnue" + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=False + ).first_or_404( + description=f"fiche entreprise (validation) {entreprise_id} inconnue" ) sites = entreprise.sites[:] return render_template( @@ -293,15 +295,15 @@ def offres_recues(): ) -@bp.route("/fiche_entreprise//offres_expirees") +@bp.route("/fiche_entreprise//offres_expirees") @permission_required(Permission.RelationsEntreprisesView) -def offres_expirees(id): +def offres_expirees(entreprise_id): """ Permet d'afficher la liste des offres expirés d'une entreprise """ - entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( - description=f"fiche entreprise {id} inconnue" - ) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=True + ).first_or_404(description=f"fiche entreprise {entreprise_id} inconnue") offres_expirees_with_files = [] depts = are.get_depts() for offre in entreprise.offres: @@ -329,16 +331,28 @@ def add_entreprise(): if form.validate_on_submit(): entreprise = Entreprise( nom=form.nom_entreprise.data.strip(), - siret=form.siret.data.strip(), + siret=form.siret.data.strip() + if form.siret.data.strip() + else f"{SIRET_PROVISOIRE_START}{datetime.now().strftime('%d%m%y%H%M%S')}", # siret provisoire + siret_provisoire=False if form.siret.data.strip() else True, association=form.association.data, adresse=form.adresse.data.strip(), codepostal=form.codepostal.data.strip(), ville=form.ville.data.strip(), pays=form.pays.data.strip() if form.pays.data.strip() else "FRANCE", ) - db.session.add(entreprise) - db.session.commit() - db.session.refresh(entreprise) + try: + db.session.add(entreprise) + db.session.commit() + db.session.refresh(entreprise) + except: + db.session.rollback() + flash("Une erreur est survenue veuillez réessayer.") + return render_template( + "entreprises/form_ajout_entreprise.html", + title="Ajout entreprise avec correspondant", + form=form, + ) site = EntrepriseSite( entreprise_id=entreprise.id, nom=form.nom_entreprise.data.strip(), @@ -352,7 +366,6 @@ def add_entreprise(): if form.nom_correspondant.data.strip(): db.session.refresh(site) correspondant = EntrepriseCorrespondant( - entreprise_id=entreprise.id, site_id=site.id, civilite=form.civilite.data, nom=form.nom_correspondant.data.strip(), @@ -367,10 +380,10 @@ def add_entreprise(): db.session.add(correspondant) if current_user.has_permission(Permission.RelationsEntreprisesValidate, None): entreprise.visible = True - nom_entreprise = f"{entreprise.nom}" + lien_entreprise = f"{entreprise.nom}" log = EntrepriseHistorique( authenticated_user=current_user.user_name, - text=f"{nom_entreprise} - Création de la fiche entreprise ({entreprise.nom})", + text=f"{lien_entreprise} - Création de la fiche entreprise ({entreprise.nom})", entreprise_id=entreprise.id, ) db.session.add(log) @@ -387,70 +400,76 @@ def add_entreprise(): flash("L'entreprise a été ajouté à la liste pour la validation.") return redirect(url_for("entreprises.index")) return render_template( - "entreprises/ajout_entreprise.html", + "entreprises/form_ajout_entreprise.html", title="Ajout entreprise avec correspondant", form=form, ) -@bp.route("/fiche_entreprise/edit_entreprise/", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//edit_entreprise", methods=["GET", "POST"] +) @permission_required(Permission.RelationsEntreprisesChange) -def edit_entreprise(id): +def edit_entreprise(entreprise_id): """ Permet de modifier une entreprise de la base avec un formulaire """ - entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( - description=f"entreprise {id} inconnue" - ) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=True + ).first_or_404(description=f"entreprise {entreprise_id} inconnue") form = EntrepriseModificationForm(siret=entreprise.siret) if form.validate_on_submit(): - nom_entreprise = f"{form.nom.data.strip()}" + lien_entreprise = f"{form.nom.data.strip()}" + logs_text = [] + if form.new_siret.data: + logs_text.append(f"{lien_entreprise} - Modification du SIRET") + entreprise.siret = form.new_siret.data.strip() if entreprise.nom != form.nom.data.strip(): - log = EntrepriseHistorique( - authenticated_user=current_user.user_name, - entreprise_id=entreprise.id, - text=f"{nom_entreprise} - Modification du nom (ancien nom: {entreprise.nom})", + logs_text.append( + f"{lien_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 = EntrepriseHistorique( - authenticated_user=current_user.user_name, - entreprise_id=entreprise.id, - text=f"{nom_entreprise} - Modification de l'adresse (ancienne adresse: {entreprise.adresse})", + logs_text.append( + f"{lien_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 = EntrepriseHistorique( - authenticated_user=current_user.user_name, - entreprise_id=entreprise.id, - text=f"{nom_entreprise} - Modification du code postal (ancien code postal: {entreprise.codepostal})", + logs_text.append( + f"{lien_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 = EntrepriseHistorique( - authenticated_user=current_user.user_name, - entreprise_id=entreprise.id, - text=f"{nom_entreprise} - Modification de la ville (ancienne ville: {entreprise.ville})", + logs_text.append( + f"{lien_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() or not form.pays.data.strip(): - log = EntrepriseHistorique( - authenticated_user=current_user.user_name, - entreprise_id=entreprise.id, - text=f"{nom_entreprise} - Modification du pays (ancien pays: {entreprise.pays})", + logs_text.append( + f"{lien_entreprise} - Modification du pays (ancien pays: {entreprise.pays})" ) entreprise.pays = ( form.pays.data.strip() if form.pays.data.strip() else "FRANCE" ) - db.session.add(log) + entreprise.association = form.association.data + for log_text in logs_text: + db.session.add( + EntrepriseHistorique( + authenticated_user=current_user.user_name, + entreprise_id=entreprise.id, + text=log_text, + ) + ) db.session.commit() flash("L'entreprise a été modifié.") - return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=entreprise.id) + ) elif request.method == "GET": form.siret.data = entreprise.siret form.nom.data = entreprise.nom @@ -466,62 +485,69 @@ def edit_entreprise(id): ) -@bp.route("/fiche_entreprise/desactiver/", methods=["GET", "POST"]) +@bp.route("/fiche_entreprise//desactiver", methods=["GET", "POST"]) @permission_required(Permission.RelationsEntreprisesChange) -def fiche_entreprise_desactiver(id): +def fiche_entreprise_desactiver(entreprise_id): """ Permet de désactiver une entreprise """ entreprise = Entreprise.query.filter_by( - id=id, visible=True, active=True - ).first_or_404(description=f"entreprise {id} inconnue") + id=entreprise_id, visible=True, active=True + ).first_or_404(description=f"entreprise {entreprise_id} inconnue") form = DesactivationConfirmationForm() if form.validate_on_submit(): entreprise.notes_active = form.notes_active.data.strip() entreprise.active = False db.session.commit() flash("L'entreprise a été désactivé.") - return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=entreprise.id) + ) return render_template( - "entreprises/confirmation_form.html", + "entreprises/form_confirmation.html", title="Désactiver entreprise", form=form, info_message="Cliquez sur le bouton Modifier pour confirmer la désactivation", ) -@bp.route("/fiche_entreprise/activer/", methods=["GET", "POST"]) +@bp.route("/fiche_entreprise//activer", methods=["GET", "POST"]) @permission_required(Permission.RelationsEntreprisesChange) -def fiche_entreprise_activer(id): +def fiche_entreprise_activer(entreprise_id): """ Permet d'activer une entreprise """ entreprise = Entreprise.query.filter_by( - id=id, visible=True, active=False - ).first_or_404(description=f"entreprise {id} inconnue") + id=entreprise_id, visible=True, active=False + ).first_or_404(description=f"entreprise {entreprise_id} inconnue") form = ActivationConfirmationForm() if form.validate_on_submit(): entreprise.active = True db.session.commit() flash("L'entreprise a été activé.") - return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=entreprise.id) + ) return render_template( - "entreprises/confirmation_form.html", + "entreprises/form_confirmation.html", title="Activer entreprise", form=form, info_message="Cliquez sur le bouton Modifier pour confirmer l'activaction", ) -@bp.route("/fiche_entreprise//add_taxe_apprentissage", methods=["GET", "POST"]) -def add_taxe_apprentissage(id): +@bp.route( + "/fiche_entreprise//add_taxe_apprentissage", + methods=["GET", "POST"], +) +def add_taxe_apprentissage(entreprise_id): """ - Permet d'ajouter une taxe d'apprentissage sur un fiche entreprise + Permet d'ajouter une taxe d'apprentissage sur une fiche entreprise """ - entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( - description=f"entreprise {id} inconnue" - ) - form = TaxeApprentissageForm(hidden_entreprise_id=id) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=True + ).first_or_404(description=f"entreprise {entreprise_id} inconnue") + form = TaxeApprentissageForm(hidden_entreprise_id=entreprise.id) if form.validate_on_submit(): taxe = EntrepriseTaxeApprentissage( entreprise_id=entreprise.id, @@ -531,7 +557,9 @@ def add_taxe_apprentissage(id): ) db.session.add(taxe) db.session.commit() - return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=entreprise.id) + ) return render_template( "entreprises/form.html", title="Ajout taxe apprentissage", @@ -540,25 +568,26 @@ def add_taxe_apprentissage(id): @bp.route( - "/fiche_entreprise//edit_taxe_apprentissage/", + "/fiche_entreprise//edit_taxe_apprentissage/", methods=["GET", "POST"], ) -def edit_taxe_apprentissage(id_entreprise, id_taxe): +def edit_taxe_apprentissage(entreprise_id, taxe_id): """ - Permet de modifier une taxe d'apprentissage sur un fiche entreprise + Permet de modifier une taxe d'apprentissage sur une fiche entreprise """ - entreprise = Entreprise.query.filter_by( - id=id_entreprise, visible=True - ).first_or_404(description=f"entreprise {id_entreprise} inconnue") - taxe = EntrepriseTaxeApprentissage.query.filter_by(id=id_taxe).first_or_404( - description=f"taxe d'apprentissage {id_taxe} inconnue" + taxe = EntrepriseTaxeApprentissage.query.filter_by( + id=taxe_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"taxe d'apprentissage {taxe_id} inconnue pour l'entreprise {entreprise_id}" ) form = TaxeApprentissageModificationForm(annee=taxe.annee) if form.validate_on_submit(): taxe.montant = form.montant.data taxe.notes = form.notes.data.strip() db.session.commit() - return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=taxe.entreprise_id) + ) elif request.method == "GET": form.montant.data = taxe.montant form.notes.data = taxe.notes @@ -570,27 +599,28 @@ def edit_taxe_apprentissage(id_entreprise, id_taxe): @bp.route( - "/fiche_entreprise//delete_taxe_apprentissage/", + "/fiche_entreprise//delete_taxe_apprentissage/", methods=["GET", "POST"], ) -def delete_taxe_apprentissage(id_entreprise, id_taxe): +def delete_taxe_apprentissage(entreprise_id, taxe_id): """ - Permet de modifier une taxe d'apprentissage sur un fiche entreprise + Permet de modifier une taxe d'apprentissage sur une fiche entreprise """ - entreprise = Entreprise.query.filter_by( - id=id_entreprise, visible=True - ).first_or_404(description=f"entreprise {id_entreprise} inconnue") - taxe = EntrepriseTaxeApprentissage.query.filter_by(id=id_taxe).first_or_404( - description=f"taxe d'apprentissage {id_taxe} inconnue" + taxe = EntrepriseTaxeApprentissage.query.filter_by( + id=taxe_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"taxe d'apprentissage {taxe_id} inconnue pour l'entreprise {entreprise_id}" ) form = SuppressionConfirmationForm() if form.validate_on_submit(): db.session.delete(taxe) db.session.commit() flash("La taxe d'apprentissage a été supprimé de la liste.") - return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=taxe.entreprise_id) + ) return render_template( - "entreprises/confirmation_form.html", + "entreprises/form_confirmation.html", title="Supprimer taxe apprentissage", form=form, info_message="Cliquez sur le bouton Supprimer pour confirmer votre supression", @@ -598,17 +628,18 @@ def delete_taxe_apprentissage(id_entreprise, id_taxe): @bp.route( - "/fiche_entreprise_validation//validate_entreprise", methods=["GET", "POST"] + "/fiche_entreprise_validation//validate_entreprise", + methods=["GET", "POST"], ) @permission_required(Permission.RelationsEntreprisesValidate) -def validate_entreprise(id): +def validate_entreprise(entreprise_id): """ Permet de valider une entreprise """ form = ValidationConfirmationForm() - entreprise = Entreprise.query.filter_by(id=id, visible=False).first_or_404( - description=f"entreprise (validation) {id} inconnue" - ) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=False + ).first_or_404(description=f"entreprise (validation) {entreprise_id} inconnue") if form.validate_on_submit(): entreprise.visible = True nom_entreprise = f"{entreprise.nom}" @@ -620,26 +651,26 @@ def validate_entreprise(id): db.session.add(log) db.session.commit() flash("L'entreprise a été validé et ajouté à la liste.") - return redirect(url_for("entreprises.index")) + return redirect(url_for("entreprises.validation")) return render_template( - "entreprises/validate_confirmation.html", + "entreprises/form_validate_confirmation.html", title="Validation entreprise", form=form, ) @bp.route( - "/fiche_entreprise_validation//delete_validation_entreprise", + "/fiche_entreprise_validation//delete_validation_entreprise", methods=["GET", "POST"], ) @permission_required(Permission.RelationsEntreprisesValidate) -def delete_validation_entreprise(id): +def delete_validation_entreprise(entreprise_id): """ Permet de supprimer une entreprise en attente de validation avec une formulaire de validation """ - entreprise = Entreprise.query.filter_by(id=id, visible=False).first_or_404( - description=f"entreprise (validation) {id} inconnue" - ) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=False + ).first_or_404(description=f"entreprise (validation) {entreprise_id} inconnue") form = SuppressionConfirmationForm() if form.validate_on_submit(): db.session.delete(entreprise) @@ -647,23 +678,23 @@ def delete_validation_entreprise(id): flash("L'entreprise a été supprimé de la liste des entreprise à valider.") return redirect(url_for("entreprises.validation")) return render_template( - "entreprises/confirmation_form.html", + "entreprises/form_confirmation.html", title="Supression entreprise", form=form, info_message="Cliquez sur le bouton Supprimer pour confirmer votre supression", ) -@bp.route("/fiche_entreprise//add_offre", methods=["GET", "POST"]) +@bp.route("/fiche_entreprise//add_offre", methods=["GET", "POST"]) @permission_required(Permission.RelationsEntreprisesChange) -def add_offre(id): +def add_offre(entreprise_id): """ Permet d'ajouter une offre a une entreprise """ - entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( - description=f"entreprise {id} inconnue" - ) - form = OffreCreationForm(hidden_entreprise_id=id) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=True + ).first_or_404(description=f"entreprise {entreprise_id} inconnue") + form = OffreCreationForm(hidden_entreprise_id=entreprise.id) if form.validate_on_submit(): offre = EntrepriseOffre( entreprise_id=entreprise.id, @@ -709,7 +740,9 @@ def add_offre(id): 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 redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=entreprise.id) + ) return render_template( "entreprises/form.html", title="Ajout offre", @@ -717,14 +750,19 @@ def add_offre(id): ) -@bp.route("/fiche_entreprise/edit_offre/", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//edit_offre/", + methods=["GET", "POST"], +) @permission_required(Permission.RelationsEntreprisesChange) -def edit_offre(id): +def edit_offre(entreprise_id, offre_id): """ Permet de modifier une offre """ - offre = EntrepriseOffre.query.filter_by(id=id).first_or_404( - description=f"offre {id} inconnue" + offre = EntrepriseOffre.query.filter_by( + id=offre_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"offre {offre_id} inconnue pour l'entreprise {entreprise_id}" ) offre_depts = EntrepriseOffreDepartement.query.filter_by(offre_id=offre.id).all() form = OffreModificationForm( @@ -766,7 +804,9 @@ def edit_offre(id): db.session.add(log) db.session.commit() flash("L'offre a été modifié.") - return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise.id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=offre.entreprise_id) + ) elif request.method == "GET": form.intitule.data = offre.intitule form.description.data = offre.description @@ -782,14 +822,19 @@ def edit_offre(id): ) -@bp.route("/fiche_entreprise/delete_offre/", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//delete_offre/", + methods=["GET", "POST"], +) @permission_required(Permission.RelationsEntreprisesChange) -def delete_offre(id): +def delete_offre(entreprise_id, offre_id): """ Permet de supprimer une offre """ - offre = EntrepriseOffre.query.filter_by(id=id).first_or_404( - description=f"offre {id} inconnue" + offre = EntrepriseOffre.query.filter_by( + id=offre_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"offre {offre_id} inconnue pour l'entreprise {entreprise_id}" ) entreprise_id = offre.entreprise.id form = SuppressionConfirmationForm() @@ -813,37 +858,46 @@ def delete_offre(id): 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 redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=offre.entreprise_id) + ) return render_template( - "entreprises/confirmation_form.html", + "entreprises/form_confirmation.html", title="Supression offre", form=form, info_message="Cliquez sur le bouton Supprimer pour confirmer votre supression", ) -@bp.route("/offres_recues/delete_offre_recue/", methods=["GET", "POST"]) +@bp.route( + "/offres_recues/delete_offre_recue/", methods=["GET", "POST"] +) @permission_required(Permission.RelationsEntreprisesView) -def delete_offre_recue(id): +def delete_offre_recue(envoi_offre_id): """ Permet de supprimer une offre reçue """ offre_recue = EntrepriseEnvoiOffre.query.filter_by( - id=id, receiver_id=current_user.id - ).first_or_404(description=f"offre recu {id} inconnue") + id=envoi_offre_id, receiver_id=current_user.id + ).first_or_404(description=f"offre recu {envoi_offre_id} inconnue") db.session.delete(offre_recue) db.session.commit() return redirect(url_for("entreprises.offres_recues")) -@bp.route("/fiche_entreprise/expired/", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//expired/", + methods=["GET", "POST"], +) @permission_required(Permission.RelationsEntreprisesChange) -def expired(id): +def expired(entreprise_id, offre_id): """ Permet de rendre expirée et non expirée une offre """ - offre = EntrepriseOffre.query.filter_by(id=id).first_or_404( - description=f"offre {id} inconnue" + offre = EntrepriseOffre.query.filter_by( + id=offre_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"offre {offre_id} inconnue pour l'entreprise {entreprise_id}" ) offre.expired = not offre.expired db.session.commit() @@ -851,20 +905,23 @@ def expired(id): flash("L'offre a été rendu expirée") else: flash("L'offre a été rendu non expirée") - return redirect(url_for("entreprises.fiche_entreprise", id=offre.entreprise_id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=offre.entreprise_id) + ) @bp.route( - "/fiche_entreprise//add_site", + "/fiche_entreprise//add_site", methods=["GET", "POST"], ) @permission_required(Permission.RelationsEntreprisesChange) -def add_site(id): - entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( - description=f"entreprise {id} inconnue" - ) - form = SiteCreationForm(hidden_entreprise_id=id) +def add_site(entreprise_id): + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=True + ).first_or_404(description=f"entreprise {entreprise_id} inconnue") + form = SiteCreationForm(hidden_entreprise_id=entreprise.id) if form.validate_on_submit(): + lien_entreprise = f"{entreprise.nom} - {form.nom.data.strip()}" site = EntrepriseSite( entreprise_id=entreprise.id, nom=form.nom.data.strip(), @@ -875,8 +932,20 @@ def add_site(id): ) db.session.add(site) db.session.commit() + db.session.refresh(site) + log = EntrepriseHistorique( + authenticated_user=current_user.user_name, + entreprise_id=entreprise.id, + object="site", + object_id=site.id, + text=f"{lien_entreprise} - Création d'un site", + ) + db.session.add(log) + db.session.commit() flash("Le site a été créé et ajouté à la fiche entreprise") - return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=entreprise.id) + ) return render_template( "entreprises/form.html", title="Ajout site", @@ -885,18 +954,17 @@ def add_site(id): @bp.route( - "/fiche_entreprise//edit_site/", + "/fiche_entreprise//edit_site/", methods=["GET", "POST"], ) -def edit_site(id_entreprise, id_site): - entreprise = Entreprise.query.filter_by( - id=id_entreprise, visible=True - ).first_or_404(description=f"entreprise {id_entreprise} inconnue") +def edit_site(entreprise_id, site_id): site = EntrepriseSite.query.filter_by( - id=id_site, entreprise_id=entreprise.id - ).first_or_404(description=f"site {id_site} inconnu") + id=site_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"site {site_id} inconnu pour l'entreprise {entreprise_id}" + ) form = SiteModificationForm( - hidden_entreprise_id=id_entreprise, hidden_site_id=id_site + hidden_entreprise_id=site.entreprise_id, hidden_site_id=site.id ) if form.validate_on_submit(): site.nom = form.nom.data.strip() @@ -905,7 +973,9 @@ def edit_site(id_entreprise, id_site): site.ville = form.ville.data.strip() site.pays = (form.pays.data.strip() if form.pays.data.strip() else "FRANCE",) db.session.commit() - return redirect(url_for("entreprises.fiche_entreprise", id=site.entreprise_id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=site.entreprise_id) + ) elif request.method == "GET": form.nom.data = site.nom form.adresse.data = site.adresse @@ -920,25 +990,24 @@ def edit_site(id_entreprise, id_site): @bp.route( - "/fiche_entreprise//add_correspondant/", + "/fiche_entreprise//site//add_correspondant", methods=["GET", "POST"], ) @permission_required(Permission.RelationsEntreprisesChange) -def add_correspondant(id_entreprise, id_site): +def add_correspondant(entreprise_id, site_id): """ Permet d'ajouter un correspondant a une entreprise """ - entreprise = Entreprise.query.filter_by( - id=id_entreprise, visible=True - ).first_or_404(description=f"entreprise {id_entreprise} inconnue") - site = EntrepriseSite.query.filter_by(id=id_site).first_or_404( - description=f"site {id_site} inconnue" + site = EntrepriseSite.query.filter_by( + id=site_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"site {site_id} inconnue pour l'entreprise {entreprise_id}" ) - form = CorrespondantsCreationForm(hidden_entreprise_id=entreprise.id) + print(site.entreprise_id) + form = CorrespondantsCreationForm(hidden_site_id=site.id) if form.validate_on_submit(): for correspondant_entry in form.correspondants.entries: correspondant = EntrepriseCorrespondant( - entreprise_id=entreprise.id, site_id=site.id, civilite=correspondant_entry.civilite.data, nom=correspondant_entry.nom.data.strip(), @@ -955,7 +1024,7 @@ def add_correspondant(id_entreprise, id_site): db.session.refresh(correspondant) log = EntrepriseHistorique( authenticated_user=current_user.user_name, - entreprise_id=correspondant.entreprise_id, + entreprise_id=correspondant.site.entreprise.id, object="correspondant", object_id=correspondant.id, text="Création d'un correspondant", @@ -963,28 +1032,44 @@ def add_correspondant(id_entreprise, id_site): db.session.add(log) db.session.commit() flash("Le correspondant a été ajouté à la fiche entreprise.") - return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) + return redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=site.entreprise_id) + ) return render_template( - "entreprises/ajout_correspondants.html", + "entreprises/form_ajout_correspondants.html", title="Ajout correspondant", form=form, ) -@bp.route("/fiche_entreprise/edit_correspondant/", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//site//edit_correspondant/", + methods=["GET", "POST"], +) @permission_required(Permission.RelationsEntreprisesChange) -def edit_correspondant(id): +def edit_correspondant(entreprise_id, site_id, correspondant_id): """ Permet de modifier un correspondant """ correspondant = ( db.session.query(EntrepriseCorrespondant) - .join(Entreprise, EntrepriseCorrespondant.entreprise_id == Entreprise.id) - .filter(EntrepriseCorrespondant.id == id, Entreprise.visible == True) - .first_or_404(description=f"correspondant {id} inconnu") + .join( + EntrepriseSite, + EntrepriseCorrespondant.site_id == EntrepriseSite.id, + ) + .join(Entreprise, EntrepriseSite.entreprise_id == Entreprise.id) + .filter( + EntrepriseCorrespondant.id == correspondant_id, + EntrepriseCorrespondant.site_id == site_id, + EntrepriseSite.entreprise_id == entreprise_id, + Entreprise.visible == True, + ) + .first_or_404( + description=f"correspondant {correspondant_id} inconnu pour l'entreprise {entreprise_id} et le site {site_id}" + ) ) form = CorrespondantModificationForm( - hidden_entreprise_id=correspondant.entreprise_id, + hidden_site_id=correspondant.site.id, hidden_correspondant_id=correspondant.id, ) if form.validate_on_submit(): @@ -999,7 +1084,7 @@ def edit_correspondant(id): correspondant.notes = form.notes.data.strip() log = EntrepriseHistorique( authenticated_user=current_user.user_name, - entreprise_id=correspondant.entreprise_id, + entreprise_id=correspondant.site.entreprise.id, object="correspondant", object_id=correspondant.id, text="Modification d'un correspondant", @@ -1008,7 +1093,10 @@ def edit_correspondant(id): db.session.commit() flash("Le correspondant a été modifié.") return redirect( - url_for("entreprises.fiche_entreprise", id=correspondant.entreprise_id) + url_for( + "entreprises.fiche_entreprise", + entreprise_id=correspondant.site.entreprise.id, + ) ) elif request.method == "GET": form.civilite.data = correspondant.civilite @@ -1027,24 +1115,38 @@ def edit_correspondant(id): ) -@bp.route("/fiche_entreprise/delete_correspondant/", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//site//delete_correspondant/", + methods=["GET", "POST"], +) @permission_required(Permission.RelationsEntreprisesChange) -def delete_correspondant(id): +def delete_correspondant(entreprise_id, site_id, correspondant_id): """ Permet de supprimer un correspondant """ correspondant = ( db.session.query(EntrepriseCorrespondant) - .join(Entreprise, EntrepriseCorrespondant.entreprise_id == Entreprise.id) - .filter(EntrepriseCorrespondant.id == id, Entreprise.visible == True) - .first_or_404(description=f"correspondant {id} inconnu") + .join( + EntrepriseSite, + EntrepriseCorrespondant.site_id == EntrepriseSite.id, + ) + .join(Entreprise, EntrepriseSite.entreprise_id == Entreprise.id) + .filter( + EntrepriseCorrespondant.id == correspondant_id, + EntrepriseCorrespondant.site_id == site_id, + EntrepriseSite.entreprise_id == entreprise_id, + Entreprise.visible == True, + ) + .first_or_404( + description=f"correspondant {correspondant_id} inconnu pour l'entreprise {entreprise_id} et le site {site_id}" + ) ) form = SuppressionConfirmationForm() if form.validate_on_submit(): db.session.delete(correspondant) log = EntrepriseHistorique( authenticated_user=current_user.user_name, - entreprise_id=correspondant.entreprise_id, + entreprise_id=correspondant.site.entreprise.id, object="correspondant", object_id=correspondant.id, text="Suppression d'un correspondant", @@ -1053,25 +1155,31 @@ def delete_correspondant(id): db.session.commit() flash("Le correspondant a été supprimé de la fiche entreprise.") return redirect( - url_for("entreprises.fiche_entreprise", id=correspondant.entreprise_id) + url_for( + "entreprises.fiche_entreprise", + entreprise_id=correspondant.site.entreprise.id, + ) ) return render_template( - "entreprises/confirmation_form.html", + "entreprises/form_confirmation.html", title="Supression correspondant", form=form, info_message="Cliquez sur le bouton Supprimer pour confirmer votre supression", ) -@bp.route("/fiche_entreprise//add_contact", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//contacts/add_contact", + methods=["GET", "POST"], +) @permission_required(Permission.RelationsEntreprisesChange) -def add_contact(id): +def add_contact(entreprise_id): """ Permet d'ajouter un contact avec une entreprise """ - entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( - description=f"entreprise {id} inconnue" - ) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=True + ).first_or_404(description=f"entreprise {entreprise_id} inconnue") form = ContactCreationForm( date=f"{datetime.now().strftime('%Y-%m-%dT%H:%M')}", utilisateur=f"{current_user.nom} {current_user.prenom} ({current_user.user_name})" @@ -1096,7 +1204,7 @@ def add_contact(id): ) db.session.add(contact) db.session.commit() - return redirect(url_for("entreprises.contacts", id=entreprise.id)) + return redirect(url_for("entreprises.contacts", entreprise_id=entreprise.id)) return render_template( "entreprises/form.html", title="Ajout contact", @@ -1104,14 +1212,19 @@ def add_contact(id): ) -@bp.route("/fiche_entreprise/edit_contact/", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//contacts/edit_contact/", + methods=["GET", "POST"], +) @permission_required(Permission.RelationsEntreprisesChange) -def edit_contact(id): +def edit_contact(entreprise_id, contact_id): """ Permet d'editer un contact avec une entreprise """ - contact = EntrepriseContact.query.filter_by(id=id).first_or_404( - description=f"contact {id} inconnu" + contact = EntrepriseContact.query.filter_by( + id=contact_id, entreprise=entreprise_id + ).first_or_404( + description=f"contact {contact_id} inconnu pour l'entreprise {entreprise_id}" ) form = ContactModificationForm() if form.validate_on_submit(): @@ -1128,7 +1241,9 @@ def edit_contact(id): contact.user = utilisateur.id contact.notes = form.notes.data db.session.commit() - return redirect(url_for("entreprises.contacts", id=contact.entreprise)) + return redirect( + url_for("entreprises.contacts", entreprise_id=contact.entreprise) + ) elif request.method == "GET": utilisateur = User.query.filter_by(id=contact.user).first() form.date.data = contact.date.strftime("%Y-%m-%dT%H:%M") @@ -1143,33 +1258,36 @@ def edit_contact(id): ) -@bp.route("/fiche_entreprise//contacts") +@bp.route("/fiche_entreprise//contacts") @permission_required(Permission.RelationsEntreprisesView) -def contacts(id): +def contacts(entreprise_id): """ Permet d'afficher une page avec la liste des contacts d'une entreprise """ - entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( - description=f"entreprise {id} inconnue" - ) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=True + ).first_or_404(description=f"entreprise {entreprise_id} inconnue") contacts = EntrepriseContact.query.filter_by(entreprise=entreprise.id).all() return render_template( "entreprises/contacts.html", title="Liste des contacts", contacts=contacts, - entreprise_id=id, + entreprise=entreprise, ) -@bp.route("/fiche_entreprise//add_stage_apprentissage", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//add_stage_apprentissage", + methods=["GET", "POST"], +) @permission_required(Permission.RelationsEntreprisesChange) -def add_stage_apprentissage(id): +def add_stage_apprentissage(entreprise_id): """ Permet d'ajouter un étudiant ayant réalisé un stage ou une alternance sur la fiche entreprise de l'entreprise """ - entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( - description=f"entreprise {id} inconnue" - ) + entreprise = Entreprise.query.filter_by( + id=entreprise_id, visible=True + ).first_or_404(description=f"entreprise {entreprise_id} inconnue") form = StageApprentissageCreationForm() if form.validate_on_submit(): etudiant_nomcomplet = form.etudiant.data.upper().strip() @@ -1199,27 +1317,32 @@ def add_stage_apprentissage(id): db.session.add(stage_apprentissage) db.session.commit() 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", entreprise_id=entreprise.id) + ) return render_template( - "entreprises/ajout_stage_apprentissage.html", + "entreprises/form_ajout_stage_apprentissage.html", title="Ajout stage / apprentissage", form=form, ) @bp.route( - "/fiche_entreprise/edit_stage_apprentissage/", methods=["GET", "POST"] + "/fiche_entreprise//edit_stage_apprentissage/", + methods=["GET", "POST"], ) @permission_required(Permission.RelationsEntreprisesChange) -def edit_stage_apprentissage(id): +def edit_stage_apprentissage(entreprise_id, stage_apprentissage_id): """ Permet de modifier un étudiant ayant réalisé un stage ou une alternance sur la fiche entreprise de l'entreprise """ stage_apprentissage = EntrepriseStageApprentissage.query.filter_by( - id=id - ).first_or_404(description=f"stage_apprentissage {id} inconnue") + id=stage_apprentissage_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"stage_apprentissage {stage_apprentissage_id} inconnue pour l'entreprise {entreprise_id}" + ) etudiant = Identite.query.filter_by(id=stage_apprentissage.etudid).first_or_404( - description=f"etudiant {id} inconnue" + description=f"etudiant {stage_apprentissage.etudid} inconnue" ) form = StageApprentissageModificationForm() if form.validate_on_submit(): @@ -1249,7 +1372,8 @@ def edit_stage_apprentissage(id): db.session.commit() return redirect( url_for( - "entreprises.fiche_entreprise", id=stage_apprentissage.entreprise_id + "entreprises.fiche_entreprise", + entreprise_id=stage_apprentissage.entreprise_id, ) ) elif request.method == "GET": @@ -1259,48 +1383,55 @@ def edit_stage_apprentissage(id): form.date_fin.data = stage_apprentissage.date_fin form.notes.data = stage_apprentissage.notes return render_template( - "entreprises/ajout_stage_apprentissage.html", + "entreprises/form_ajout_stage_apprentissage.html", title="Modification stage / apprentissage", form=form, ) @bp.route( - "/fiche_entreprise/delete_stage_apprentissage/", methods=["GET", "POST"] + "/fiche_entreprise//delete_stage_apprentissage/", + methods=["GET", "POST"], ) @permission_required(Permission.RelationsEntreprisesChange) -def delete_stage_apprentissage(id): +def delete_stage_apprentissage(entreprise_id, stage_apprentissage_id): """ Permet de supprimer un étudiant ayant réalisé un stage ou une alternance sur la fiche entreprise de l'entreprise """ stage_apprentissage = EntrepriseStageApprentissage.query.filter_by( - id=id - ).first_or_404(description=f"stage_apprentissage {id} inconnu") + id=stage_apprentissage_id, entreprise_id=entreprise_id + ).first_or_404(description=f"stage_apprentissage {stage_apprentissage_id} inconnu") form = SuppressionConfirmationForm() if form.validate_on_submit(): db.session.delete(stage_apprentissage) db.session.commit() return redirect( url_for( - "entreprises.fiche_entreprise", id=stage_apprentissage.entreprise_id + "entreprises.fiche_entreprise", + entreprise_id=stage_apprentissage.entreprise_id, ) ) return render_template( - "entreprises/confirmation_form.html", + "entreprises/form_confirmation.html", title="Supression stage/apprentissage", form=form, info_message="Cliquez sur le bouton Supprimer pour confirmer votre supression", ) -@bp.route("/fiche_entreprise/envoyer_offre/", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//envoyer_offre/", + methods=["GET", "POST"], +) @permission_required(Permission.RelationsEntreprisesSend) -def envoyer_offre(id): +def envoyer_offre(entreprise_id, offre_id): """ Permet d'envoyer une offre à un utilisateur ScoDoc """ - offre = EntrepriseOffre.query.filter_by(id=id).first_or_404( - description=f"offre {id} inconnue" + offre = EntrepriseOffre.query.filter_by( + id=offre_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"offre {offre_id} inconnue pour l'entreprise {entreprise_id}" ) form = EnvoiOffreForm() if form.validate_on_submit(): @@ -1323,9 +1454,11 @@ def envoyer_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 redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=offre.entreprise_id) + ) return render_template( - "entreprises/envoi_offre_form.html", + "entreprises/form_envoi_offre.html", title="Envoyer une offre", form=form, ) @@ -1443,9 +1576,18 @@ def import_donnees(): ): return redirect(url_for("entreprises.import_donnees")) for entreprise in entreprises_import: - db.session.add(entreprise) - db.session.commit() - db.session.refresh(entreprise) + try: + db.session.add(entreprise) + db.session.commit() + db.session.refresh(entreprise) + except: + db.session.rollback() + flash("Une erreur est survenue veuillez réessayer.") + return render_template( + "entreprises/import_donnees.html", + title="Importation données", + form=form, + ) site = EntrepriseSite( entreprise_id=entreprise.id, nom=entreprise.nom, @@ -1484,7 +1626,7 @@ def import_donnees(): @bp.route( - "/get_offre_file////" + "/fiche_entreprise//offre//get_offre_file//" ) @permission_required(Permission.RelationsEntreprisesView) def get_offre_file(entreprise_id, offre_id, filedir, filename): @@ -1516,14 +1658,19 @@ def get_offre_file(entreprise_id, offre_id, filedir, filename): abort(404, description=f"fichier {filename} inconnu") -@bp.route("/fiche_entreprise/add_offre_file/", methods=["GET", "POST"]) +@bp.route( + "/fiche_entreprise//offre//add_offre_file", + methods=["GET", "POST"], +) @permission_required(Permission.RelationsEntreprisesChange) -def add_offre_file(offre_id): +def add_offre_file(entreprise_id, offre_id): """ Permet d'ajouter un fichier à une offre """ - offre = EntrepriseOffre.query.filter_by(id=offre_id).first_or_404( - description=f"offre {offre_id} inconnue" + offre = EntrepriseOffre.query.filter_by( + id=offre_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"offre {offre_id} inconnue pour l'entreprise {entreprise_id}" ) form = AjoutFichierForm() if form.validate_on_submit(): @@ -1540,7 +1687,9 @@ def add_offre_file(offre_id): 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 redirect( + url_for("entreprises.fiche_entreprise", entreprise_id=offre.entreprise_id) + ) return render_template( "entreprises/form.html", title="Ajout fichier à une offre", @@ -1549,16 +1698,18 @@ def add_offre_file(offre_id): @bp.route( - "/fiche_entreprise/delete_offre_file//", + "/fiche_entreprise//offre//delete_offre_file/", methods=["GET", "POST"], ) @permission_required(Permission.RelationsEntreprisesChange) -def delete_offre_file(offre_id, filedir): +def delete_offre_file(entreprise_id, offre_id, filedir): """ Permet de supprimer un fichier d'une offre """ - offre = EntrepriseOffre.query.filter_by(id=offre_id).first_or_404( - description=f"offre {offre_id} inconnue" + offre = EntrepriseOffre.query.filter_by( + id=offre_id, entreprise_id=entreprise_id + ).first_or_404( + description=f"offre {offre_id} inconnue pour l'entreprise {entreprise_id}" ) form = SuppressionConfirmationForm() if form.validate_on_submit(): @@ -1573,10 +1724,12 @@ def delete_offre_file(offre_id, filedir): shutil.rmtree(path) flash("Le fichier relié à l'offre a été supprimé.") return redirect( - url_for("entreprises.fiche_entreprise", id=offre.entreprise_id) + url_for( + "entreprises.fiche_entreprise", entreprise_id=offre.entreprise_id + ) ) return render_template( - "entreprises/confirmation_form.html", + "entreprises/form_confirmation.html", title="Suppression fichier d'une offre", form=form, info_message="Cliquez sur le bouton Supprimer pour confirmer votre supression", @@ -1602,3 +1755,8 @@ def preferences(): title="Préférences", form=form, ) + + +@bp.errorhandler(404) +def not_found_error_handler(e): + return render_template("entreprises/error.html", title="Erreur", e=e) diff --git a/app/static/css/entreprises.css b/app/static/css/entreprises.css index ce3a4ccd..947f90fb 100644 --- a/app/static/css/entreprises.css +++ b/app/static/css/entreprises.css @@ -1,37 +1,85 @@ -.nav-entreprise { +/* nav */ + +.nav_entreprise { text-align: left; } -.nav-entreprise ul { +.nav_entreprise > ul { padding: 0; } -.nav-entreprise li{ +.nav_entreprise_item { list-style: none; display: inline-block; padding: 10px; + margin: 2px; border: 2px black solid; border-radius: 10px; } -.nav-entreprise li:hover{ +.nav_entreprise_item:hover { background-color: rgb(212, 212, 212); } -.nav-entreprise>ul>li>a { +.nav_entreprise_link { text-decoration: none; color: black; padding: 15px; } -.nav-entreprise>ul>li>a:hover { +.nav_entreprise_link:hover { text-decoration: underline; + color: black; +} + +.nav_entreprise_link-active { + font-weight: bold; +} + +/* breadcrumbs */ + +.breadcrumbs { + padding: 0; +} + +.breadcrumbs_item { + display: inline-block; +} + +.breadcrumbs_item:not(:last-of-type)::after { + content: '\203a'; + margin: 0 5px; + color: #777; +} + +.breadcrumbs_link { + text-decoration: none; + color: #777; +} + +.breadcrumbs_link:hover { + text-decoration: underline; + color: #333; +} + +.breadcrumbs_link-active { + color: #333; + font-weight: bold; +} + +/* form error */ + +.title-form-error { + font-weight: bold; + color: #a94442; } .form-error { color: #a94442; } +/* entreprises */ + .boutons .btn { margin-top: 5px; margin-bottom: 5px; @@ -90,7 +138,7 @@ flex: 1 0 0; } -.taxe-apprentissage{ +.taxe-apprentissage { overflow-y: scroll; height: 100px; } @@ -138,38 +186,4 @@ #form-entreprise-filter > label { margin-right: 20px; -} - -.title-form-error { - font-weight: bold; - color: #a94442; -} - -.breadcrumbs { - padding: 0; -} - -.breadcrumbs_item { - display: inline-block; -} - -.breadcrumbs_item:not(:last-of-type)::after { - content: '\203a'; - margin: 0 5px; - color: #777; -} - -.breadcrumbs_link { - text-decoration: none; - color: #777; -} - -.breadcrumbs_link:hover { - text-decoration: underline; - color: #333; -} - -.breadcrumbs_link-active { - color: #333; - font-weight: bold; } \ No newline at end of file diff --git a/app/templates/entreprises/_correspondant.html b/app/templates/entreprises/_correspondant.html index d8c00846..7d1539b7 100644 --- a/app/templates/entreprises/_correspondant.html +++ b/app/templates/entreprises/_correspondant.html @@ -26,8 +26,8 @@ {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %} {% endif %} \ No newline at end of file diff --git a/app/templates/entreprises/_offre.html b/app/templates/entreprises/_offre.html index c799b6f4..4ae08232 100644 --- a/app/templates/entreprises/_offre.html +++ b/app/templates/entreprises/_offre.html @@ -23,31 +23,31 @@ {% for fichier in offre[1] %} {{ fichier[1] }} {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %} - supprimer + supprimer {% endif %}
{% endfor %} {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %} - Ajoutez un fichier + Ajoutez un fichier {% endif %}
{% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %} - Modifier l'offre - Supprimer l'offre + Modifier l'offre + Supprimer l'offre {% endif %} {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesSend, None) %} - Envoyer l'offre + Envoyer l'offre {% endif %} {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %} {% if not offre[0].expired %} - Rendre expirée + Rendre expirée {% else %} - Rendre non expirée + Rendre non expirée {% endif %} {% endif %}
diff --git a/app/templates/entreprises/contacts.html b/app/templates/entreprises/contacts.html index 70ad459e..e79f214b 100644 --- a/app/templates/entreprises/contacts.html +++ b/app/templates/entreprises/contacts.html @@ -15,7 +15,7 @@ Entreprises