From 9959b7ad0a11150ea41f2faabd9a2a7f830df245 Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Thu, 23 Apr 2020 18:27:42 +0200 Subject: [PATCH] Add pretty representation of objects in InterfaceHistory --- logs/models.py | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/logs/models.py b/logs/models.py index 6e520b93..f6c9a10c 100644 --- a/logs/models.py +++ b/logs/models.py @@ -28,10 +28,12 @@ from django.contrib.auth.models import Group from machines.models import IpList from machines.models import Interface from machines.models import Machine +from machines.models import MachineType from users.models import User from users.models import Adherent from users.models import Club from topologie.models import Room +from topologie.models import Port class MachineHistorySearchEvent: @@ -350,10 +352,7 @@ class UserHistoryEvent(HistoryEvent): return ", ".join(users) - if value is None: - return _("None") - - return value + return super(UserHistoryEvent, self)._repr(name, value) def edits(self, hide=["password", "pwd_ntlm", "gpg_fingerprint"]): """ @@ -477,6 +476,41 @@ class InterfaceHistory(History): return self.events[::-1] + def _repr(self, name, value): + """ + Returns the best representation of the given field + :param name: the name of the field + :param value: the value of the field + :return: object + """ + if name == "ipv4_id" and value is not None: + try: + return IpList.objects.get(id=value) + except IpList.DoesNotExist: + return "{} ({})".format(_("Deleted"), value) + elif name == "machine_type_id": + try: + return MachineType.objects.get(id=value).name + except MachineType.DoesNotExist: + return "{} ({})".format(_("Deleted"), value) + elif name == "machine_id": + try: + return Machine.objects.get(id=value).get_name() or _("No name") + except Machine.DoesNotExist: + return "{} ({})".format(_("Deleted"), value) + elif name == "port_lists": + if len(value) == 0: + return _("None") + + ports = [] + for pid in value: + try: + ports.append(Port.objects.get(id=pid).pretty_name()) + except Group.DoesNotExist: + ports.append("{} ({})".format(_("Deleted"), pid)) + + return super(UserHistoryEvent, self)._repr(name, value) + def _add_revision(self, version): """ Add a new revision to the chronological order