From d6727d18e5e80bec70df26506de9ce95547023e9 Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Thu, 23 Apr 2020 19:24:28 +0200 Subject: [PATCH] Displayed related history suggestions in detailed history view --- logs/models.py | 36 +++++++++++++---------- logs/templates/logs/detailed_history.html | 23 ++++++++++++++- logs/views.py | 2 +- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/logs/models.py b/logs/models.py index b6ddc692..e48b0dd1 100644 --- a/logs/models.py +++ b/logs/models.py @@ -215,16 +215,26 @@ class MachineHistorySearch: class RelatedHistory: - def __init__(self, model_name, object_id, detailed=True): + def __init__(self, name, instance, detailed=True): """ :param model_name: Name of the related model (e.g. "user") :param object_id: ID of the related object :param detailed: Whether the related history should be shown in an detailed view """ - self.model_name = model_name - self.object_id = object_id + self.name = name + self.instance = instance self.detailed = detailed + def __eq__(self, other): + return ( + self.name == other.name + and self.instance.id == other.instance.id + and self.detailed == other.detailed + ) + + def __hash__(self): + return hash((self.name, self.instance.id, self.detailed)) + class HistoryEvent: def __init__(self, version, previous_version=None, edited_fields=None): @@ -454,14 +464,12 @@ class UserHistory(History): # Add as "related" histories the list of Machine objects # that were once owned by this user - self.related = list(filter( + self.related = filter( lambda x: x.field_dict["user_id"] == user.id, Version.objects.get_for_model(Machine).order_by("revision__date_created") - )) - self.related = sorted( - list(dict.fromkeys(self.related)), - key=lambda r: r.model_name ) + self.related = [RelatedHistory(m.get_name(), m) for m in self.related] + self.related = list(dict.fromkeys(self.related)) # Get all the versions for this user, with the oldest first self._last_version = None @@ -538,8 +546,6 @@ class MachineHistory(History): self.event_type = MachineHistoryEvent def get(self, machine): - super(MachineHistory, self).get(machine) - # Add as "related" histories the list of Interface objects # that were once assigned to this machine self.related = list(filter( @@ -547,11 +553,11 @@ class MachineHistory(History): Version.objects.get_for_model(Interface).order_by("revision__date_created") )) - # Remove duplicates and sort - self.related = sorted( - list(dict.fromkeys(self.related)), - key=lambda r: r.model_name - ) + # Create RelatedHistory objects and remove duplicates + self.related = [RelatedHistory(i.mac_address, i) for i in self.related] + self.related = list(dict.fromkeys(self.related)) + + return super(MachineHistory, self).get(machine) class InterfaceHistoryEvent(HistoryEvent): diff --git a/logs/templates/logs/detailed_history.html b/logs/templates/logs/detailed_history.html index df0f0162..53302451 100644 --- a/logs/templates/logs/detailed_history.html +++ b/logs/templates/logs/detailed_history.html @@ -1,4 +1,3 @@ -{% extends 'logs/sidebar.html' %} {% comment %} Re2o est un logiciel d'administration développé initiallement au rezometz. Il se veut agnostique au réseau considéré, de manière à être installable en @@ -73,6 +72,28 @@ with this program; if not, write to the Free Software Foundation, Inc., {% else %}

{% trans "No event" %}

{% endif %} + +

{% trans Related history %}

+ +{% if related_history %} + + + + + + + + {% for related in related_history %} + + + + + {% endfor %} +
{% trans "ID" %}{% trans "Actions" %}
{{ related.name }}{% history_button related.instance text=True detailed=related.detailed %}
+ {% include 'pagination.html' with list=events %} +{% else %} +

{% trans "No related history" %}

+{% endif %}


diff --git a/logs/views.py b/logs/views.py index a4396158..54432794 100644 --- a/logs/views.py +++ b/logs/views.py @@ -574,7 +574,7 @@ def detailed_history(request, object_name, object_id): return render( request, "logs/detailed_history.html", - {"object": instance, "events": events}, + {"object": instance, "events": events, "related_history": history.related}, )