Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit for audit logs #160

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/source/audit_logs/commands.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Commands
========

If you've registered the ``audit_logs`` app in your ``piccolo_conf.py`` file
(see the :ref:`migrations docs <AuditLogMigrations>`), it gives you access to a
custom command.

clean
-----

If you run the following on the command line, it will delete all logs
from the database.

.. code-block:: bash

piccolo audit_logs clean
63 changes: 63 additions & 0 deletions docs/source/audit_logs/tables.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Tables
======

``audit_logs`` is a ``Piccolo`` app that records changes made by users to database tables.
We store the audit logs in :class:`AuditLog <piccolo_api.audit_logs.tables.AuditLog>`.

-------------------------------------------------------------------------------

.. _AuditLogMigrations:

Migrations
----------

We recommend creating the ``audit_logs`` tables using migrations.

You can add ``piccolo_api.audit_logs.piccolo_app`` to the ``apps`` arguments
of the :class:`AppRegistry <piccolo.conf.apps.AppRegistry>` in ``piccolo_conf.py``.

.. code-block:: bash

APP_REGISTRY = AppRegistry(
apps=[
...
"piccolo_api.audit_logs.piccolo_app",
...
]
)

To learn more about Piccolo apps, see the `Piccolo docs <https://piccolo-orm.readthedocs.io/en/latest/piccolo/projects_and_apps/index.html>`_.

To run the migrations and create the table, run:

.. code-block:: bash

piccolo migrations forwards audit_logs

-------------------------------------------------------------------------------

Creating them manually
----------------------

If you prefer not to use migrations, and want to create them manually, you can
do this instead:

.. code-block:: python

from piccolo_api.audit_logs.tables import AuditLog
from piccolo.tables import create_db_tables_sync

create_db_tables_sync(AuditLog, if_not_exists=True)

-------------------------------------------------------------------------------

Source
------

AuditLog
~~~~~~~~

.. currentmodule:: piccolo_api.audit_logs.tables

.. autoclass:: AuditLog
:members:
7 changes: 7 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ ASGI app, covering authentication, security, and more.
./change_password/index
./advanced_auth/index

.. toctree::
:caption: Audit logs
:maxdepth: 1

./audit_logs/tables.rst
./audit_logs/commands.rst

.. toctree::
:caption: Piccolo Admin
:maxdepth: 1
Expand Down
Empty file.
10 changes: 10 additions & 0 deletions piccolo_api/audit_logs/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .tables import AuditLog


async def clean():
"""
Removes all audit logs.
"""
print("Removing audit logs ...")
await AuditLog.delete(force=True).run()
print("Successfully removed audit logs")
24 changes: 24 additions & 0 deletions piccolo_api/audit_logs/piccolo_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Import all of the Tables subclasses in your app here, and register them with
the APP_CONFIG.
"""

import os

from piccolo.conf.apps import AppConfig

from .commands import clean
from .tables import AuditLog

CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))


APP_CONFIG = AppConfig(
app_name="audit_logs",
migrations_folder_path=os.path.join(
CURRENT_DIRECTORY, "piccolo_migrations"
),
table_classes=[AuditLog],
migration_dependencies=[],
commands=[clean],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
from enum import Enum

from piccolo.apps.migrations.auto.migration_manager import MigrationManager
from piccolo.columns.column_types import JSON, Text, Timestamp, Varchar
from piccolo.columns.defaults.timestamp import TimestampNow
from piccolo.columns.indexes import IndexMethod

ID = "2022-06-28T18:57:05:840638"
VERSION = "0.80.0"
DESCRIPTION = ""


async def forwards():
manager = MigrationManager(
migration_id=ID, app_name="audit_logs", description=DESCRIPTION
)

manager.add_table("AuditLog", tablename="audit_log")

manager.add_column(
table_class_name="AuditLog",
tablename="audit_log",
column_name="action_time",
db_column_name="action_time",
column_class_name="Timestamp",
column_class=Timestamp,
params={
"default": TimestampNow(),
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)

manager.add_column(
table_class_name="AuditLog",
tablename="audit_log",
column_name="action_type",
db_column_name="action_type",
column_class_name="Varchar",
column_class=Varchar,
params={
"length": 255,
"default": "",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": Enum(
"ActionType",
{
"creating": "creating",
"updating": "updating",
"deleting": "deleting",
},
),
"db_column_name": None,
"secret": False,
},
)

manager.add_column(
table_class_name="AuditLog",
tablename="audit_log",
column_name="action_user",
db_column_name="action_user",
column_class_name="Varchar",
column_class=Varchar,
params={
"length": 255,
"default": "",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)

manager.add_column(
table_class_name="AuditLog",
tablename="audit_log",
column_name="table_name",
db_column_name="table_name",
column_class_name="Varchar",
column_class=Varchar,
params={
"length": 255,
"default": "",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)

manager.add_column(
table_class_name="AuditLog",
tablename="audit_log",
column_name="change_message",
db_column_name="change_message",
column_class_name="Text",
column_class=Text,
params={
"default": "",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)

manager.add_column(
table_class_name="AuditLog",
tablename="audit_log",
column_name="changes_in_row",
db_column_name="changes_in_row",
column_class_name="JSON",
column_class=JSON,
params={
"default": "{}",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)

return manager
Loading