예제 #1
0
def import_serializer(path):
    try:
        parts = path.split(".")
        module_path, class_name = ".".join(parts[:-1]), parts[-1]
        module = importlib.import_module(module_path)
        return getattr(module, class_name)()
    except (ImportError, AttributeError):
        raise ImportError("Could not import serializer '{}' from {}".format(
            class_name, path))
예제 #2
0
def import_from_string(val, setting_name):
    """
    Attempt to import a class from a string representation.
    """
    try:
        # Nod to tastypie's use of importlib.
        parts = val.split('.')
        module_path, class_name = '.'.join(parts[:-1]), parts[-1]
        module = importlib.import_module(module_path)
        return getattr(module, class_name)
    except ImportError as e:
        msg = "Could not import '%s' for API setting '%s'. %s: %s." % (val, setting_name, e.__class__.__name__, e)
        raise ImportError(msg)
예제 #3
0
def import_from_string(val, setting_name):
    """
    Attempt to import a class from a string representation.
    """
    try:
        # Nod to tastypie's use of importlib.
        parts = val.split('.')
        module_path, class_name = '.'.join(parts[:-1]), parts[-1]
        module = importlib.import_module(module_path)
        return getattr(module, class_name)
    except (ImportError, AttributeError) as e:
        msg = "Could not import '%s' for API setting '%s'. %s: %s." % (val, setting_name, e.__class__.__name__, e)
        raise ImportError(msg)
예제 #4
0
def get_resource_type(model):
    RESOURCE_TYPE_EXTRACTOR = getattr(
        settings, "REST_FRAMEWORK", None).get("RESOURCE_TYPE_EXTRACTOR", None)
    if RESOURCE_TYPE_EXTRACTOR:
        try:
            parts = RESOURCE_TYPE_EXTRACTOR.split(".")
            module_path, class_name = ".".join(parts[:-1]), parts[-1]
            module = importlib.import_module(module_path)
            return getattr(module, class_name)(model)
        except (ImportError, AttributeError) as e:
            msg = ("Could not import '{}' for API setting "
                   "'RESOURCE_TYPE_EXTRACTOR'. {}: {}.".format(
                       RESOURCE_TYPE_EXTRACTOR, e.__class__.__name__, e))
            raise ImportError(msg)
    return force_text(dasherize(underscore(model._meta.object_name)).strip())