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"]) @login_required(groups=["k-space:janitors"])
def view_inventory_audit(item_id): def view_inventory_audit(item_id):
user = read_user() user = read_user()
db.inventory.update_one({ item = db.inventory.find_one(filter = {
"_id": ObjectId(item_id), "_id": ObjectId(item_id)
}, { }, projection = {
"inventory.audit": 1
})
q = {
"$set": { "$set": {
"inventory.audit.username": user["username"], "inventory.audit.username": user["username"],
"inventory.audit.timestamp": datetime.utcnow(), "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): if request.form and request.form.get("noRedirect", False):
return {} return {}
else: else:

View File

@ -65,13 +65,29 @@
<td>{{ item.inventory.get("user").username | display_name }}</td> <td>{{ item.inventory.get("user").username | display_name }}</td>
</tr> </tr>
{% macro auditLine(timestamp, user) -%}
{{ timestamp | timeago }}
by
{{ user | display_name }}
{%- endmacro %}
<tr> <tr>
<td>Last audited</td> <td>Last audited</td>
<td> <td>
{% if item.inventory.get("audit") %} {% if item.inventory.get("audit") %}
{{ item.inventory.audit.timestamp | timeago }} {% if item.inventory.get("audit", {}).get("log") %}
by <details>
{{ item.inventory.audit.username | display_name }} <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 %} {% else %}
Never Never
{% endif %} {% endif %}