Skip to content

Commit

Permalink
Closes #12085: Add ReportSourceView
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Mar 29, 2023
1 parent 27f1c6e commit 2c6400f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions netbox/extras/reports.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import logging
import traceback
from datetime import timedelta
Expand Down Expand Up @@ -127,6 +128,14 @@ def name(self):
"""
return self.class_name

@property
def filename(self):
return inspect.getfile(self.__class__)

@property
def source(self):
return inspect.getsource(self.__class__)

#
# Logging methods
#
Expand Down
1 change: 1 addition & 0 deletions netbox/extras/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
path('reports/results/<int:job_pk>/', views.ReportResultView.as_view(), name='report_result'),
path('reports/<int:pk>/', include(get_model_urls('extras', 'reportmodule'))),
path('reports/<str:module>/<str:name>/', views.ReportView.as_view(), name='report'),
path('reports/<str:module>.<str:name>/source/', views.ReportSourceView.as_view(), name='report_source'),
path('reports/<str:module>/<str:name>/jobs/', views.ReportJobsView.as_view(), name='report_jobs'),

# Scripts
Expand Down
16 changes: 16 additions & 0 deletions netbox/extras/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,22 @@ def post(self, request, module, name):
})


class ReportSourceView(ContentTypePermissionRequiredMixin, View):

def get_required_permission(self):
return 'extras.view_report'

def get(self, request, module, name):
module = get_object_or_404(ReportModule.objects.restrict(request.user), file_path__startswith=module)
report = module.reports[name]()

return render(request, 'extras/report/source.html', {
'module': module,
'report': report,
'tab': 'source',
})


class ReportJobsView(ContentTypePermissionRequiredMixin, View):

def get_required_permission(self):
Expand Down
3 changes: 3 additions & 0 deletions netbox/templates/extras/report/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<li class="nav-item" role="presentation">
<a class="nav-link{% if not tab %} active{% endif %}" href="{% url 'extras:report' module=report.module name=report.class_name %}">Report</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link{% if tab == 'source' %} active{% endif %}" href="{% url 'extras:report_source' module=report.module name=report.class_name %}">Source</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link{% if tab == 'jobs' %} active{% endif %}" href="{% url 'extras:report_jobs' module=report.module name=report.class_name %}">Jobs</a>
</li>
Expand Down
6 changes: 6 additions & 0 deletions netbox/templates/extras/report/source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'extras/report/base.html' %}

{% block content %}
<code class="h6 my-3 d-block">{{ report.filename }}</code>
<pre class="block">{{ report.source }}</pre>
{% endblock %}

0 comments on commit 2c6400f

Please sign in to comment.