lié migrations appli relations entreprises

This commit is contained in:
Arthur ZHU 2022-07-04 21:12:56 +02:00
parent 9d26c3126a
commit 9f7d6a3020
5 changed files with 95 additions and 108 deletions

View File

@ -40,7 +40,7 @@ from app.entreprises.models import (
EntrepriseOffreDepartement,
EntreprisePreferences,
EntrepriseSite,
EntrepriseLog,
EntrepriseHistorique,
)
from app import email, db
from app.scodoc import sco_preferences
@ -324,7 +324,7 @@ def check_entreprises_import(m):
return False
if len(entreprises_import) > 0:
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
text=f"Importation de {len(entreprises_import)} entreprise(s)",
)
@ -432,14 +432,14 @@ def check_sites_import(m):
return False, False
if len(sites_import) > 0:
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
text=f"Importation de {len(sites_import)} site(s)",
)
db.session.add(log)
if len(correspondants_import) > 0:
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
text=f"Importation de {len(correspondants_import)} correspondant(s)",
)

View File

@ -149,8 +149,8 @@ class EntrepriseOffre(db.Model):
}
class EntrepriseLog(db.Model):
__tablename__ = "are_logs"
class EntrepriseHistorique(db.Model):
__tablename__ = "are_historique"
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
authenticated_user = db.Column(db.Text)

View File

