# Path: django_core/utils/loading.py
# def get_class_from_settings(settings_key):
#     """Gets a class from a setting key.  This will first check loaded models,
#     then look in installed apps, then fallback to import from lib.
# 
#     :param settings_key: the key defined in settings to the value for
#     """
#     cls_path = getattr(settings, settings_key, None)
#     if not cls_path:
#         raise NotImplementedError()
# 
#     try:
#         # First check to see if it's an installed model
#         return get_model_from_settings(settings_key=settings_key)
#     except:
#         try:
#             # Next, check from installed apps
#             return get_class_from_settings_from_apps(settings_key=settings_key)
#         except:
#             # Last, try to load from the full path
#             return get_class_from_settings_full_path(settings_key)
# 
# Path: django_core/utils/loading.py
# def get_class_from_settings_from_apps(settings_key):
#     """Try and get a class from a settings path by lookin in installed apps.
#     """
#     cls_path = getattr(settings, settings_key, None)
# 
#     if not cls_path:
#         raise NotImplementedError()
# 
#     try:
#         app_label = cls_path.split('.')[-2]
#         model_name = cls_path.split('.')[-1]
#     except ValueError:
#         raise ImproperlyConfigured("{0} must be of the form "
#                                    "'app_label.model_name'".format(
#                                                                 settings_key))
# 
#     app = apps.get_app_config(app_label).models_module
# 
#     if not app:
#         raise ImproperlyConfigured("{0} setting refers to an app that has not "
#                                    "been installed".format(settings_key))
# 
#     return getattr(app, model_name)
# 
# Path: django_core/utils/loading.py
# def get_class_from_settings_full_path(settings_key):
#     """Get a class from it's full path.
# 
#     Example:
# 
#     some.path.module.MyClass
#     """
#     cls_path = getattr(settings, settings_key, None)
# 
#     if not cls_path:
#         raise NotImplementedError()
# 
#     try:
#         module_name, class_name = cls_path.rsplit('.', 1)
#     except ValueError:
#         raise ImproperlyConfigured("{0} must be of the form "
#                                    "'some.path.module.MyClass'".format(
#                                                                 settings_key))
# 
#     manager_module = importlib.import_module(module_name)
# 
#     if not manager_module:
#         raise ImproperlyConfigured("{0} refers to a module that has not been "
#                                    "installed".format(settings_key))
# 
#     return getattr(manager_module, class_name)
# 
# Path: django_core/utils/loading.py
# def get_function_from_settings(settings_key):
#     """Gets a function from the string path defined in a settings file.
# 
#     Example:
# 
#     # my_app/my_file.py
#     def some_function():
#         # do something
#         pass
# 
#     # settings.py
#     SOME_FUNCTION = 'my_app.my_file.some_function'
# 
#     > get_function_from_settings('SOME_FUNCTION')
#     <function my_app.my_file.some_function>
#     """
# 
#     renderer_func_str = getattr(settings, settings_key, None)
#     if not renderer_func_str:
#         return None
# 
#     module_str, renderer_func_name = renderer_func_str.rsplit('.', 1)
# 
#     try:
#         mod = importlib.import_module(module_str)
#         return getattr(mod, renderer_func_name)
#     except Exception:
#         return None
# 
# Path: django_core/utils/loading.py
# def get_model_from_settings(settings_key):
#     """Return the django model from a settings key.
# 
#     This is the same pattern user for django's "get_user_model()" method. To
#     allow you to set the model instance to a different model subclass.
# 
#     :param settings_key: the key defined in settings to the value for
# 
#     """
#     cls_path = getattr(settings, settings_key, None)
#     if not cls_path:
#         raise NotImplementedError()
# 
#     try:
#         app_label, model_name = cls_path.split('.')
#     except ValueError:
#         raise ImproperlyConfigured("{0} must be of the form "
#                                    "'app_label.model_name'".format(settings_key))
# 
#     model = apps.get_model(app_label, model_name)
# 
#     if model is None:
#         raise ImproperlyConfigured("{0} refers to model '%s' that has not "
#                                    "been installed".format(settings_key))
# 
#     return model


from django.test.testcases import TestCase
from django.test.utils import override_settings
from django_core.utils.loading import get_class_from_settings
from django_core.utils.loading import get_class_from_settings_from_apps
from django_core.utils.loading import get_class_from_settings_full_path
from django_core.utils.loading import get_function_from_settings
from django_core.utils.loading import get_model_from_settings
from test_objects.models import TestModel



class LoadingTestCase(TestCase):
    """TestCase for loading classes."""

    @override_settings(MY_MODEL_SETTING='test_objects.TestModel')
    def test_get_class_from_settings(self):
        model = get_class_from_settings(settings_key='MY_MODEL_SETTING')
        self.assertEqual(model, TestModel)

    @override_settings(MY_MODEL_SETTING='test_objects.TestModel')
    def test_get_model_from_settings(self):

