Skip to content

Commit

Permalink
Closes #12085: Add a file source view for reports
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Mar 29, 2023
1 parent 7155925 commit c4891fe
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/version-3.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Two new webhook trigger events have been introduced: `job_start` and `job_end`.
* [#11693](https://github.com/netbox-community/netbox/issues/11693) - Enable syncing export template content from remote sources
* [#11780](https://github.com/netbox-community/netbox/issues/11780) - Enable loading import data from remote sources
* [#11968](https://github.com/netbox-community/netbox/issues/11968) - Add navigation menu buttons to create device & VM components
* [#12085](https://github.com/netbox-community/netbox/issues/12085) - Add a file source view for reports

### Other Changes

Expand Down
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 c4891fe

Please sign in to comment.