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

Started adding a view for individual reports

This commit is contained in:
Jeremy Stretch
2017-09-27 17:39:22 -04:00
parent f4c87b3739
commit 2fbb39bf6f
6 changed files with 68 additions and 19 deletions

View File

@ -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()

View File

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

View File

@ -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({ report.result = results.get(report.full_name, None)
'name': report.name, module_reports.append(report)
'description': report.description, ret.append((module, module_reports))
'results': results.get(report.full_name, None)
})
foo.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,
}) })

View File

@ -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>

View File

@ -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 %}

View File

@ -20,20 +20,19 @@
{% for report in module_reports %} {% for report in module_reports %}
<tr> <tr>
<td> <td>
<a name="report.{{ report.name }}"></a> <a href="{% url 'extras:report' name=report.full_name %}" name="report.{{ report.name }}"><strong>{{ report.name }}</strong></a>
<strong>{{ report.name }}</strong>
</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 %} {% if report.result %}
<td>{{ report.results.created }}</td> <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 }}