def register_app(app_label, app): """Register a new app in the registry. This must be balanced with a :py:func:`unregister_app` call. Args: app_label (str): The label of the app. app (module): The app module. """ if apps: # Django >= 1.7 app_config = AppConfig(app.__name__, app) app_config.label = app_label app_config.models_module = app apps.set_installed_apps(settings.INSTALLED_APPS + [app_config]) else: # Django < 1.7 cache.app_store[app] = len(cache.app_store) if hasattr(cache, 'app_labels'): cache.app_labels[app_label] = app
def test_clear_template_tag_caches(self): """Testing clear_template_tag_caches""" def _check_state(enabled): if enabled: if get_templatetags_modules is not None: self.assertIn(templatetags_module_name, get_templatetags_modules()) self.assertEqual( Template(template_str).render(Context({})), 'Hello, world!') else: if get_templatetags_modules is not None: self.assertNotIn(templatetags_module_name, get_templatetags_modules()) with self.assertRaisesRegexp(TemplateSyntaxError, 'is not a (valid|registered) tag ' 'library'): Template(template_str).render(Context({})) templatetags_module_name = 'djblets.template.tests.templatetags' template_str = ( '{% load template_tests %}' '{% my_test_template_tag %}' ) # Sanity-check that the template tag module isn't registered. _check_state(enabled=False) # Enable a new template tag library. old_installed_apps = settings.INSTALLED_APPS settings.INSTALLED_APPS = list(old_installed_apps) + [ 'djblets.template.tests', ] if apps: apps.set_installed_apps(settings.INSTALLED_APPS) try: clear_template_tag_caches() _check_state(enabled=True) finally: settings.INSTALLED_APPS = old_installed_apps if apps: apps.unset_installed_apps() clear_template_tag_caches() _check_state(enabled=False) # Other libraries should still work. Template('{% load djblets_js djblets_extensions %}').render( Context({}))
def test_clear_template_tag_caches(self): """Testing clear_template_tag_caches""" def _check_state(enabled): if enabled: if get_templatetags_modules is not None: self.assertIn(templatetags_module_name, get_templatetags_modules()) self.assertEqual( Template(template_str).render(Context({})), 'Hello, world!') else: if get_templatetags_modules is not None: self.assertNotIn(templatetags_module_name, get_templatetags_modules()) with self.assertRaisesRegexp( TemplateSyntaxError, 'is not a (valid|registered) tag ' 'library'): Template(template_str).render(Context({})) templatetags_module_name = 'djblets.template.tests.templatetags' template_str = ('{% load template_tests %}' '{% my_test_template_tag %}') # Sanity-check that the template tag module isn't registered. _check_state(enabled=False) # Enable a new template tag library. old_installed_apps = settings.INSTALLED_APPS settings.INSTALLED_APPS = list(old_installed_apps) + [ 'djblets.template.tests', ] if apps: apps.set_installed_apps(settings.INSTALLED_APPS) try: clear_template_tag_caches() _check_state(enabled=True) finally: settings.INSTALLED_APPS = old_installed_apps if apps: apps.unset_installed_apps() clear_template_tag_caches() _check_state(enabled=False) # Other libraries should still work. Template('{% load djblets_js djblets_extensions %}').render(Context( {}))
def set(self, **kwargs): if not apps.app_configs: apps.populate(settings.INSTALLED_APPS) for k, v in kwargs.items(): self._original_settings.setdefault(k, getattr(settings, k, NO_SETTING)) setattr(settings, k, v) if 'INSTALLED_APPS' in kwargs: apps.set_installed_apps(kwargs['INSTALLED_APPS']) if kwargs.get('migrate', True): self.migrate()
def _add_to_installed_apps(self, extension): """Add an extension's apps to the list of installed apps. This will register each app with Django and clear any caches needed to load the extension's modules. Args: extension (djblets.extensions.extension.Extension): The extension whose apps are being added. """ self._installed_apps_setting.add_list( extension.apps or [extension.info.app_name]) if apps: apps.set_installed_apps(settings.INSTALLED_APPS)
def _add_to_installed_apps(self, extension): """Add an extension's apps to the list of installed apps. This will register each app with Django and clear any caches needed to load the extension's modules. Args: extension (djblets.extensions.extension.Extension): The extension whose apps are being added. """ self._installed_apps_setting.add_list(extension.apps or [extension.info.app_name]) if apps: apps.set_installed_apps(settings.INSTALLED_APPS)
def register_app(app_label, app): """Register a new app in the registry. This must be balanced with a :py:func:`unregister_app` call. Args: app_label (str): The label of the app. app (module): The app module. """ if apps: # Django >= 1.7 app_config = AppConfig(app.__name__, app) app_config.label = app_label apps.set_installed_apps(settings.INSTALLED_APPS + [app_config]) else: # Django < 1.7 cache.app_store[app] = len(cache.app_store) if hasattr(cache, 'app_labels'): cache.app_labels[app_label] = app