From 4b33f5793500d7776407f74c86082e2dd34b2add Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Sat, 25 Apr 2020 11:22:39 +0000 Subject: [PATCH] Make related history more extensive for users and machines --- logs/models.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/logs/models.py b/logs/models.py index 365424d9..56bf962a 100644 --- a/logs/models.py +++ b/logs/models.py @@ -301,6 +301,9 @@ class RelatedHistory: self.object_id = version.object_id self.name = version.object_repr + if self.model_name: + self.name = "{}: {}".format(self.model_name.title(), self.name) + def __eq__(self, other): return ( self.model_name == other.model_name @@ -643,11 +646,14 @@ class UserHistory(History): if obj is None: return None - # Add in "related" elements the list of Machine objects + # Add in "related" elements the list of objects # that were once owned by this user + query = Q(serialized_data__contains='"user": {}'.format(user_id)) + query &= ~Q(serialized_data__contains='"model": "users.user"') self.related = ( - Version.objects.get_for_model(Machine) - .filter(serialized_data__contains='"user": {}'.format(user_id)) + Version.objects.all() + .filter(query) + .order_by("content_type__model") ) self.related = [RelatedHistory(v) for v in self.related] self.related = list(dict.fromkeys(self.related)) @@ -732,12 +738,13 @@ class MachineHistory(History): self.event_type = MachineHistoryEvent def get(self, machine_id, model): - # Add as "related" histories the list of Interface objects - # that were once assigned to this machine - self.related = list( + query = Q(serialized_data__contains='"machine": {}'.format(machine_id)) + query &= ~Q(serialized_data__contains='"model": "machines.machine"') + + self.related = ( Version.objects.get_for_model(Interface) - .filter(serialized_data__contains='"machine": {}'.format(machine_id)) - .order_by("-revision__date_created") + .filter(query) + .order_by("content_type__model") ) # Create RelatedHistory objects and remove duplicates