Parcourir la source

Introduced the REPORTS_ROOT config parameter; Python2 fixes

Jeremy Stretch il y a 7 ans
Parent
commit
fdae3a3f31

+ 9 - 1
docs/configuration/optional-settings.md

@@ -145,7 +145,7 @@ An API consumer can request an arbitrary number of objects by appending the "lim
 
 Default: $BASE_DIR/netbox/media/
 
-The file path to the location where media files (such as image attachments) are stored. By default, this is the `netbox/media` directory within the base NetBox installation path.
+The file path to the location where media files (such as image attachments) are stored. By default, this is the `netbox/media/` directory within the base NetBox installation path.
 
 ---
 
@@ -207,6 +207,14 @@ When determining the primary IP address for a device, IPv6 is preferred over IPv
 
 ---
 
+## REPORTS_ROOT
+
+Default: $BASE_DIR/netbox/reports/
+
+The file path to the location where custom reports will be kept. By default, this is the `netbox/reports/` directory within the base NetBox installation path.
+
+---
+
 ## TIME_ZONE
 
 Default: UTC

+ 4 - 3
netbox/extras/reports.py

@@ -1,13 +1,14 @@
+from __future__ import unicode_literals
 from collections import OrderedDict
 import importlib
 import inspect
 import pkgutil
 
+from django.conf import settings
 from django.utils import timezone
 
 from .constants import LOG_DEFAULT, LOG_FAILURE, LOG_INFO, LOG_LEVEL_CODES, LOG_SUCCESS, LOG_WARNING
 from .models import ReportResult
-import reports as custom_reports
 
 
 def is_report(obj):
@@ -42,9 +43,9 @@ def get_reports():
     """
     module_list = []
 
-    # Iterate through all modules within the reports path. These are the user-defined files in which reports are
+    # Iterate through all modules within the reports path. These are the user-created files in which reports are
     # defined.
-    for importer, module_name, is_pkg in pkgutil.walk_packages(custom_reports.__path__):
+    for importer, module_name, is_pkg in pkgutil.walk_packages([settings.REPORTS_ROOT]):
         module = importlib.import_module('reports.{}'.format(module_name))
         report_list = [cls() for _, cls in inspect.getmembers(module, is_report)]
         module_list.append((module_name, report_list))

+ 4 - 0
netbox/netbox/configuration.example.py

@@ -118,6 +118,10 @@ PAGINATE_COUNT = 50
 # prefer IPv4 instead.
 PREFER_IPV4 = False
 
+# The file path where custom reports will be stored. A trailing slash is not needed. Note that the default value of
+# this setting is derived from the installed location.
+# REPORTS_ROOT = '/opt/netbox/netbox/reports'
+
 # Time zone (default: UTC)
 TIME_ZONE = 'UTC'
 

+ 1 - 0
netbox/netbox/settings.py

@@ -54,6 +54,7 @@ NAPALM_TIMEOUT = getattr(configuration, 'NAPALM_TIMEOUT', 30)
 NAPALM_ARGS = getattr(configuration, 'NAPALM_ARGS', {})
 PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50)
 PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False)
+REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/')
 SHORT_DATE_FORMAT = getattr(configuration, 'SHORT_DATE_FORMAT', 'Y-m-d')
 SHORT_DATETIME_FORMAT = getattr(configuration, 'SHORT_DATETIME_FORMAT', 'Y-m-d H:i')
 SHORT_TIME_FORMAT = getattr(configuration, 'SHORT_TIME_FORMAT', 'H:i:s')

+ 2 - 1
netbox/templates/extras/report_list.html

@@ -52,7 +52,8 @@
                 {% endfor %}
             {% else %}
                 <div class="alert alert-info">
-                    <strong>No reports found.</strong>
+                    <p><strong>No reports found.</strong></p>
+                    <p>Reports should be saved to <code>{{ settings.REPORTS_ROOT }}</code>. (This path can be changed by setting <code>REPORTS_ROOT</code> in NetBox's configuration.)</p>
                 </div>
             {% endif %}
         </div>