mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 04:22:01 -06:00
#2006: Clean up report views
This commit is contained in:
parent
365b6f5e17
commit
93da6b6edf
@ -571,7 +571,6 @@ class Script(models.Model):
|
||||
"""
|
||||
Dummy model used to generate permissions for custom scripts. Does not exist in the database.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
managed = False
|
||||
|
||||
@ -585,7 +584,6 @@ class Report(models.Model):
|
||||
"""
|
||||
Dummy model used to generate permissions for reports. Does not exist in the database.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
managed = False
|
||||
|
||||
@ -606,7 +604,7 @@ class JobResult(models.Model):
|
||||
related_name='job_results',
|
||||
verbose_name='Object types',
|
||||
limit_choices_to=FeatureQuery('job_results'),
|
||||
help_text="The object type to which this job result applies.",
|
||||
help_text="The object type to which this job result applies",
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
created = models.DateTimeField(
|
||||
|
@ -342,18 +342,7 @@ class ReportListView(ContentTypePermissionRequiredMixin, View):
|
||||
})
|
||||
|
||||
|
||||
class GetReportMixin:
|
||||
def _get_report(self, name, module=None):
|
||||
if module is None:
|
||||
module, name = name.split('.', 1)
|
||||
report = get_report(module, name)
|
||||
if report is None:
|
||||
raise Http404
|
||||
|
||||
return report
|
||||
|
||||
|
||||
class ReportView(GetReportMixin, ContentTypePermissionRequiredMixin, View):
|
||||
class ReportView(ContentTypePermissionRequiredMixin, View):
|
||||
"""
|
||||
Display a single Report and its associated JobResult (if any).
|
||||
"""
|
||||
@ -362,7 +351,9 @@ class ReportView(GetReportMixin, ContentTypePermissionRequiredMixin, View):
|
||||
|
||||
def get(self, request, module, name):
|
||||
|
||||
report = self._get_report(name, module)
|
||||
report = get_report(module, name)
|
||||
if report is None:
|
||||
raise Http404
|
||||
|
||||
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
||||
report.result = JobResult.objects.filter(
|
||||
@ -382,50 +373,48 @@ class ReportView(GetReportMixin, ContentTypePermissionRequiredMixin, View):
|
||||
if not request.user.has_perm('extras.run_report'):
|
||||
return HttpResponseForbidden()
|
||||
|
||||
report = self._get_report(name, module)
|
||||
form = ConfirmationForm(request.POST)
|
||||
report = get_report(module, name)
|
||||
if report is None:
|
||||
raise Http404
|
||||
|
||||
# Allow execution only if RQ worker process is running
|
||||
if not Worker.count(get_connection('default')):
|
||||
messages.error(request, "Unable to run report: RQ worker process not running.")
|
||||
return render(request, 'extras/report.html', {
|
||||
'report': report,
|
||||
})
|
||||
|
||||
elif form.is_valid():
|
||||
# Run the Report. A new JobResult is created.
|
||||
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
||||
job_result = JobResult.enqueue_job(
|
||||
run_report,
|
||||
report.full_name,
|
||||
report_content_type,
|
||||
request.user
|
||||
)
|
||||
|
||||
# Run the Report. A new JobResult is created.
|
||||
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
||||
job_result = JobResult.enqueue_job(
|
||||
run_report,
|
||||
report.full_name,
|
||||
report_content_type,
|
||||
request.user
|
||||
)
|
||||
|
||||
return redirect('extras:report_result', job_result_pk=job_result.pk)
|
||||
|
||||
return render(request, 'extras/report.html', {
|
||||
'report': report,
|
||||
'run_form': form,
|
||||
})
|
||||
return redirect('extras:report_result', job_result_pk=job_result.pk)
|
||||
|
||||
|
||||
class ReportResultView(ContentTypePermissionRequiredMixin, GetReportMixin, View):
|
||||
|
||||
class ReportResultView(ContentTypePermissionRequiredMixin, View):
|
||||
"""
|
||||
Display a JobResult pertaining to the execution of a Report.
|
||||
"""
|
||||
def get_required_permission(self):
|
||||
return 'extras.view_report'
|
||||
|
||||
def get(self, request, job_result_pk):
|
||||
result = get_object_or_404(JobResult.objects.all(), pk=job_result_pk)
|
||||
report_content_type = ContentType.objects.get(app_label='extras', model='report')
|
||||
if result.obj_type != report_content_type:
|
||||
raise Http404
|
||||
jobresult = get_object_or_404(JobResult.objects.all(), pk=job_result_pk, obj_type=report_content_type)
|
||||
|
||||
report = self._get_report(result.name)
|
||||
# Retrieve the Report and attach the JobResult to it
|
||||
module, report_name = jobresult.name.split('.')
|
||||
report = get_report(module, report_name)
|
||||
report.result = jobresult
|
||||
|
||||
return render(request, 'extras/report_result.html', {
|
||||
'report': report,
|
||||
'result': result,
|
||||
'class_name': report.name,
|
||||
'run_form': ConfirmationForm(),
|
||||
'result': jobresult,
|
||||
})
|
||||
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
{% block title %}{{ report.name }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% block header %}
|
||||
<div class="row noprint">
|
||||
<div class="col-md-12">
|
||||
<ol class="breadcrumb">
|
||||
@ -17,21 +17,29 @@
|
||||
<div class="pull-right noprint">
|
||||
<form action="{% url 'extras:report' module=report.module name=report.class_name %}" method="post">
|
||||
{% csrf_token %}
|
||||
{{ run_form }}
|
||||
<button type="submit" name="_run" class="btn btn-primary"><i class="fa fa-play"></i> Run Report</button>
|
||||
<button type="submit" name="_run" class="btn btn-primary">
|
||||
{% if report.result %}
|
||||
<i class="fa fa-refresh"></i> Run Again
|
||||
{% else %}
|
||||
<i class="fa fa-play"></i> Run Report
|
||||
{% endif %}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
{% endif %}
|
||||
<h1>{{ report.name }}</h1>
|
||||
{% if report.description %}
|
||||
<p class="lead">{{ report.description }}</p>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
{% if report.description %}
|
||||
<p class="lead">{{ report.description }}</p>
|
||||
{% endif %}
|
||||
{% if report.result %}
|
||||
<p>Last run: <a href="{% url 'extras:report_result' job_result_pk=report.result.pk %}">
|
||||
Last run: <a href="{% url 'extras:report_result' job_result_pk=report.result.pk %}">
|
||||
<strong>{{ report.result.created }}</strong>
|
||||
</a></p>
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -15,6 +15,7 @@
|
||||
<th>Status</th>
|
||||
<th>Description</th>
|
||||
<th class="text-right">Last Run</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -36,10 +37,26 @@
|
||||
<span class="text-muted">Never</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
{% if perms.extras.run_report %}
|
||||
<div class="pull-right noprint">
|
||||
<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-xs">
|
||||
{% if report.result %}
|
||||
<i class="fa fa-refresh"></i> Run Again
|
||||
{% else %}
|
||||
<i class="fa fa-play"></i> Run Report
|
||||
{% endif %}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% for method, stats in report.result.data.items %}
|
||||
<tr>
|
||||
<td colspan="3" class="method">
|
||||
<td colspan="4" class="method">
|
||||
{{ method }}
|
||||
</td>
|
||||
<td class="text-right text-nowrap report-stats">
|
||||
|
@ -1,34 +1,12 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'extras/report.html' %}
|
||||
{% load helpers %}
|
||||
{% load static %}
|
||||
|
||||
{% block title %}{{ report.name }}{% endblock %}
|
||||
{% block title %}{{ report.name }} - {{ result.get_status_display }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row noprint">
|
||||
<div class="col-md-12">
|
||||
<ol class="breadcrumb">
|
||||
<li><a href="{% url 'extras:report_list' %}">Reports</a></li>
|
||||
<li><a href="{% url 'extras:report_list' %}#module.{{ report.module }}">{{ report.module|bettertitle }}</a></li>
|
||||
<li>{{ report.name }}</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
{% if perms.extras.run_report %}
|
||||
<div class="pull-right noprint">
|
||||
<form action="{% url 'extras:report' module=report.module name=report.name %}" method="post">
|
||||
{% csrf_token %}
|
||||
{{ run_form }}
|
||||
<button type="submit" name="_run" class="btn btn-primary"><i class="fa fa-play"></i> Run Report</button>
|
||||
</form>
|
||||
</div>
|
||||
{% endif %}
|
||||
<h1>{{ report.name }}</h1>
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
{% if report.description %}
|
||||
<p class="lead">{{ report.description }}</p>
|
||||
{% endif %}
|
||||
<p>
|
||||
Run: <strong>{{ result.created }}</strong>
|
||||
{% if result.completed %}
|
||||
|
Loading…
Reference in New Issue
Block a user