Add audit log to account settings page

This commit is contained in:
rubenwardy
2020-12-09 19:59:14 +00:00
parent 58e1b924ca
commit d5190b0d76
4 changed files with 78 additions and 70 deletions

View File

@@ -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")

View File

@@ -8,74 +8,9 @@ Audit Log
<h1>Audit Log</h1>
{% from "macros/pagination.html" import render_pagination %}
{% from "macros/audit_log.html" import render_audit_log %}
{{ render_pagination(pagination, url_set_query) }}
<div class="list-group mt-3">
{% for entry in log %}
<a class="list-group-item list-group-item-action"
{% if entry.description %}
href="{{ url_for('admin.audit_view', id=entry.id) }}">
{% else %}
href="{{ entry.url }}">
{% endif %}
<div class="row {% if entry.severity == entry.severity.NORMAL %}text-muted{% endif %}">
<div class="col-sm-auto text-center" style="width: 50px;"
title="{{ _('Severity: %(sev)s.', sev=entry.severity.getTitle()) }}">
{% if entry.severity == entry.severity.MODERATION %}
<i class="fas fa-exclamation-triangle" style="color: yellow;"></i>
{% elif entry.severity == entry.severity.EDITOR %}
<i class="fas fa-users" style="color: #537eac;"></i>
{% elif entry.severity == entry.severity.USER %}
<i class="fas fa-user"></i>
{% endif %}
</div>
<div class="col-sm-2 text-muted">
{% if entry.causer %}
<img
class="img-fluid user-photo img-thumbnail img-thumbnail-1"
style="max-height: 22px;"
src="{{ entry.causer.getProfilePicURL() }}" />
<span class="pl-2">{{ entry.causer.display_name }}</span>
{% else %}
<i>Deleted User</i>
{% endif %}
</div>
<div class="col-sm">
{{ entry.title}}
{% if entry.description %}
<i class="fas fa-paperclip ml-3"></i>
{% endif %}
</div>
{% if entry.package %}
<div class="col-sm-auto text-muted">
<span class="pr-2">
{{ entry.package.title }}
</span>
<img
class="img-fluid"
style="max-height: 22px; max-width: 22px;"
src="{{ entry.package.getThumbnailURL(1) }}" />
</div>
{% endif %}
<div class="col-sm-auto text-muted">
{{ entry.created_at | datetime }}
</div>
</div>
</a>
{% else %}
<p class="list-group-item"><i>No audit log entires.</i></p>
{% endfor %}
</div>
{% from "macros/pagination.html" import render_pagination %}
{{ render_audit_log(log, show_view=True) }}
{{ render_pagination(pagination, url_set_query) }}
{% endblock %}

View File

@@ -0,0 +1,67 @@
{% macro render_audit_log(log, show_view=False) -%}
<div class="list-group mt-3">
{% for entry in log %}
<a class="list-group-item list-group-item-action"
{% if entry.description and show_view %}
href="{{ url_for('admin.audit_view', id=entry.id) }}">
{% else %}
href="{{ entry.url }}">
{% endif %}
<div class="row {% if entry.severity == entry.severity.NORMAL %}text-muted{% endif %}">
<div class="col-sm-auto text-center" style="width: 50px;"
title="{{ _('Severity: %(sev)s.', sev=entry.severity.getTitle()) }}">
{% if entry.severity == entry.severity.MODERATION %}
<i class="fas fa-exclamation-triangle" style="color: yellow;"></i>
{% elif entry.severity == entry.severity.EDITOR %}
<i class="fas fa-users" style="color: #537eac;"></i>
{% elif entry.severity == entry.severity.USER %}
<i class="fas fa-user"></i>
{% endif %}
</div>
<div class="col-sm-2 text-muted">
{% if entry.causer %}
<img
class="img-fluid user-photo img-thumbnail img-thumbnail-1"
style="max-height: 22px;"
src="{{ entry.causer.getProfilePicURL() }}" />
<span class="pl-2">{{ entry.causer.display_name }}</span>
{% else %}
<i>Deleted User</i>
{% endif %}
</div>
<div class="col-sm">
{{ entry.title}}
{% if entry.description %}
<i class="fas fa-paperclip ml-3"></i>
{% endif %}
</div>
{% if entry.package %}
<div class="col-sm-auto text-muted">
<span class="pr-2">
{{ entry.package.title }}
</span>
<img
class="img-fluid"
style="max-height: 22px; max-width: 22px;"
src="{{ entry.package.getThumbnailURL(1) }}" />
</div>
{% endif %}
<div class="col-sm-auto text-muted">
{{ entry.created_at | datetime }}
</div>
</div>
</a>
{% else %}
<p class="list-group-item"><i>No audit log entires.</i></p>
{% endfor %}
</div>
{% endmacro %}

View File

@@ -59,6 +59,11 @@
</tr>
</table>
<h3>{{ _("Recent Account Actions") }}</h3>
{% from "macros/audit_log.html" import render_audit_log %}
{{ render_audit_log(user.audit_log_entries.limit(10).all(), show_view=True) }}
<h3>{{ _("Account Deletion and Deactivation") }}</h3>
{% if current_user.rank.atLeast(current_user.rank.ADMIN) %}