Parcourir la source

Added a run view for reports

Jeremy Stretch il y a 7 ans
Parent
commit
2b33e78fd3
3 fichiers modifiés avec 44 ajouts et 5 suppressions
  1. 1 0
      netbox/extras/urls.py
  2. 33 2
      netbox/extras/views.py
  3. 10 3
      netbox/templates/extras/report.html

+ 1 - 0
netbox/extras/urls.py

@@ -15,5 +15,6 @@ urlpatterns = [
     # Reports
     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>[^/]+\.[^/]+)/run/$', views.ReportRunView.as_view(), name='report_run'),
 
 ]

+ 33 - 2
netbox/extras/views.py

@@ -1,13 +1,16 @@
 from __future__ import unicode_literals
 
 from django.contrib.auth.mixins import PermissionRequiredMixin
+from django.contrib import messages
 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 utilities.forms import ConfirmationForm
 from utilities.views import ObjectDeleteView, ObjectEditView
 from .forms import ImageAttachmentForm
-from .models import ImageAttachment, ReportResult
+from .models import ImageAttachment, ReportResult, UserAction
 from .reports import get_report, get_reports
 
 
@@ -84,4 +87,32 @@ class ReportView(View):
 
         return render(request, 'extras/report.html', {
             '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)

+ 10 - 3
netbox/templates/extras/report.html

@@ -5,16 +5,23 @@
 
 {% block content %}
     <div class="row">
-        <div class="col-sm-8 col-md-9">
+        <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 class="col-sm-4 col-md-3">
-        </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>
     <div class="row">
         <div class="col-md-9">