From 9f7d6a3020ebf96913a4acc18d073fc08721faf5 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Mon, 4 Jul 2022 21:12:56 +0200 Subject: [PATCH] =?UTF-8?q?li=C3=A9=20migrations=20appli=20relations=20ent?= =?UTF-8?q?reprises?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/entreprises/app_relations_entreprises.py | 8 +- app/entreprises/models.py | 4 +- app/entreprises/routes.py | 54 +++++++------ ...7_ajout_taxe_apprentissage_association_.py | 58 ++++++++++++++ ...a_ajout_taxe_apprentissage_association_.py | 79 ------------------- 5 files changed, 95 insertions(+), 108 deletions(-) create mode 100644 migrations/versions/0b337376e9f7_ajout_taxe_apprentissage_association_.py delete mode 100644 migrations/versions/2b872cb116ca_ajout_taxe_apprentissage_association_.py diff --git a/app/entreprises/app_relations_entreprises.py b/app/entreprises/app_relations_entreprises.py index 64208cb3..b5b14dab 100644 --- a/app/entreprises/app_relations_entreprises.py +++ b/app/entreprises/app_relations_entreprises.py @@ -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)", ) diff --git a/app/entreprises/models.py b/app/entreprises/models.py index bb4ad616..4edbe458 100644 --- a/app/entreprises/models.py +++ b/app/entreprises/models.py @@ -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) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index a116c768..ef15f32b 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -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"{entreprise.nom}" - 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"{form.nom.data.strip()}" 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"{entreprise.nom}" - 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", diff --git a/migrations/versions/0b337376e9f7_ajout_taxe_apprentissage_association_.py b/migrations/versions/0b337376e9f7_ajout_taxe_apprentissage_association_.py new file mode 100644 index 00000000..c8635598 --- /dev/null +++ b/migrations/versions/0b337376e9f7_ajout_taxe_apprentissage_association_.py @@ -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 ### diff --git a/migrations/versions/2b872cb116ca_ajout_taxe_apprentissage_association_.py b/migrations/versions/2b872cb116ca_ajout_taxe_apprentissage_association_.py deleted file mode 100644 index 8cab659d..00000000 --- a/migrations/versions/2b872cb116ca_ajout_taxe_apprentissage_association_.py +++ /dev/null @@ -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 ###