@ -41,7 +41,7 @@ from app.entreprises.models import (
Entreprise,
EntrepriseOffre,
EntrepriseCorrespondant,
EntrepriseLog,
EntrepriseHistorique,
EntrepriseContact,
EntrepriseSite,
EntrepriseStageApprentissage,
@ -69,7 +69,11 @@ def index():
Permet d'afficher une page avec la liste des entreprises (visible) et une liste des dernières opérations
"""
entreprises = Entreprise.query.filter_by(visible=True, active=True)
logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all()
logs = (
EntrepriseHistorique.query.order_by(EntrepriseHistorique.date.desc())
.limit(LOGS_LEN)
.all()
)
if current_user.has_permission(Permission.RelationsEntreprisesChange, None):
form = EntreprisesFilterForm()
checked = [False, False]
@ -103,9 +107,9 @@ def logs():
Permet d'afficher les logs (toutes les entreprises)
"""
page = request.args.get("page", 1, type=int)
logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).paginate(
page=page, per_page=20
)
logs = EntrepriseHistorique.query.order_by(
EntrepriseHistorique.date.desc()
).paginate(page=page, per_page=20)
return render_template(
"entreprises/logs.html",
title="Logs",
@ -140,7 +144,11 @@ def correspondants():
.filter_by(visible=True, active=True)
.all()
)
logs = EntrepriseLog.query.order_by(EntrepriseLog.date.desc()).limit(LOGS_LEN).all()
logs = (
EntrepriseHistorique.query.order_by(EntrepriseHistorique.date.desc())
.limit(LOGS_LEN)
.all()
)
return render_template(
"entreprises/correspondants.html",
title="Correspondants",
@ -176,8 +184,8 @@ def fiche_entreprise(id):
offres_with_files.append(offre_with_files)
sites = entreprise.sites[:]
logs = (
EntrepriseLog.query.order_by(EntrepriseLog.date.desc())
.filter(EntrepriseLog.entreprise_id == id)
EntrepriseHistorique.query.order_by(EntrepriseHistorique.date.desc())
.filter(EntrepriseHistorique.entreprise_id == id)
.limit(LOGS_LEN)
.all()
)
@ -216,8 +224,8 @@ def logs_entreprise(id):
description=f"logs fiche entreprise {id} inconnu"
)
logs = (
EntrepriseLog.query.order_by(EntrepriseLog.date.desc())
.filter(EntrepriseLog.entreprise_id == entreprise.id)
EntrepriseHistorique.query.order_by(EntrepriseHistorique.date.desc())
.filter(EntrepriseHistorique.entreprise_id == entreprise.id)
.paginate(page=page, per_page=20)
)
return render_template(
@ -360,7 +368,7 @@ def add_entreprise():
if current_user.has_permission(Permission.RelationsEntreprisesValidate, None):
entreprise.visible = True
nom_entreprise = f"<a href=/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}>{entreprise.nom}</a>"
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
text=f"{nom_entreprise} - Création de la fiche entreprise ({entreprise.nom})",
entreprise_id=entreprise.id,
@ -398,7 +406,7 @@ def edit_entreprise(id):
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(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=entreprise.id,
text=f"{nom_entreprise} - Modification du nom (ancien nom: {entreprise.nom})",
@ -406,7 +414,7 @@ def edit_entreprise(id):
entreprise.nom = form.nom.data.strip()
db.session.add(log)
if entreprise.adresse != form.adresse.data.strip():
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=entreprise.id,
text=f"{nom_entreprise} - Modification de l'adresse (ancienne adresse: {entreprise.adresse})",
@ -414,7 +422,7 @@ def edit_entreprise(id):
entreprise.adresse = form.adresse.data.strip()
db.session.add(log)
if entreprise.codepostal != form.codepostal.data.strip():
log = EntrepriseLog(
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})",
@ -422,7 +430,7 @@ def edit_entreprise(id):
entreprise.codepostal = form.codepostal.data.strip()
db.session.add(log)
if entreprise.ville != form.ville.data.strip():
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=entreprise.id,
text=f"{nom_entreprise} - Modification de la ville (ancienne ville: {entreprise.ville})",
@ -430,7 +438,7 @@ def edit_entreprise(id):
entreprise.ville = form.ville.data.strip()
db.session.add(log)
if entreprise.pays != form.pays.data.strip() or not form.pays.data.strip():
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=entreprise.id,
text=f"{nom_entreprise} - Modification du pays (ancien pays: {entreprise.pays})",
@ -604,7 +612,7 @@ def validate_entreprise(id):
if form.validate_on_submit():
entreprise.visible = True
nom_entreprise = f"<a href=/ScoDoc/entreprises/fiche_entreprise/{entreprise.id}>{entreprise.nom}</a>"
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=entreprise.id,
text=f"{nom_entreprise} - Validation de la fiche entreprise ({entreprise.nom})",
@ -691,7 +699,7 @@ def add_offre(id):
file = form.fichier.data
filename = secure_filename(file.filename)
file.save(os.path.join(path, filename))
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=entreprise.id,
object="offre",
@ -748,7 +756,7 @@ def edit_offre(id):
offre_id=offre.id, dept_id=dept
).first_or_404()
db.session.delete(offre_dept)
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=offre.entreprise_id,
object="offre",
@ -795,7 +803,7 @@ def delete_offre(id):
)
if os.path.isdir(path):
shutil.rmtree(path)
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=offre.entreprise_id,
object="offre",
@ -945,7 +953,7 @@ def add_correspondant(id_entreprise, id_site):
db.session.add(correspondant)
db.session.commit()
db.session.refresh(correspondant)
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=correspondant.entreprise_id,
object="correspondant",
@ -989,7 +997,7 @@ def edit_correspondant(id):
correspondant.service = form.service.data.strip()
correspondant.origine = form.origine.data.strip()
correspondant.notes = form.notes.data.strip()
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=correspondant.entreprise_id,
object="correspondant",
@ -1034,7 +1042,7 @@ def delete_correspondant(id):
form = SuppressionConfirmationForm()
if form.validate_on_submit():
db.session.delete(correspondant)
log = EntrepriseLog(
log = EntrepriseHistorique(
authenticated_user=current_user.user_name,
entreprise_id=correspondant.entreprise_id,
object="correspondant",

View File

@ -0,0 +1,58 @@
"""ajout taxe apprentissage, association, changement historique
Revision ID: 0b337376e9f7
Revises: ee21c76c8183
Create Date: 2022-07-04 21:10:53.946385
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '0b337376e9f7'
down_revision = 'ee21c76c8183'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('are_historique',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('date', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.Column('authenticated_user', sa.Text(), nullable=True),
sa.Column('entreprise_id', sa.Integer(), nullable=True),
sa.Column('object', sa.Text(), nullable=True),
sa.Column('object_id', sa.Integer(), nullable=True),
sa.Column('text', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('are_taxe_apprentissage',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('entreprise_id', sa.Integer(), nullable=True),
sa.Column('annee', sa.Integer(), nullable=True),
sa.Column('montant', sa.Integer(), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.ForeignKeyConstraint(['entreprise_id'], ['are_entreprises.id'], ondelete='cascade'),
sa.PrimaryKeyConstraint('id')
)
op.drop_table('are_logs')
op.add_column('are_entreprises', sa.Column('association', sa.Boolean(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('are_entreprises', 'association')
op.create_table('are_logs',
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('date', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=True),
sa.Column('authenticated_user', sa.TEXT(), autoincrement=False, nullable=True),
sa.Column('object', sa.INTEGER(), autoincrement=False, nullable=True),
sa.Column('text', sa.TEXT(), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('id', name='are_logs_pkey')
)
op.drop_table('are_taxe_apprentissage')
op.drop_table('are_historique')
# ### end Alembic commands ###

View File

@ -1,79 +0,0 @@
"""ajout taxe apprentissage, association, changement historique
Revision ID: 2b872cb116ca
Revises: d5b3bdd1d622
Create Date: 2022-05-06 18:16:58.727052
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "2b872cb116ca"
down_revision = "d5b3bdd1d622"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"are_taxe_apprentissage",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("entreprise_id", sa.Integer(), nullable=True),
sa.Column("annee", sa.Integer(), nullable=True),
sa.Column("montant", sa.Integer(), nullable=True),
sa.Column("notes", sa.Text(), nullable=True),
sa.ForeignKeyConstraint(
["entreprise_id"], ["are_entreprises.id"], ondelete="cascade"
),
sa.PrimaryKeyConstraint("id"),
)
op.add_column(
"are_entreprises", sa.Column("association", sa.Boolean(), nullable=True)
)
op.add_column("are_logs", sa.Column("entreprise_id", sa.Integer(), nullable=True))
op.add_column("are_logs", sa.Column("object_id", sa.Integer(), nullable=True))
op.alter_column(
"are_logs",
"object",
existing_type=sa.INTEGER(),
type_=sa.Text(),
existing_nullable=True,
)
op.create_index(
op.f("ix_scolar_news_authenticated_user"),
"scolar_news",
["authenticated_user"],
unique=False,
)
op.create_index(op.f("ix_scolar_news_date"), "scolar_news", ["date"], unique=False)
op.create_index(
op.f("ix_scolar_news_object"), "scolar_news", ["object"], unique=False
)
op.create_index(op.f("ix_scolar_news_type"), "scolar_news", ["type"], unique=False)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_scolar_news_type"), table_name="scolar_news")
op.drop_index(op.f("ix_scolar_news_object"), table_name="scolar_news")
op.drop_index(op.f("ix_scolar_news_date"), table_name="scolar_news")
op.drop_index(op.f("ix_scolar_news_authenticated_user"), table_name="scolar_news")
op.execute(
"alter table are_logs alter column object set data type int using object::integer"
)
op.alter_column(
"are_logs",
"object",
existing_type=sa.Text(),
type_=sa.INTEGER(),
existing_nullable=True,
)
op.drop_column("are_logs", "object_id")
op.drop_column("are_logs", "entreprise_id")
op.drop_column("are_entreprises", "association")
op.drop_table("are_taxe_apprentissage")
# ### end Alembic commands ###