Пример #1
0
 def _add_installed_apps_translations(self):
     """Merge translations from each installed app."""
     try:
         app_configs = reversed(list(apps.get_app_configs()))
     except AppRegistryNotReady:
         raise AppRegistryNotReady(
             "The translation infrastructure cannot be initialized before the "
             "apps registry is ready. Check that you don't make non-lazy "
             "gettext calls at import time.")
     for app_config in app_configs:
         localedir = os.path.join(app_config.path, 'locale')
         if os.path.exists(localedir):
             translation = self._new_gnu_trans(localedir)
             self.merge(translation)
Пример #2
0
def get_app_name(model):
    if model._meta.installed:
        return get_app_label(model), get_model_name(model)

    raise AppRegistryNotReady(
        _("%(model)s is not installed or missing from the app registry.")
        % {
            "model": (
                getattr(
                    model._meta,
                    "app_label",
                    model.__class__.__name__,
                )
            )
        }
    )
Пример #3
0
    def set_installed_apps(self, installed):
        """
        Enable a different set of installed apps for get_app_config[s].

        installed must be an iterable in the same format as INSTALLED_APPS.

        set_installed_apps() must be balanced with unset_installed_apps(),
        even if it exits with an exception.

        Primarily used as a receiver of the setting_changed signal in tests.

        This method may trigger new imports, which may add new models to the
        registry of all imported models. They will stay in the registry even
        after unset_installed_apps(). Since it isn't possible to replay
        imports safely (e.g. that could lead to registering listeners twice),
        models are registered when they're imported and never removed.
        """
        if not self.ready:
            raise AppRegistryNotReady("App registry isn't ready yet.")
Пример #4
0
    def _add_installed_apps_translations_new(self):
        """Merge translations from each installed app."""
        try:
            non_mayan_app_configs = [
                app for app in apps.get_app_configs()
                if not app.name.startswith('mayan.')
            ]

            mayan_app_configs = [
                app for app in apps.get_app_configs()
                if app.name.startswith('mayan.')
            ]
            mayan_app_configs.extend(non_mayan_app_configs)
            app_configs = mayan_app_configs
        except AppRegistryNotReady:
            raise AppRegistryNotReady(
                "The translation infrastructure cannot be initialized before the "
                "apps registry is ready. Check that you don't make non-lazy "
                "gettext calls at import time.")
        for app_config in app_configs:
            localedir = os.path.join(app_config.path, 'locale')
            if os.path.exists(localedir):
                translation = self._new_gnu_trans(localedir)
                self.merge(translation)
Пример #5
0
 def check_models_ready(self):
     """
     Raises an exception if all models haven't been imported yet.
     """
     if not self.models_ready:
         raise AppRegistryNotReady("Models aren't loaded yet.")
Пример #6
0
 def check_apps_ready(self):
     """
     Raises an exception if all apps haven't been imported yet.
     """
     if not self.apps_ready:
         raise AppRegistryNotReady("Apps aren't loaded yet.")
Пример #7
0
    def connect(self,
                receiver,
                sender=None,
                fields=None,
                dispatch_uid=None,
                **kwargs):
        """
        Connect a FieldSignal. Usage::

            foo.connect(func, sender=MyModel, fields=['myfield1', 'myfield2'])
        """

        if not apps.models_ready:
            # We require access to Model._meta.get_fields(), which isn't available yet.
            # (This error would be raised below anyway, but we want to add a more meaningful message)
            raise AppRegistryNotReady(
                "django-fieldsignals signals must be connected after the app cache is ready. "
                "Connect the signal in your AppConfig.ready() handler.")

        # Validate arguments

        if kwargs.get('weak', False):
            # TODO: weak refs? I'm hella confused.
            # We can't go passing our proxy receivers around as weak refs, since they're
            # defined as closures and hence don't exist by the time they're called.
            # However, we can probably make _make_proxy_receiver() create weakrefs to
            # the original receiver if required. Patches welcome
            raise NotImplementedError(
                "This signal doesn't yet handle weak refs")

        # Check it's a class, don't check if it's a model class (useful for tests)
        if not isinstance(sender, type):
            raise ValueError("sender should be a model class")

        def is_reverse_rel(f):
            return f.many_to_many or f.one_to_many or isinstance(
                f, ForeignObjectRel)

        if fields is None:
            fields = sender._meta.get_fields()
            fields = [f for f in fields if not is_reverse_rel(f)]
        else:
            fields = [
                f for f in sender._meta.get_fields() if f.name in set(fields)
            ]
            for f in fields:
                if is_reverse_rel(f):
                    raise ValueError(
                        "django-fieldsignals doesn't handle reverse related fields "
                        "({f.name} is a {f.__class__.__name__})".format(f=f))

        if not fields:
            raise ValueError("fields must be non-empty")

        proxy_receiver = self._make_proxy_receiver(receiver, sender, fields)

        super(ChangedSignal, self).connect(proxy_receiver,
                                           sender=sender,
                                           weak=False,
                                           dispatch_uid=dispatch_uid)

        ### post_init : initialize the list of fields for each instance
        def post_init_closure(sender, instance, **kwargs):
            self.get_and_update_changed_fields(receiver, instance, fields)

        _signals.post_init.connect(post_init_closure,
                                   sender=sender,
                                   weak=False,
                                   dispatch_uid=(self, receiver))
        self.connect_source_signals(sender)
