Introduce get_latest_jobs() method on JobsMixin

This commit is contained in:
jeremystretch 2023-03-29 10:23:32 -04:00
parent c915d2360c
commit 556353c2f3
5 changed files with 81 additions and 82 deletions

View File

@ -39,7 +39,8 @@ class Job(models.Model):
)
object = GenericForeignKey(
ct_field='object_type',
fk_field='object_id'
fk_field='object_id',
for_concrete_model=False
)
name = models.CharField(
max_length=200

View File

@ -819,18 +819,9 @@ class ReportListView(ContentTypePermissionRequiredMixin, View):
def get(self, request):
report_modules = ReportModule.objects.restrict(request.user)
jobs = {
r.name: r
for r in Job.objects.filter(
object_type=ContentType.objects.get_by_natural_key('extras', 'reportmodule'),
status__in=JobStatusChoices.TERMINAL_STATE_CHOICES
).order_by('name', '-created').distinct('name').defer('data')
}
return render(request, 'extras/report_list.html', {
'model': ReportModule,
'report_modules': report_modules,
'jobs': jobs,
})
@ -986,18 +977,9 @@ class ScriptListView(ContentTypePermissionRequiredMixin, View):
def get(self, request):
script_modules = ScriptModule.objects.restrict(request.user)
jobs = {
r.name: r
for r in Job.objects.filter(
object_type=ContentType.objects.get_by_natural_key('extras', 'scriptmodule'),
status__in=JobStatusChoices.TERMINAL_STATE_CHOICES
).order_by('name', '-created').distinct('name').defer('data')
}
return render(request, 'extras/script_list.html', {
'model': ScriptModule,
'script_modules': script_modules,
'jobs': jobs,
})

View File

@ -9,9 +9,9 @@ from django.db.models.signals import class_prepared
from django.dispatch import receiver
from django.utils import timezone
from django.utils.translation import gettext as _
from taggit.managers import TaggableManager
from core.choices import JobStatusChoices
from extras.choices import CustomFieldVisibilityChoices, ObjectChangeActionChoices
from extras.utils import is_taggable, register_features
from netbox.registry import registry
@ -302,12 +302,24 @@ class JobsMixin(models.Model):
jobs = GenericRelation(
to='core.Job',
content_type_field='object_type',
object_id_field='object_id'
object_id_field='object_id',
for_concrete_model=False
)
class Meta:
abstract = True
def get_latest_jobs(self):
"""
Return a dictionary mapping of the most recent jobs for this instance.
"""
return {
job.name: job
for job in self.jobs.filter(
status__in=JobStatusChoices.TERMINAL_STATE_CHOICES
).order_by('name', '-created').distinct('name').defer('data')
}
class JournalingMixin(models.Model):
"""

View File

@ -49,19 +49,20 @@
</tr>
</thead>
<tbody>
{% with jobs=module.get_latest_jobs %}
{% for report_name, report in module.reports.items %}
{% with last_result=jobs|get_key:report.name %}
{% with last_job=jobs|get_key:report.name %}
<tr>
<td>
<a href="{% url 'extras:report' module=module.path name=report.class_name %}" id="{{ report.module }}.{{ report.class_name }}">{{ report.name }}</a>
</td>
<td>{{ report.description|markdown|placeholder }}</td>
{% if last_result %}
{% if last_job %}
<td>
<a href="{% url 'extras:report_result' job_pk=last_result.pk %}">{{ last_result.created|annotated_date }}</a>
<a href="{% url 'extras:report_result' job_pk=last_job.pk %}">{{ last_job.created|annotated_date }}</a>
</td>
<td>
{% badge last_result.get_status_display last_result.get_status_color %}
{% badge last_job.get_status_display last_job.get_status_color %}
</td>
{% else %}
<td class="text-muted">Never</td>
@ -73,7 +74,7 @@
<form action="{% url 'extras:report' module=report.module name=report.class_name %}" method="post">
{% csrf_token %}
<button type="submit" name="_run" class="btn btn-primary btn-sm" style="width: 110px">
{% if last_result %}
{% if last_job %}
<i class="mdi mdi-replay"></i> Run Again
{% else %}
<i class="mdi mdi-play"></i> Run Report
@ -84,7 +85,7 @@
{% endif %}
</td>
</tr>
{% for method, stats in last_result.data.items %}
{% for method, stats in last_job.data.items %}
<tr>
<td colspan="4" class="method">
<span class="ps-3">{{ method }}</span>
@ -99,6 +100,7 @@
{% endfor %}
{% endwith %}
{% endfor %}
{% endwith %}
</tbody>
</table>
</div>

View File

@ -47,8 +47,8 @@
</tr>
</thead>
<tbody>
{% with jobs=module.get_latest_jobs %}
{% for script_name, script_class in module.scripts.items %}
{% with last_result=jobs|get_key:script_class.name %}
<tr>
<td>
<a href="{% url 'extras:script' module=module.path name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a>
@ -56,6 +56,7 @@
<td>
{{ script_class.Meta.description|markdown|placeholder }}
</td>
{% with last_result=jobs|get_key:script_class.name %}
{% if last_result %}
<td>
<a href="{% url 'extras:script_result' job_pk=last_result.pk %}">{{ last_result.created|annotated_date }}</a>
@ -67,9 +68,10 @@
<td class="text-muted">Never</td>
<td class="text-end">{{ ''|placeholder }}</td>
{% endif %}
</tr>
{% endwith %}
</tr>
{% endfor %}
{% endwith %}
</tbody>
</table>
</div>