# Path: django_core/utils/loading.py
# def get_class_from_settings(settings_key):
#     """Gets a class from a setting key.  This will first check loaded models,
#     then look in installed apps, then fallback to import from lib.
# 
#     :param settings_key: the key defined in settings to the value for
#     """
#     cls_path = getattr(settings, settings_key, None)
#     if not cls_path:
#         raise NotImplementedError()
# 
#     try:
#         # First check to see if it's an installed model
#         return get_model_from_settings(settings_key=settings_key)
#     except:
#         try:
#             # Next, check from installed apps
#             return get_class_from_settings_from_apps(settings_key=settings_key)
#         except:
#             # Last, try to load from the full path
#             return get_class_from_settings_full_path(settings_key)
# 
# Path: django_core/utils/loading.py
# def get_class_from_settings_from_apps(settings_key):
#     """Try and get a class from a settings path by lookin in installed apps.
#     """
#     cls_path = getattr(settings, settings_key, None)
# 
#     if not cls_path:
#         raise NotImplementedError()
# 
#     try:
#         app_label = cls_path.split('.')[-2]
#         model_name = cls_path.split('.')[-1]
#     except ValueError:
#         raise ImproperlyConfigured("{0} must be of the form "
#                                    "'app_label.model_name'".format(
#                                                                 settings_key))
# 
#     app = apps.get_app_config(app_label).models_module
# 
#     if not app:
#         raise ImproperlyConfigured("{0} setting refers to an app that has not "
#                                    "been installed".format(settings_key))
# 
#     return getattr(app, model_name)
# 
# Path: django_core/utils/loading.py
# def get_class_from_settings_full_path(settings_key):
#     """Get a class from it's full path.
# 
#     Example:
# 
#     some.path.module.MyClass
#     """
#     cls_path = getattr(settings, settings_key, None)
# 
#     if not cls_path:
#         raise NotImplementedError()
# 
#     try:
#         module_name, class_name = cls_path.rsplit('.', 1)
#     except ValueError:
#         raise ImproperlyConfigured("{0} must be of the form "
#                                    "'some.path.module.MyClass'".format(
#                                                                 settings_key))
# 
#     manager_module = importlib.import_module(module_name)
# 
#     if not manager_module:
#         raise ImproperlyConfigured("{0} refers to a module that has not been "
#                                    "installed".format(settings_key))
# 
#     return getattr(manager_module, class_name)
# 
# Path: django_core/utils/loading.py
# def get_function_from_settings(settings_key):
#     """Gets a function from the string path defined in a settings file.
# 
#     Example:
# 
#     # my_app/my_file.py
#     def some_function():
#         # do something
#         pass
# 
#     # settings.py
#     SOME_FUNCTION = 'my_app.my_file.some_function'
# 
#     > get_function_from_settings('SOME_FUNCTION')
#     <function my_app.my_file.some_function>
#     """
# 
#     renderer_func_str = getattr(settings, settings_key, None)
#     if not renderer_func_str:
#         return None
# 
#     module_str, renderer_func_name = renderer_func_str.rsplit('.', 1)
# 
#     try:
#         mod = importlib.import_module(module_str)
#         return getattr(mod, renderer_func_name)
#     except Exception:
#         return None
# 
# Path: django_core/utils/loading.py
# def get_model_from_settings(settings_key):
#     """Return the django model from a settings key.
# 
#     This is the same pattern user for django's "get_user_model()" method. To
#     allow you to set the model instance to a different model subclass.
# 
#     :param settings_key: the key defined in settings to the value for
# 
#     """
#     cls_path = getattr(settings, settings_key, None)
#     if not cls_path:
#         raise NotImplementedError()
# 
#     try:
#         app_label, model_name = cls_path.split('.')
#     except ValueError:
#         raise ImproperlyConfigured("{0} must be of the form "
#                                    "'app_label.model_name'".format(settings_key))
# 
#     model = apps.get_model(app_label, model_name)
# 
#     if model is None:
#         raise ImproperlyConfigured("{0} refers to model '%s' that has not "
#                                    "been installed".format(settings_key))
# 
#     return model


# Path: tests/test_loading.py
from django.test.testcases import TestCase
from django.test.utils import override_settings
from django_core.utils.loading import get_class_from_settings
from django_core.utils.loading import get_class_from_settings_from_apps
from django_core.utils.loading import get_class_from_settings_full_path
from django_core.utils.loading import get_function_from_settings
from django_core.utils.loading import get_model_from_settings
from test_objects.models import TestModel




class LoadingTestCase(TestCase):
    """TestCase for loading classes."""

    @override_settings(MY_MODEL_SETTING='test_objects.TestModel')
    def test_get_class_from_settings(self):
        model = get_class_from_settings(settings_key='MY_MODEL_SETTING')
        self.assertEqual(model, TestModel)

    @override_settings(MY_MODEL_SETTING='test_objects.TestModel')
    def test_get_model_from_settings(self):

        model = get_model_from_settings(settings_key='MY_MODEL_SETTING')