Example #1
0
    def populate_apps(self, installed_apps=None):
        """
        Populate app-related information.

        This method imports each application module.

        It is thread safe and idempotent, but not reentrant.
        """
        if self._apps_loaded:
            return
        # Since populate_apps() may be a side effect of imports, and since
        # it will itself import modules, an ABBA deadlock between threads
        # would be possible if we didn't take the import lock. See #18251.
        with import_lock():
            if self._apps_loaded:
                return

            # app_config should be pristine, otherwise the code below won't
            # guarantee that the order matches the order in INSTALLED_APPS.
            if self.app_configs:
                raise RuntimeError("populate_apps() isn't reentrant")

            # Application modules aren't expected to import anything, and
            # especially not other application modules, even indirectly.
            # Therefore we simply import them sequentially.
            if installed_apps is None:
                installed_apps = settings.INSTALLED_APPS
            for app_name in installed_apps:
                app_config = AppConfig.create(app_name)
                self.app_configs[app_config.label] = app_config

            self.get_models.cache_clear()
            self._apps_loaded = True
Example #2
0
    def populate(self, installed_apps=None):
        """
        Loads application configurations and models.

        This method imports each application module and then each model module.

        It is thread safe and idempotent, but not reentrant.
        """
        if self.ready:
            return
        # Since populate() may be a side effect of imports, and since it will
        # itself import modules, an ABBA deadlock between threads would be
        # possible if we didn't take the import lock. See #18251.
        with import_lock():
            if self.ready:
                return

            # app_config should be pristine, otherwise the code below won't
            # guarantee that the order matches the order in INSTALLED_APPS.
            if self.app_configs:
                raise RuntimeError("populate() isn't reentrant")

            # Load app configs and app modules.
            for entry in installed_apps:
                if isinstance(entry, AppConfig):
                    app_config = entry
                else:
                    app_config = AppConfig.create(entry)
                if app_config.label in self.app_configs:
                    raise ImproperlyConfigured(
                        "Application labels aren't unique, "
                        "duplicates: %s" % app_config.label)

                self.app_configs[app_config.label] = app_config

            # Check for duplicate app names.
            counts = Counter(app_config.name
                             for app_config in self.app_configs.values())
            duplicates = [
                name for name, count in counts.most_common() if count > 1
            ]
            if duplicates:
                raise ImproperlyConfigured("Application names aren't unique, "
                                           "duplicates: %s" %
                                           ", ".join(duplicates))

            # Load models.
            for app_config in self.app_configs.values():
                all_models = self.all_models[app_config.label]
                app_config.import_models(all_models)

            self.clear_cache()
            self.ready = True

            for app_config in self.get_app_configs():
                app_config.ready()
Example #3
0
    def populate(self, installed_apps=None):
        """
        Loads application configurations and models.

        This method imports each application module and then each model module.

        It is thread safe and idempotent, but not reentrant.
        """
        if self.ready:
            return
        # Since populate() may be a side effect of imports, and since it will
        # itself import modules, an ABBA deadlock between threads would be
        # possible if we didn't take the import lock. See #18251.
        with import_lock():
            if self.ready:
                return

            # app_config should be pristine, otherwise the code below won't
            # guarantee that the order matches the order in INSTALLED_APPS.
            if self.app_configs:
                raise RuntimeError("populate() isn't reentrant")

            # Load app configs and app modules.
            for entry in installed_apps:
                if isinstance(entry, AppConfig):
                    app_config = entry
                else:
                    app_config = AppConfig.create(entry)
                if app_config.label in self.app_configs:
                    raise ImproperlyConfigured(
                        "Application labels aren't unique, "
                        "duplicates: %s" % app_config.label)

                self.app_configs[app_config.label] = app_config

            # Check for duplicate app names.
            counts = Counter(
                app_config.name for app_config in self.app_configs.values())
            duplicates = [
                name for name, count in counts.most_common() if count > 1]
            if duplicates:
                raise ImproperlyConfigured(
                    "Application names aren't unique, "
                    "duplicates: %s" % ", ".join(duplicates))

            # Load models.
            for app_config in self.app_configs.values():
                all_models = self.all_models[app_config.label]
                app_config.import_models(all_models)

            self.clear_cache()
            self.ready = True

            for app_config in self.get_app_configs():
                app_config.ready()
Example #4
0
    def populate_models(self):
        """
        Populate model-related information.

        This method imports each models module.

        It is thread safe, idempotent and reentrant.
        """
        if self._models_loaded:
            return
        # Since populate_models() may be a side effect of imports, and since
        # it will itself import modules, an ABBA deadlock between threads
        # would be possible if we didn't take the import lock. See #18251.
        with import_lock():
            if self._models_loaded:
                return

            self.populate_apps()

            # Models modules are likely to import other models modules, for
            # example to reference related objects. As a consequence:
            # - we deal with import loops by postponing affected modules.
            # - we provide reentrancy by making import_models() idempotent.

            outermost = not hasattr(self, '_postponed')
            if outermost:
                self._postponed = []

            for app_config in self.app_configs.values():

                if app_config.models is not None:
                    continue

                try:
                    all_models = self.all_models[app_config.label]
                    app_config.import_models(all_models)
                except ImportError:
                    self._postponed.append(app_config)

            if outermost:
                for app_config in self._postponed:
                    all_models = self.all_models[app_config.label]
                    app_config.import_models(all_models)

                del self._postponed

                self.get_models.cache_clear()
                self._models_loaded = True
Example #5
0
    def populate(self, installed_apps=None):
        """
        Loads application configurations and models.

        This method imports each application module and then each model module.

        It is thread safe and idempotent, but not reentrant.
        """
        if self.ready:
            return
        # Since populate() may be a side effect of imports, and since it will
        # itself import modules, an ABBA deadlock between threads would be
        # possible if we didn't take the import lock. See #18251.
        with import_lock():
            if self.ready:
                return

            # app_config should be pristine, otherwise the code below won't
            # guarantee that the order matches the order in INSTALLED_APPS.
            if self.app_configs:
                raise RuntimeError("populate() isn't reentrant")

            # Load app configs and app modules.
            for entry in installed_apps:
                if isinstance(entry, AppConfig):
                    app_config = entry
                else:
                    app_config = AppConfig.create(entry)
                self.app_configs[app_config.label] = app_config

            # Load models.
            for app_config in self.app_configs.values():
                all_models = self.all_models[app_config.label]
                app_config.import_models(all_models)

            self.clear_cache()
            self.ready = True

            for app_config in self.get_app_configs():
                app_config.setup()