From cb19cac4c9cf475e9b925ab8fb0d1c2d5d5e15e2 Mon Sep 17 00:00:00 2001 From: Glenn Matthews Date: Wed, 4 Aug 2021 14:48:56 -0400 Subject: [PATCH 1/4] Add Job column to JobResults table. Fixes #472 --- nautobot/extras/tables.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/nautobot/extras/tables.py b/nautobot/extras/tables.py index ca852f8fd5..a54f841b0b 100644 --- a/nautobot/extras/tables.py +++ b/nautobot/extras/tables.py @@ -60,6 +60,16 @@ """ +JOB_RESULT_JOB = """ +{% if record.related_object and record.related_object.Meta and record.related_object.Meta.name %} +{{ record.related_object.Meta.name }} +{% elif record.related_object %} +{{ record.related_object }} +{% else %} +{{ record.name }} +{% endif %} +""" + OBJECTCHANGE_OBJECT = """ {% if record.changed_object and record.changed_object.get_absolute_url %} {{ record.object_repr }} @@ -259,8 +269,8 @@ def job_creator_link(value, record): class JobResultTable(BaseTable): pk = ToggleColumn() obj_type = tables.Column(verbose_name="Object Type", accessor="obj_type.name") + job = tables.TemplateColumn(template_code=JOB_RESULT_JOB, linkify=job_creator_link) name = tables.Column(linkify=job_creator_link) - job_id = tables.Column(linkify=job_creator_link, verbose_name="Job ID") created = tables.DateTimeColumn(linkify=True, format=settings.SHORT_DATETIME_FORMAT) status = tables.TemplateColumn( template_code="{% include 'extras/inc/job_label.html' with result=record %}", @@ -283,15 +293,15 @@ class Meta(BaseTable.Meta): "pk", "created", "obj_type", + "job", "name", - "job_id", "duration", "completed", "user", "status", "data", ) - default_columns = ("pk", "created", "name", "user", "status", "data") + default_columns = ("pk", "created", "job", "user", "status", "data") class ObjectChangeTable(BaseTable): From 3966a0617049a103f46d81aa3631f2385299a3f7 Mon Sep 17 00:00:00 2001 From: Glenn Matthews Date: Tue, 10 Aug 2021 09:53:38 -0400 Subject: [PATCH 2/4] Suggestion from code review --- nautobot/extras/tables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nautobot/extras/tables.py b/nautobot/extras/tables.py index a54f841b0b..fa2007ab3a 100644 --- a/nautobot/extras/tables.py +++ b/nautobot/extras/tables.py @@ -61,7 +61,7 @@ """ JOB_RESULT_JOB = """ -{% if record.related_object and record.related_object.Meta and record.related_object.Meta.name %} +{% if record.related_object.Meta.name %} {{ record.related_object.Meta.name }} {% elif record.related_object %} {{ record.related_object }} From 2117a96c023d3010cf670516dc740d87369bb6ef Mon Sep 17 00:00:00 2001 From: Glenn Matthews Date: Tue, 10 Aug 2021 09:54:06 -0400 Subject: [PATCH 3/4] Use job Meta.name if available in homepage view as well --- .../extras/templates/extras/inc/panel_jobhistory.html | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/nautobot/extras/templates/extras/inc/panel_jobhistory.html b/nautobot/extras/templates/extras/inc/panel_jobhistory.html index 9912e3c879..4eb107172c 100644 --- a/nautobot/extras/templates/extras/inc/panel_jobhistory.html +++ b/nautobot/extras/templates/extras/inc/panel_jobhistory.html @@ -1,7 +1,16 @@ {% if job_results and perms.extras.view_jobresult %} {% for result in job_results %}
- {{ result.obj_type.name }} - {{ result.name }} + + {{ result.obj_type.name }} - + {% if result.related_object.Meta.name %} + {{ result.related_object.Meta.name }} + {% elif result.related_object %} + {{ result.related_object }} + {% else %} + {{ result.name }} + {% endif %} + {% include 'extras/inc/job_label.html' %}
From b81fd9a7f8da4674f566773b08e323f197f1b813 Mon Sep 17 00:00:00 2001 From: Glenn Matthews Date: Tue, 10 Aug 2021 11:12:05 -0400 Subject: [PATCH 4/4] Refactor to reduce duplication --- nautobot/extras/models/models.py | 12 +++++++++++ nautobot/extras/tables.py | 20 +++++-------------- .../extras/inc/panel_jobhistory.html | 9 +-------- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/nautobot/extras/models/models.py b/nautobot/extras/models/models.py index 94096c4b8a..be65f09053 100644 --- a/nautobot/extras/models/models.py +++ b/nautobot/extras/models/models.py @@ -826,6 +826,18 @@ def related_object(self): return None + @property + def related_name(self): + """ + Similar to self.name, but if there's an appropriate `related_object`, use its name instead. + """ + related_object = self.related_object + if not related_object: + return self.name + if hasattr(related_object, "name"): + return related_object.name + return str(related_object) + def get_absolute_url(self): return reverse("extras:jobresult", kwargs={"pk": self.pk}) diff --git a/nautobot/extras/tables.py b/nautobot/extras/tables.py index fa2007ab3a..fcf23f957a 100644 --- a/nautobot/extras/tables.py +++ b/nautobot/extras/tables.py @@ -60,16 +60,6 @@ """ -JOB_RESULT_JOB = """ -{% if record.related_object.Meta.name %} -{{ record.related_object.Meta.name }} -{% elif record.related_object %} -{{ record.related_object }} -{% else %} -{{ record.name }} -{% endif %} -""" - OBJECTCHANGE_OBJECT = """ {% if record.changed_object and record.changed_object.get_absolute_url %} {{ record.object_repr }} @@ -269,8 +259,8 @@ def job_creator_link(value, record): class JobResultTable(BaseTable): pk = ToggleColumn() obj_type = tables.Column(verbose_name="Object Type", accessor="obj_type.name") - job = tables.TemplateColumn(template_code=JOB_RESULT_JOB, linkify=job_creator_link) - name = tables.Column(linkify=job_creator_link) + related_object = tables.Column(verbose_name="Related Object", linkify=job_creator_link, accessor="related_name") + name = tables.Column() created = tables.DateTimeColumn(linkify=True, format=settings.SHORT_DATETIME_FORMAT) status = tables.TemplateColumn( template_code="{% include 'extras/inc/job_label.html' with result=record %}", @@ -292,16 +282,16 @@ class Meta(BaseTable.Meta): fields = ( "pk", "created", - "obj_type", - "job", "name", + "obj_type", + "related_object", "duration", "completed", "user", "status", "data", ) - default_columns = ("pk", "created", "job", "user", "status", "data") + default_columns = ("pk", "created", "related_object", "user", "status", "data") class ObjectChangeTable(BaseTable): diff --git a/nautobot/extras/templates/extras/inc/panel_jobhistory.html b/nautobot/extras/templates/extras/inc/panel_jobhistory.html index 4eb107172c..7127dd17e0 100644 --- a/nautobot/extras/templates/extras/inc/panel_jobhistory.html +++ b/nautobot/extras/templates/extras/inc/panel_jobhistory.html @@ -2,14 +2,7 @@ {% for result in job_results %}