From fdae3a3f3184dfbc3cee53f47480d3e8e99de9cd Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 11 Oct 2017 14:03:35 -0400 Subject: [PATCH] Introduced the REPORTS_ROOT config parameter; Python2 fixes --- docs/configuration/optional-settings.md | 10 +++++++++- netbox/extras/reports.py | 7 ++++--- netbox/netbox/configuration.example.py | 4 ++++ netbox/netbox/settings.py | 1 + netbox/templates/extras/report_list.html | 3 ++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/docs/configuration/optional-settings.md b/docs/configuration/optional-settings.md index fd85c0775..d34137ea0 100644 --- a/docs/configuration/optional-settings.md +++ b/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 diff --git a/netbox/extras/reports.py b/netbox/extras/reports.py index 921aab380..409aaaf30 100644 --- a/netbox/extras/reports.py +++ b/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)) diff --git a/netbox/netbox/configuration.example.py b/netbox/netbox/configuration.example.py index ce7a62464..4c4cf4277 100644 --- a/netbox/netbox/configuration.example.py +++ b/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' diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 653ec1a7b..52d9a3b59 100644 --- a/netbox/netbox/settings.py +++ b/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') diff --git a/netbox/templates/extras/report_list.html b/netbox/templates/extras/report_list.html index 023ffba7c..9cba1a71b 100644 --- a/netbox/templates/extras/report_list.html +++ b/netbox/templates/extras/report_list.html @@ -52,7 +52,8 @@ {% endfor %} {% else %}
- No reports found. +

No reports found.

+

Reports should be saved to {{ settings.REPORTS_ROOT }}. (This path can be changed by setting REPORTS_ROOT in NetBox's configuration.)

{% endif %}