From f59682a7c955baa6b336ebe927d69c26f725ad67 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 7 Aug 2018 12:10:14 -0400 Subject: [PATCH] Fixes #2318: ImportError when viewing a report --- netbox/_reports | 1 - netbox/extras/reports.py | 24 ++++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) delete mode 160000 netbox/_reports diff --git a/netbox/_reports b/netbox/_reports deleted file mode 160000 index b3a449437..000000000 --- a/netbox/_reports +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b3a449437792668041d5cfb9cd6d025e1a5b3470 diff --git a/netbox/extras/reports.py b/netbox/extras/reports.py index a516a0b09..52883063c 100644 --- a/netbox/extras/reports.py +++ b/netbox/extras/reports.py @@ -1,9 +1,10 @@ from __future__ import unicode_literals +from collections import OrderedDict import importlib import inspect import pkgutil -from collections import OrderedDict +import sys from django.conf import settings from django.utils import timezone @@ -23,10 +24,29 @@ def get_report(module_name, report_name): """ Return a specific report from within a module. """ - module = importlib.import_module(module_name) + file_path = '{}/{}.py'.format(settings.REPORTS_ROOT, module_name) + + # Python 3.5+ + if sys.version_info >= (3, 5): + spec = importlib.util.spec_from_file_location(module_name, file_path) + module = importlib.util.module_from_spec(spec) + try: + spec.loader.exec_module(module) + except FileNotFoundError: + return None + + # Python 2.7 + else: + import imp + try: + module = imp.load_source(module_name, file_path) + except IOError: + return None + report = getattr(module, report_name, None) if report is None: return None + return report()