From d5190b0d760703e6737ce184a9a6cdf79a2993f6 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Wed, 9 Dec 2020 19:59:14 +0000 Subject: [PATCH] Add audit log to account settings page --- app/models.py | 5 +- app/templates/admin/audit.html | 71 ++--------------------------- app/templates/macros/audit_log.html | 67 +++++++++++++++++++++++++++ app/templates/users/account.html | 5 ++ 4 files changed, 78 insertions(+), 70 deletions(-) create mode 100644 app/templates/macros/audit_log.html diff --git a/app/models.py b/app/models.py index 0bd809a7..bc5c9900 100644 --- a/app/models.py +++ b/app/models.py @@ -169,11 +169,12 @@ class User(db.Model, UserMixin): notifications = db.relationship("Notification", foreign_keys="Notification.user_id", order_by=desc(text("Notification.created_at")), back_populates="user", cascade="all, delete, delete-orphan") caused_notifications = db.relationship("Notification", foreign_keys="Notification.causer_id", - back_populates="causer", cascade="all, delete, delete-orphan") + back_populates="causer", cascade="all, delete, delete-orphan", lazy="dynamic") notification_preferences = db.relationship("UserNotificationPreferences", uselist=False, back_populates="user", cascade="all, delete, delete-orphan") - audit_log_entries = db.relationship("AuditLogEntry", foreign_keys="AuditLogEntry.causer_id", back_populates="causer") + audit_log_entries = db.relationship("AuditLogEntry", foreign_keys="AuditLogEntry.causer_id", back_populates="causer", + order_by=desc("audit_log_entry_created_at"), lazy="dynamic") packages = db.relationship("Package", back_populates="author", lazy="dynamic") reviews = db.relationship("PackageReview", back_populates="author", order_by=db.desc("package_review_created_at"), cascade="all, delete, delete-orphan") diff --git a/app/templates/admin/audit.html b/app/templates/admin/audit.html index 278b98a8..d80065ec 100644 --- a/app/templates/admin/audit.html +++ b/app/templates/admin/audit.html @@ -8,74 +8,9 @@ Audit Log

Audit Log

{% from "macros/pagination.html" import render_pagination %} + {% from "macros/audit_log.html" import render_audit_log %} + {{ render_pagination(pagination, url_set_query) }} - -
- {% for entry in log %} - - {% else %} - href="{{ entry.url }}"> - {% endif %} - -
-
- {% if entry.severity == entry.severity.MODERATION %} - - {% elif entry.severity == entry.severity.EDITOR %} - - {% elif entry.severity == entry.severity.USER %} - - {% endif %} -
- -
- {% if entry.causer %} - - - {{ entry.causer.display_name }} - {% else %} - Deleted User - {% endif %} -
- -
- {{ entry.title}} - - {% if entry.description %} - - {% endif %} -
- - {% if entry.package %} -
- - {{ entry.package.title }} - - - -
- {% endif %} - - -
- {{ entry.created_at | datetime }} -
-
-
- {% else %} -

No audit log entires.

- {% endfor %} -
- - {% from "macros/pagination.html" import render_pagination %} + {{ render_audit_log(log, show_view=True) }} {{ render_pagination(pagination, url_set_query) }} {% endblock %} diff --git a/app/templates/macros/audit_log.html b/app/templates/macros/audit_log.html new file mode 100644 index 00000000..a08e45e9 --- /dev/null +++ b/app/templates/macros/audit_log.html @@ -0,0 +1,67 @@ +{% macro render_audit_log(log, show_view=False) -%} +
+ {% for entry in log %} + + {% else %} + href="{{ entry.url }}"> + {% endif %} + +
+
+ {% if entry.severity == entry.severity.MODERATION %} + + {% elif entry.severity == entry.severity.EDITOR %} + + {% elif entry.severity == entry.severity.USER %} + + {% endif %} +
+ +
+ {% if entry.causer %} + + + {{ entry.causer.display_name }} + {% else %} + Deleted User + {% endif %} +
+ +
+ {{ entry.title}} + + {% if entry.description %} + + {% endif %} +
+ + {% if entry.package %} +
+ + {{ entry.package.title }} + + + +
+ {% endif %} + + +
+ {{ entry.created_at | datetime }} +
+
+
+ {% else %} +

No audit log entires.

+ {% endfor %} +
+{% endmacro %} diff --git a/app/templates/users/account.html b/app/templates/users/account.html index 0491fbf8..550c41b4 100644 --- a/app/templates/users/account.html +++ b/app/templates/users/account.html @@ -59,6 +59,11 @@ +

{{ _("Recent Account Actions") }}

+ +{% from "macros/audit_log.html" import render_audit_log %} +{{ render_audit_log(user.audit_log_entries.limit(10).all(), show_view=True) }} +

{{ _("Account Deletion and Deactivation") }}

{% if current_user.rank.atLeast(current_user.rank.ADMIN) %}