2020-12-15 22:33:43 +01:00
|
|
|
from importlib import import_module
|
|
|
|
|
2021-05-26 17:35:21 +02:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
|
2020-12-15 22:33:43 +01:00
|
|
|
|
|
|
|
def import_class(path):
|
|
|
|
path_bits = path.split(".")
|
|
|
|
|
|
|
|
if len(path_bits) < 2:
|
|
|
|
message = "'{0}' is not a complete Python path.".format(path)
|
|
|
|
raise ImproperlyConfigured(message)
|
|
|
|
|
|
|
|
class_name = path_bits.pop()
|
|
|
|
module_path = ".".join(path_bits)
|
|
|
|
module_itself = import_module(module_path)
|
|
|
|
|
|
|
|
if not hasattr(module_itself, class_name):
|
2021-05-26 17:35:21 +02:00
|
|
|
message = "The Python module '{}' has no '{}' class.".format(module_path, class_name)
|
2020-12-15 22:33:43 +01:00
|
|
|
raise ImportError(message)
|
|
|
|
|
|
|
|
return getattr(module_itself, class_name)
|