Parcourir la source

Started adding a view for individual reports

Jeremy Stretch il y a 7 ans
Parent
commit
2fbb39bf6f

+ 2 - 0
netbox/extras/reports.py

@@ -24,6 +24,8 @@ def get_report(module_name, report_name):
     """
     """
     module = importlib.import_module('reports.{}'.format(module_name))
     module = importlib.import_module('reports.{}'.format(module_name))
     report = getattr(module, report_name, None)
     report = getattr(module, report_name, None)
+    if report is None:
+        return None
     return report()
     return report()
 
 
 
 

+ 1 - 0
netbox/extras/urls.py

@@ -14,5 +14,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'),
 
 
 ]
 ]

+ 28 - 12
netbox/extras/views.py

@@ -1,16 +1,14 @@
 from __future__ import unicode_literals
 from __future__ import unicode_literals
-from collections import OrderedDict
 
 
 from django.contrib.auth.mixins import PermissionRequiredMixin
 from django.contrib.auth.mixins import PermissionRequiredMixin
+from django.http import Http404
 from django.shortcuts import get_object_or_404, render
 from django.shortcuts import get_object_or_404, render
-from django.urls import reverse
 from django.views.generic import View
 from django.views.generic import View
 
 
-from . import reports
 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
-from .reports import get_reports
+from .reports import get_report, get_reports
 
 
 
 
 #
 #
@@ -55,17 +53,35 @@ class ReportListView(View):
         reports = get_reports()
         reports = get_reports()
         results = {r.report: r for r in ReportResult.objects.all()}
         results = {r.report: r for r in ReportResult.objects.all()}
 
 
-        foo = []
+        ret = []
         for module, report_list in reports:
         for module, report_list in reports:
             module_reports = []
             module_reports = []
             for report in report_list:
             for report in report_list:
-                module_reports.append({
-                    'name': report.name,
-                    'description': report.description,
-                    'results': results.get(report.full_name, None)
-                })
-            foo.append((module, module_reports))
+                report.result = results.get(report.full_name, None)
+                module_reports.append(report)
+            ret.append((module, module_reports))
 
 
         return render(request, 'extras/report_list.html', {
         return render(request, 'extras/report_list.html', {
-            'reports': foo,
+            'reports': ret,
+        })
+
+
+class ReportView(View):
+    """
+    Display a single Report and its associated ReportResult (if any).
+    """
+
+    def get(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
+
+        # Attach the ReportResult (if any)
+        report.result = ReportResult.objects.filter(report=report.full_name).first()
+
+        return render(request, 'extras/report.html', {
+            'report': report,
         })
         })

+ 2 - 2
netbox/templates/extras/inc/report_label.html

@@ -1,6 +1,6 @@
-{% if report.results.failed %}
+{% if report.result.failed %}
     <label class="label label-danger">Failed</label>
     <label class="label label-danger">Failed</label>
-{% elif report.results %}
+{% elif report.result %}
     <label class="label label-success">Passed</label>
     <label class="label label-success">Passed</label>
 {% else %}
 {% else %}
     <label class="label label-default">N/A</label>
     <label class="label label-default">N/A</label>

+ 31 - 0
netbox/templates/extras/report.html

@@ -0,0 +1,31 @@
+{% extends '_base.html' %}
+{% load helpers %}
+
+{% block title %}{{ report.name }}{% endblock %}
+
+{% block content %}
+    <div class="row">
+        <div class="col-sm-8 col-md-9">
+            <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>
+    <h1>{{ report.name }}{% include 'extras/inc/report_label.html' %}</h1>
+    <div class="row">
+        <div class="col-md-9">
+            {% if report.description %}
+                <p class="lead">{{ report.description }}</p>
+            {% endif %}
+            {% if report.result %}
+                <p>Last run: {{ report.result.created }}</p>
+            {% else %}
+                <p class="text-muted">Last run: Never</p>
+            {% endif %}
+        </div>
+    </div>
+{% endblock %}

+ 4 - 5
netbox/templates/extras/report_list.html

@@ -20,20 +20,19 @@
                         {% for report in module_reports %}
                         {% for report in module_reports %}
                             <tr>
                             <tr>
                                 <td>
                                 <td>
-                                    <a name="report.{{ report.name }}"></a>
-                                    <strong>{{ report.name }}</strong>
+                                    <a href="{% url 'extras:report' name=report.full_name %}" name="report.{{ report.name }}"><strong>{{ report.name }}</strong></a>
                                 </td>
                                 </td>
                                 <td>
                                 <td>
                                     {% include 'extras/inc/report_label.html' %}
                                     {% include 'extras/inc/report_label.html' %}
                                 </td>
                                 </td>
                                 <td>{{ report.description|default:"" }}</td>
                                 <td>{{ report.description|default:"" }}</td>
-                                {% if report.results %}
-                                    <td>{{ report.results.created }}</td>
+                                {% if report.result %}
+                                    <td>{{ report.result.created }}</td>
                                 {% else %}
                                 {% else %}
                                     <td class="text-muted">Never</td>
                                     <td class="text-muted">Never</td>
                                 {% endif %}
                                 {% endif %}
                             </tr>
                             </tr>
-                            {% for method, stats in report.results.data.items %}
+                            {% for method, stats in report.result.data.items %}
                                 <tr>
                                 <tr>
                                     <td colspan="3" class="method">
                                     <td colspan="3" class="method">
                                         {{ method }}
                                         {{ method }}