Python django.apps.AppConfig() Examples

The following are 16 code examples for showing how to use django.apps.AppConfig(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.apps , or try the search function .

Example 1
Project: omniport-backend   Author: IMGIITRoorkee   File: app_config_class.py    License: GNU General Public License v3.0 7 votes vote down vote up
def get_app_config_class(path):
    """
    Return a subclass of AppConfig to configure the app
    :param path: the path to the apps.py file, can be accessed as __file__
    :return: a subclass of AppConfig that configures the given app
    """

    file_path = os.path.abspath(path)
    app_directory = os.path.dirname(file_path)
    app_directory_name = os.path.basename(app_directory)
    conf = settings.DISCOVERY.get_app_configuration(app_directory_name)

    class Config(AppConfig):
        name = app_directory_name
        label = conf.nomenclature.name
        verbose_name = conf.nomenclature.verbose_name
        configuration = conf

    return Config 
Example 2
Project: cleanerversion   Author: swisscom   File: apps.py    License: Apache License 2.0 6 votes vote down vote up
def index_adjustments(sender, using=None, **kwargs):
    """
    Remove -like indexes (varchar_pattern_ops) on UUID fields and create
    version-unique indexes for models that have a VERSION_UNIQUE attribute.
    :param AppConfig sender:
    :param str sender: database alias
    :param kwargs:
    """
    from versions.util.postgresql import (
        remove_uuid_id_like_indexes,
        create_current_version_unique_indexes,
        create_current_version_unique_identity_indexes
    )
    remove_uuid_id_like_indexes(sender.name, using)
    create_current_version_unique_indexes(sender.name, using)
    create_current_version_unique_identity_indexes(sender.name, using) 
Example 3
Project: django-postgres-extra   Author: SectorLabs   File: fake_model.py    License: MIT License 6 votes vote down vote up
def define_fake_app():
    """Creates and registers a fake Django app."""

    name = str(uuid.uuid4()).replace("-", "")[:8] + "-app"

    app_config_cls = type(
        name + "Config",
        (AppConfig,),
        {"name": name, "path": os.path.dirname(__file__)},
    )

    app_config = app_config_cls(name, "")
    app_config.apps = apps
    app_config.models = {}

    apps.app_configs[name] = app_config
    sys.modules[name] = {}

    try:
        yield app_config
    finally:
        del apps.app_configs[name]
        del sys.modules[name] 
Example 4
Project: django-micro   Author: zenwalker   File: django_micro.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _create_app(stack):
    parent_module = sys.modules[stack[1][0].f_locals['__name__']]
    parent_module_dir = os.path.dirname(os.path.abspath(parent_module.__file__))

    # use parent directory of application as import root
    sys.path[0] = os.path.dirname(parent_module_dir)

    app_module = os.path.basename(parent_module_dir)
    entrypoint = '{}.{}'.format(app_module, os.path.basename(parent_module.__file__).split('.')[0])

    # allow relative import from app.py
    parent_module.__package__ = app_module
    import_module(app_module)

    # allow recursive import app.py
    if parent_module.__name__ != app_module:
        sys.modules[entrypoint] = parent_module

    class MicroAppConfig(AppConfig):
        module = app_module
        label = app_module
        name = app_module

        def import_models(self):
            super().import_models()
            if self.models_module is None:
                self.models_module = parent_module

    globals().update(
        _app_config=MicroAppConfig,
        _parent_module=parent_module,
    ) 
Example 5
Project: zulip   Author: zulip   File: apps.py    License: 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: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_an_app_config(self):
        """
        Tests when INSTALLED_APPS contains a class that isn't an app config.
        """
        msg = "'apps.apps.NotAConfig' isn't a subclass of AppConfig."
        with self.assertRaisesMessage(ImproperlyConfigured, msg):
            with self.settings(INSTALLED_APPS=['apps.apps.NotAConfig']):
                pass 
Example 7
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_path_set_explicitly(self):
        """If subclass sets path as class attr, no module attributes needed."""
        class MyAppConfig(AppConfig):
            path = 'foo'

        ac = MyAppConfig('label', Stub())

        self.assertEqual(ac.path, 'foo') 
Example 8
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_explicit_path_overrides(self):
        """If path set as class attr, overrides __path__ and __file__."""
        class MyAppConfig(AppConfig):
            path = 'foo'

        ac = MyAppConfig('label', Stub(__path__=['a'], __file__='b/__init__.py'))

        self.assertEqual(ac.path, 'foo') 
Example 9
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_dunder_path(self):
        """If single element in __path__, use it (in preference to __file__)."""
        ac = AppConfig('label', Stub(__path__=['a'], __file__='b/__init__.py'))

        self.assertEqual(ac.path, 'a') 
Example 10
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_no_dunder_path_fallback_to_dunder_file(self):
        """If there is no __path__ attr, use __file__."""
        ac = AppConfig('label', Stub(__file__='b/__init__.py'))

        self.assertEqual(ac.path, 'b') 
Example 11
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_multiple_dunder_path_fallback_to_dunder_file(self):
        """If the __path__ attr is length>1, use __file__ if set."""
        ac = AppConfig('label', Stub(__path__=['a', 'b'], __file__='c/__init__.py'))

        self.assertEqual(ac.path, 'c') 
Example 12
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_no_dunder_path_or_dunder_file(self):
        """If there is no __path__ or __file__, raise ImproperlyConfigured."""
        with self.assertRaises(ImproperlyConfigured):
            AppConfig('label', Stub()) 
Example 13
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_empty_dunder_path_no_dunder_file(self):
        """If the __path__ attr is empty and there is no __file__, raise."""
        with self.assertRaises(ImproperlyConfigured):
            AppConfig('label', Stub(__path__=[])) 
Example 14
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_multiple_dunder_path_no_dunder_file(self):
        """If the __path__ attr is length>1 and there is no __file__, raise."""
        with self.assertRaises(ImproperlyConfigured):
            AppConfig('label', Stub(__path__=['a', 'b'])) 
Example 15
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_duplicate_dunder_path_no_dunder_file(self):
        """
        If the __path__ attr contains duplicate paths and there is no
        __file__, they duplicates should be deduplicated (#25246).
        """
        ac = AppConfig('label', Stub(__path__=['a', 'a']))
        self.assertEqual(ac.path, 'a') 
Example 16
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_check_for_langauge(self):
        with tempfile.TemporaryDirectory() as app_dir:
            os.makedirs(os.path.join(app_dir, 'locale', 'dummy_Lang', 'LC_MESSAGES'))
            open(os.path.join(app_dir, 'locale', 'dummy_Lang', 'LC_MESSAGES', 'django.mo'), 'w').close()
            app_config = AppConfig('dummy_app', AppModuleStub(__path__=[app_dir]))
            with mock.patch('django.apps.apps.get_app_configs', return_value=[app_config]):
                self.assertIs(check_for_language('dummy-lang'), True)