Fixes #2318: ImportError when viewing a report

This commit is contained in:
Jeremy Stretch 2018-08-07 12:10:14 -04:00
parent 507a023f41
commit f59682a7c9
2 changed files with 22 additions and 3 deletions

@ -1 +0,0 @@
Subproject commit b3a449437792668041d5cfb9cd6d025e1a5b3470

View File

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