Added alembic migration
This commit is contained in:
parent
1d15af53b7
commit
bc82bab1eb
|
@ -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,
|
||||
)
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue