Python django.apps.AppConfig() Examples

The following are code examples for showing how to use django.apps.AppConfig(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: django-admin-search   Author: shinneider   File: app_test.py    MIT License 6 votes vote down vote up
def test_valid_subclass_appconfig(self):
        """
            This project use format major.minor.patch
        """
        self.assertEqual(issubclass(DjangoAdminSearchConfig, AppConfig), True) 
Example 2
Project: coolfig   Author: GaretJax   File: django.py    MIT License 6 votes vote down vote up
def get_app_settings_path(app_path):
    try:
        from django.apps import AppConfig
    except ImportError:
        # AppConfigs not supported on this django version
        pass
    else:
        if "." in app_path:
            # Check if this is a path to a Django AppConfig class
            try:
                app_config = types.dottedpath(app_path)
            except (ImportError, AttributeError):
                pass
            else:
                if isinstance(app_config, type) and issubclass(
                    app_config, AppConfig
                ):
                    try:
                        return app_config.settings
                    except AttributeError:
                        app_path = app_config.name

    return app_path + ".settings.AppSettings" 
Example 3
Project: django-north   Author: peopledoc   File: test_management_permissions.py    MIT License 6 votes vote down vote up
def test_get_all_contenttypes_for_app_config(mocker):
    model1 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel1'))
    model2 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel2'))
    ct1 = ContentType(app_label='myapp', model='mymodel1')
    ct2 = ContentType(app_label='myapp', model='mymodel2')
    app_config = CustomAppConfig('myapp', mocker.Mock())

    mocker.patch(
        'django.apps.AppConfig.get_models', return_value=[model1, model2])
    mock_get_for_model = mocker.patch(
        'django.contrib.contenttypes.models.ContentTypeManager.get')

    mock_get_for_model.side_effect = [ct1, ct2]
    result = permissions.get_all_contenttypes_for_app_config(app_config)

    assert result == [(ct1, model1), (ct2, model2)]

    mock_get_for_model.side_effect = [ct1, ContentType.DoesNotExist]
    result = permissions.get_all_contenttypes_for_app_config(app_config)

    assert result == [(ct1, model1)] 
Example 4
Project: django-conditioner   Author: omni-digital   File: test_apps.py    MIT License 5 votes vote down vote up
def test_conditioner_app_config(self):
        """Test 'licenses' module `AppConfig` instance"""
        conditioner_app_config = license_tracker_apps.get_app_config('conditioner')

        self.assertIsInstance(conditioner_app_config, AppConfig)
        self.assertEqual(conditioner_app_config.name, 'conditioner')
        self.assertEqual(conditioner_app_config.verbose_name, 'Conditioner') 
Example 5
Project: zulip   Author: zulip   File: apps.py    Apache License 2.0 5 votes vote down vote up
def flush_cache(sender: AppConfig, **kwargs: Any) -> None:
    logging.info("Clearing memcached cache after migrations")
    cache.clear() 
Example 6
Project: coolfig   Author: GaretJax   File: test_django.py    MIT License 5 votes vote down vote up
def test_load_appconfig():
    settings_class = make_django_settings({"INSTALLED_APPS": []})
    s = settings_class(DictConfig({"APP_KEY": "appconfig_val"}))

    # Load empty apps
    s.load_apps()
    s.load_apps([])

    class AppSettings(Settings):
        APP_KEY = Value(str)

    class AppConfig(DjangoAppConfig):
        name = "my_test_appconfig.module.submodule"

    with pytest.raises(AttributeError):
        s.APP_KEY

    install_module(
        "my_test_appconfig.module.submodule.apps", AppConfig=AppConfig
    )
    install_module(
        "my_test_appconfig.module.submodule.settings", AppSettings=AppSettings
    )
    s.load_apps(
        [
            "my_non_test_app",  # Does not exist
            "my_test_appconfig.module.submodule.apps.AppConfig",
        ]
    )

    assert s.APP_KEY == "appconfig_val" 
Example 7
Project: coolfig   Author: GaretJax   File: test_django.py    MIT License 5 votes vote down vote up
def test_load_appconfig_custom():
    settings_class = make_django_settings({"INSTALLED_APPS": []})
    s = settings_class(DictConfig({"APP_KEY": "appconfig_custom_val"}))

    # Load empty apps
    s.load_apps()
    s.load_apps([])

    class AppSettings(Settings):
        APP_KEY = Value(str)

    class AppConfig(DjangoAppConfig):
        name = "my_test_appconfig_custom.module.submodule"
        settings = "some_module.custom_settings.CustomSettings"

    with pytest.raises(AttributeError):
        s.APP_KEY

    install_module(
        "my_test_appconfig_custom.module.submodule.apps", AppConfig=AppConfig
    )
    install_module("some_module.custom_settings", CustomSettings=AppSettings)
    s.load_apps(
        [
            "my_non_test_app",  # Does not exist
            "my_test_appconfig_custom.module.submodule.apps.AppConfig",
        ]
    )

    assert s.APP_KEY == "appconfig_custom_val" 
Example 8
Project: airavata-django-portal   Author: apache   File: app_config.py    Apache License 2.0 5 votes vote down vote up
def get_url_app_name(app_config):
    """Return the urls namespace for the given AppConfig instance."""
    urls = get_app_urls(app_config)
    return getattr(urls, 'app_name', None) 
Example 9
Project: django-north   Author: peopledoc   File: test_management_contenttypes.py    MIT License 5 votes vote down vote up
def test_get_known_models_for_app_config(mocker):
    model1 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel1'))
    model2 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel2'))
    app_config = CustomAppConfig('myapp', mocker.Mock())

    mocker.patch(
        'django.apps.AppConfig.get_models', return_value=[model1, model2])

    result = contenttypes.get_known_models_for_app_config(app_config)

    assert result == {
        'mymodel1': model1,
        'mymodel2': model2,
    } 
Example 10
Project: fakester   Author: pawelad   File: test_apps.py    MIT License 5 votes vote down vote up
def test_redirects_app_config():
    """Test 'redirects' module `AppConfig` instance"""
    redirects_app_config = fakester_apps.get_app_config('redirects')

    assert isinstance(redirects_app_config, AppConfig)
    assert redirects_app_config.name == 'redirects'
    assert redirects_app_config.verbose_name == 'redirects'