Add audit history
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Madis Mägi 2024-08-06 15:43:48 +03:00
parent aa76374f1f
commit 495ddd3e21
2 changed files with 38 additions and 7 deletions

View File

@ -447,14 +447,29 @@ def view_inventory(slug=None):
@login_required(groups=["k-space:janitors"])
def view_inventory_audit(item_id):
user = read_user()
db.inventory.update_one({
"_id": ObjectId(item_id),
}, {
item = db.inventory.find_one(filter = {
"_id": ObjectId(item_id)
}, projection = {
"inventory.audit": 1
})
q = {
"$set": {
"inventory.audit.username": user["username"],
"inventory.audit.timestamp": datetime.utcnow(),
},
})
}
if item["inventory"].get("audit"):
q["$push"] = {
"inventory.audit.log": {
"username": item["inventory"]["audit"]["username"],
"timestamp": item["inventory"]["audit"]["timestamp"],
}
}
db.inventory.update_one({
"_id": ObjectId(item_id)
}, q)
if request.form and request.form.get("noRedirect", False):
return {}
else:

View File

@ -65,13 +65,29 @@
<td>{{ item.inventory.get("user").username | display_name }}</td>
</tr>
{% macro auditLine(timestamp, user) -%}
{{ timestamp | timeago }}
by
{{ user | display_name }}
{%- endmacro %}
<tr>
<td>Last audited</td>
<td>
{% if item.inventory.get("audit") %}
{{ item.inventory.audit.timestamp | timeago }}
by
{{ item.inventory.audit.username | display_name }}
{% if item.inventory.get("audit", {}).get("log") %}
<details>
<summary>
{{ auditLine(item.inventory.audit.timestamp, item.inventory.audit.username) }}
</summary>
<ul class="browser-default">
{% for audit in item.inventory.audit.log | reverse %}
<li>{{ auditLine(audit.timestamp, audit.username) }}</li>
{% endfor %}
</ul>
</details>
{% else %}
{{ auditLine(item.inventory.audit.timestamp, item.inventory.audit.username) }}
{% endif %}
{% else %}
Never
{% endif %}