1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Added a run view for reports

This commit is contained in:
Jeremy Stretch
2017-09-28 12:51:10 -04:00
parent b65e9fe0f5
commit 2b33e78fd3
3 changed files with 44 additions and 5 deletions

View File

@ -15,5 +15,6 @@ urlpatterns = [
# Reports # Reports
url(r'^reports/$', views.ReportListView.as_view(), name='report_list'), url(r'^reports/$', views.ReportListView.as_view(), name='report_list'),
url(r'^reports/(?P<name>[^/]+\.[^/]+)/$', views.ReportView.as_view(), name='report'), url(r'^reports/(?P<name>[^/]+\.[^/]+)/$', views.ReportView.as_view(), name='report'),
url(r'^reports/(?P<name>[^/]+\.[^/]+)/run/$', views.ReportRunView.as_view(), name='report_run'),
] ]

View File

@ -1,13 +1,16 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from django.contrib import messages
from django.http import Http404 from django.http import Http404
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, redirect, render
from django.utils.safestring import mark_safe
from django.views.generic import View from django.views.generic import View
from utilities.forms import ConfirmationForm
from utilities.views import ObjectDeleteView, ObjectEditView from utilities.views import ObjectDeleteView, ObjectEditView
from .forms import ImageAttachmentForm from .forms import ImageAttachmentForm
from .models import ImageAttachment, ReportResult from .models import ImageAttachment, ReportResult, UserAction
from .reports import get_report, get_reports from .reports import get_report, get_reports
@ -84,4 +87,32 @@ class ReportView(View):
return render(request, 'extras/report.html', { return render(request, 'extras/report.html', {
'report': report, 'report': report,
'run_form': ConfirmationForm(),
}) })
class ReportRunView(PermissionRequiredMixin, View):
"""
Run a Report and record a new ReportResult.
"""
permission_required = 'extras.add_reportresult'
def post(self, request, name):
# Retrieve the Report by "<module>.<report>"
module_name, report_name = name.split('.')
report = get_report(module_name, report_name)
if report is None:
raise Http404
form = ConfirmationForm(request.POST)
if form.is_valid():
# Run the Report. A new ReportResult is created.
report.run()
result = 'failed' if report.failed else 'passed'
msg = "Ran report {} ({})".format(report.full_name, result)
messages.success(request, mark_safe(msg))
UserAction.objects.log_create(request.user, report.result, msg)
return redirect('extras:report', name=report.full_name)

View File

@ -5,16 +5,23 @@
{% block content %} {% block content %}
<div class="row"> <div class="row">
<div class="col-sm-8 col-md-9"> <div class="col-md-12">
<ol class="breadcrumb"> <ol class="breadcrumb">
<li><a href="{% url 'extras:report_list' %}">Reports</a></li> <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><a href="{% url 'extras:report_list' %}#module.{{ report.module }}">{{ report.module|bettertitle }}</a></li>
<li>{{ report.name }}</li> <li>{{ report.name }}</li>
</ol> </ol>
</div> </div>
<div class="col-sm-4 col-md-3">
</div>
</div> </div>
{% if perms.extras.add_reportresult %}
<div class="pull-right">
<form action="{% url 'extras:report_run' name=report.full_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 }}{% include 'extras/inc/report_label.html' %}</h1> <h1>{{ report.name }}{% include 'extras/inc/report_label.html' %}</h1>
<div class="row"> <div class="row">
<div class="col-md-9"> <div class="col-md-9">