Просмотр исходного кода

Closes #12085: Add a file source view for reports

jeremystretch 2 лет назад
Родитель
Сommit
c4891fe105

+ 1 - 0
docs/release-notes/version-3.5.md

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

+ 9 - 0
netbox/extras/reports.py

@@ -1,3 +1,4 @@
+import inspect
 import logging
 import traceback
 from datetime import timedelta
@@ -127,6 +128,14 @@ class Report(object):
         """
         return self.class_name
 
+    @property
+    def filename(self):
+        return inspect.getfile(self.__class__)
+
+    @property
+    def source(self):
+        return inspect.getsource(self.__class__)
+
     #
     # Logging methods
     #

+ 1 - 0
netbox/extras/urls.py

@@ -98,6 +98,7 @@ urlpatterns = [
     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

+ 16 - 0
netbox/extras/views.py

@@ -887,6 +887,22 @@ class ReportView(ContentTypePermissionRequiredMixin, View):
         })
 
 
+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):

+ 3 - 0
netbox/templates/extras/report/base.html

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

+ 6 - 0
netbox/templates/extras/report/source.html

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