From bc82bab1ebb940d52d8962bcbf16e8e30d843e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Thu, 10 Mar 2022 16:37:21 +0100 Subject: [PATCH] Added alembic migration --- app/admin_model.py | 15 +++++--- ...015_b500363567e3_create_admin_audit_log.py | 35 ++++++++----------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/app/admin_model.py b/app/admin_model.py index 0e8d51cf..9994acdf 100644 --- a/app/admin_model.py +++ b/app/admin_model.py @@ -36,10 +36,6 @@ class SLModelView(sqla.ModelView): return redirect(url_for("auth.login", next=request.url)) def on_model_change(self, form, model, is_created): - if is_created: - action = AdminAuditLog.ACTION_CREATE_OBJECT - else: - action = AdminAuditLog.ACTION_UPDATE_OBJECT changes = {} for attr in sqlalchemy.inspect(model).attrs: if attr.history.has_changes() and attr.key not in ( @@ -47,14 +43,23 @@ class SLModelView(sqla.ModelView): "updated_at", ): value = attr.value + # If it's a model reference, get the source id if issubclass(type(value), models.Base): value = value.id + # otherwise, if its a generic object stringify it + if issubclass(type(value), object): + value = str(value) changes[attr.key] = value + auditAction = ( + AdminAuditLog.ACTION_CREATE_OBJECT + if is_created + else AdminAuditLog.ACTION_UPDATE_OBJECT + ) AdminAuditLog.create( admin_user_id=current_user.id, model=model.__class__.__name__, model_id=model.id, - action=action, + action=auditAction, data=changes, ) diff --git a/migrations/versions/2022_031015_b500363567e3_create_admin_audit_log.py b/migrations/versions/2022_031015_b500363567e3_create_admin_audit_log.py index 32a66eff..d160adc7 100644 --- a/migrations/versions/2022_031015_b500363567e3_create_admin_audit_log.py +++ b/migrations/versions/2022_031015_b500363567e3_create_admin_audit_log.py @@ -11,30 +11,25 @@ import sqlalchemy as sa from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. -revision = 'b500363567e3' -down_revision = '9282e982bc05' +revision = "b500363567e3" +down_revision = "9282e982bc05" branch_labels = None depends_on = None def upgrade(): - op.create - # ### commands auto generated by Alembic - please adjust! ### - op.alter_column('admin_audit_log', 'data', - existing_type=postgresql.JSONB(astext_type=sa.Text()), - nullable=False) - op.alter_column('admin_audit_log', 'model_id', - existing_type=sa.INTEGER(), - nullable=False) - # ### end Alembic commands ### - + op.create_table( + "admin_audit_log", + sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), + sa.Column("created_at", sqlalchemy_utils.types.arrow.ArrowType(), nullable=False), + sa.Column("admin_user_id", sa.Integer, nullable=False), + sa.Column("action", sa.Integer, nullable=False), + sa.Column("model", sa.String(length=256), nullable=False), + sa.Column("model_id", sa.Integer, nullable=False), + sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=False), + sa.ForeignKeyConstraint(['admin_user_id'], ['users.id'], ondelete='cascade'), + sa.PrimaryKeyConstraint("id"), + ) def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.alter_column('admin_audit_log', 'model_id', - existing_type=sa.INTEGER(), - nullable=True) - op.alter_column('admin_audit_log', 'data', - existing_type=postgresql.JSONB(astext_type=sa.Text()), - nullable=True) - # ### end Alembic commands ### + op.drop_table("admin_audit_log")