38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
from molodetz.database.core import db
|
|
|
|
|
|
def insert_audit(row, links):
|
|
db["audit_log"].insert(row)
|
|
for link in links:
|
|
db["audit_links"].insert(link)
|
|
|
|
|
|
def list_audit(limit=200, category=None):
|
|
if "audit_log" not in db.tables:
|
|
return []
|
|
criteria = {}
|
|
if category:
|
|
criteria["category"] = category
|
|
return list(db["audit_log"].find(order_by=["-created_at"], _limit=limit, **criteria))
|
|
|
|
|
|
def count_audit():
|
|
if "audit_log" not in db.tables:
|
|
return 0
|
|
return db["audit_log"].count()
|
|
|
|
|
|
def prune_audit(before_iso, dry_run=False):
|
|
if "audit_log" not in db.tables:
|
|
return 0
|
|
rows = list(db.query("SELECT uid FROM audit_log WHERE created_at < :before", before=before_iso))
|
|
if rows and not dry_run:
|
|
with db:
|
|
db.query(
|
|
"DELETE FROM audit_links WHERE audit_uid IN (SELECT uid FROM audit_log WHERE created_at < :before)",
|
|
before=before_iso,
|
|
)
|
|
db.query("DELETE FROM audit_log WHERE created_at < :before", before=before_iso)
|
|
return len(rows)
|