Пример #8
0
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
        localedir = os.path.join(os.path.dirname(settingsfile), 'locale')
        translation = self._new_gnu_trans(localedir)
        self.merge(translation)

    def _add_installed_apps_translations(self):
<<<<<<< HEAD
        """Merges translations from each installed app."""
=======
        """Merge translations from each installed app."""
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
        try:
            app_configs = reversed(list(apps.get_app_configs()))
        except AppRegistryNotReady:
            raise AppRegistryNotReady(
                "The translation infrastructure cannot be initialized before the "
                "apps registry is ready. Check that you don't make non-lazy "
                "gettext calls at import time.")
        for app_config in app_configs:
            localedir = os.path.join(app_config.path, 'locale')
            if os.path.exists(localedir):
                translation = self._new_gnu_trans(localedir)
                self.merge(translation)

    def _add_local_translations(self):
<<<<<<< HEAD
        """Merges translations defined in LOCALE_PATHS."""
=======
        """Merge translations defined in LOCALE_PATHS."""
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
        for localedir in reversed(settings.LOCALE_PATHS):
            translation = self._new_gnu_trans(localedir)
Пример #9
0
 def check_ready(self):
     """
     Raises an exception if the registry isn't ready.
     """
     if not self.ready:
         raise AppRegistryNotReady()
Пример #10
0
 def check_models_ready(self):
     """
     Raises an exception if all models haven't been imported yet.
     """
     if not self.models_ready:
         raise AppRegistryNotReady("Models aren't loaded yet. Ensure django.setup() has been called.")
Пример #11
0
    def _fetch(lang, fallback=None):

        global _translations

        res = _translations.get(lang, None)
        if res is not None:
            return res

        loc = to_locale(lang)

        def _translation(path):
            try:
                t = gettext_module.translation('django', path, [loc],
                                               DjangoTranslation)
                t.set_language(lang)
                return t
            except IOError:
                return None

        res = _translation(globalpath)

        # We want to ensure that, for example,  "en-gb" and "en-us" don't share
        # the same translation object (thus, merging en-us with a local update
        # doesn't affect en-gb), even though they will both use the core "en"
        # translation. So we have to subvert Python's internal gettext caching.
        base_lang = lambda x: x.split('-', 1)[0]
        if base_lang(lang) in [
                base_lang(trans) for trans in list(_translations)
        ]:
            res._info = res._info.copy()
            res._catalog = res._catalog.copy()

        def _merge(path):
            t = _translation(path)
            if t is not None:
                if res is None:
                    return t
                else:
                    res.merge(t)
            return res

        try:
            app_configs = reversed(list(apps.get_app_configs()))
        except AppRegistryNotReady:
            raise AppRegistryNotReady(
                "The translation infrastructure cannot be initialized before the "
                "apps registry is ready. Check that you don't make non-lazy "
                "gettext calls at import time.")
        for app_config in app_configs:
            apppath = os.path.join(app_config.path, 'locale')
            if os.path.isdir(apppath):
                res = _merge(apppath)

        for localepath in reversed(settings.LOCALE_PATHS):
            if os.path.isdir(localepath):
                res = _merge(localepath)

        if res is None:
            if fallback is not None:
                res = fallback
            else:
                return gettext_module.NullTranslations()
        _translations[lang] = res
        return res
Пример #12
0
 def check_models_ready(self):
     if not self.models_ready:
         raise AppRegistryNotReady("Models aren't loaded yet.")
Пример #13
0
 def check_apps_ready(self):
     if not self.apps_ready:
         from django.conf import settings
         settings.INSTALLED_APPS
         raise AppRegistryNotReady("Apps aren't loaded yet.")