Rename resource_name to resource_type
This commit is contained in:
parent
22f02f604f
commit
870929afd6
@ -23,7 +23,7 @@ def upgrade():
|
|||||||
sa.Column('time_updated', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
|
sa.Column('time_updated', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False),
|
||||||
sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False),
|
sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False),
|
||||||
sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True),
|
sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True),
|
||||||
sa.Column('resource_name', sa.String(), nullable=False),
|
sa.Column('resource_type', sa.String(), nullable=False),
|
||||||
sa.Column('resource_id', postgresql.UUID(as_uuid=True), nullable=False),
|
sa.Column('resource_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||||
sa.Column('action', sa.String(), nullable=False),
|
sa.Column('action', sa.String(), nullable=False),
|
||||||
sa.Column('workspace_id', postgresql.UUID(as_uuid=True), nullable=True),
|
sa.Column('workspace_id', postgresql.UUID(as_uuid=True), nullable=True),
|
||||||
|
@ -35,19 +35,19 @@ class AuditLog(object):
|
|||||||
return AuditEventQuery.get_all()
|
return AuditEventQuery.get_all()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _resource_name(cls, resource):
|
def _resource_type(cls, resource):
|
||||||
return type(resource).__name__.lower()
|
return type(resource).__name__.lower()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _log(cls, user=None, workspace_id=None, resource=None, action=None):
|
def _log(cls, user=None, workspace_id=None, resource=None, action=None):
|
||||||
resource_id = resource.id if resource else None
|
resource_id = resource.id if resource else None
|
||||||
resource_name = cls._resource_name(resource) if resource else None
|
resource_type = cls._resource_type(resource) if resource else None
|
||||||
|
|
||||||
audit_event = AuditEventQuery.create(
|
audit_event = AuditEventQuery.create(
|
||||||
user=user,
|
user=user,
|
||||||
workspace_id=workspace_id,
|
workspace_id=workspace_id,
|
||||||
resource_id=resource_id,
|
resource_id=resource_id,
|
||||||
resource_name=resource_name,
|
resource_type=resource_type,
|
||||||
action=action,
|
action=action,
|
||||||
)
|
)
|
||||||
return AuditEventQuery.add_and_commit(audit_event)
|
return AuditEventQuery.add_and_commit(audit_event)
|
||||||
|
@ -17,13 +17,13 @@ class AuditEvent(Base, TimestampsMixin):
|
|||||||
workspace_id = Column(UUID(as_uuid=True), ForeignKey("workspaces.id"), index=True)
|
workspace_id = Column(UUID(as_uuid=True), ForeignKey("workspaces.id"), index=True)
|
||||||
workspace = relationship("Workspace", backref="audit_events")
|
workspace = relationship("Workspace", backref="audit_events")
|
||||||
|
|
||||||
resource_name = Column(String(), nullable=False)
|
resource_type = Column(String(), nullable=False)
|
||||||
resource_id = Column(UUID(as_uuid=True), index=True, nullable=False)
|
resource_id = Column(UUID(as_uuid=True), index=True, nullable=False)
|
||||||
action = Column(String(), nullable=False)
|
action = Column(String(), nullable=False)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
full_action = "{} on {} {}".format(
|
full_action = "{} on {} {}".format(
|
||||||
self.action, self.resource_name, self.resource_id
|
self.action, self.resource_type, self.resource_id
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.user and self.workspace:
|
if self.user and self.workspace:
|
||||||
|
@ -26,12 +26,12 @@ class AuditableMixin(object):
|
|||||||
def create_audit_event(connection, resource, action):
|
def create_audit_event(connection, resource, action):
|
||||||
user_id = getattr_path(g, "current_user.id")
|
user_id = getattr_path(g, "current_user.id")
|
||||||
workspace_id = resource.auditable_workspace_id()
|
workspace_id = resource.auditable_workspace_id()
|
||||||
resource_name = resource.auditable_resource_name()
|
resource_type = resource.auditable_resource_type()
|
||||||
|
|
||||||
audit_event = AuditEvent(
|
audit_event = AuditEvent(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
workspace_id=workspace_id,
|
workspace_id=workspace_id,
|
||||||
resource_name=resource_name,
|
resource_type=resource_type,
|
||||||
resource_id=resource.id,
|
resource_id=resource.id,
|
||||||
action=action,
|
action=action,
|
||||||
)
|
)
|
||||||
@ -58,7 +58,7 @@ class AuditableMixin(object):
|
|||||||
def audit_update(mapper, connection, target):
|
def audit_update(mapper, connection, target):
|
||||||
target.create_audit_event(connection, target, ACTION_UPDATE)
|
target.create_audit_event(connection, target, ACTION_UPDATE)
|
||||||
|
|
||||||
def auditable_resource_name(self):
|
def auditable_resource_type(self):
|
||||||
return camel_to_snake(type(self).__name__)
|
return camel_to_snake(type(self).__name__)
|
||||||
|
|
||||||
def auditable_workspace_id(self):
|
def auditable_workspace_id(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user