Ticket #8033: better_error_handling_for_ugettext_r8377.diff

File better_error_handling_for_ugettext_r8377.diff, 3.9 KB (added by Manuel Saelices, 16 years ago)

Using a flag for no try translation while django apps is still loading

  • django/db/models/loading.py

     
    99import threading
    1010
    1111__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models',
    12         'load_app', 'app_cache_ready')
     12        'load_app', 'app_cache_ready', 'app_cache_loading')
    1313
    1414class AppCache(object):
    1515    """
     
    3030
    3131        # -- Everything below here is only used when populating the cache --
    3232        loaded = False,
     33        loading = False,
    3334        handled = {},
    3435        postponed = [],
    3536        nesting_level = 0,
     
    5152        try:
    5253            if self.loaded:
    5354                return
     55            self.loading = True
    5456            for app_name in settings.INSTALLED_APPS:
    5557                if app_name in self.handled:
    5658                    continue
     
    5961                for app_name in self.postponed:
    6062                    self.load_app(app_name)
    6163                self.loaded = True
     64                self.loading = False
    6265        finally:
    6366            self.write_lock.release()
    6467
     
    9295        """
    9396        return self.loaded
    9497
     98    def app_cache_loading(self):
     99        """
     100        Returns true if the model cache is now loading.
     101        """
     102        return self.loading
     103
     104
    95105    def get_apps(self):
    96106        "Returns a list of all installed modules that contain models."
    97107        self._populate()
     
    187197register_models = cache.register_models
    188198load_app = cache.load_app
    189199app_cache_ready = cache.app_cache_ready
     200app_cache_loading = cache.app_cache_loading
  • django/core/exceptions.py

     
    2828    "Django is somehow improperly configured"
    2929    pass
    3030
     31class ImproperlyImplemented(Exception):
     32    "Django is somehow improperly implemented"
     33    pass
     34
    3135class FieldError(Exception):
    3236    """Some kind of problem with a model field."""
    3337    pass
  • django/views/debug.py

     
    6060
    6161    def get_traceback_html(self):
    6262        "Return HTML code for traceback."
     63        from django.utils import translation
     64        translation.deactivate_all() # avoid possible translation errors in 500 error handling
    6365
    6466        if issubclass(self.exc_type, TemplateDoesNotExist):
    6567            from django.template.loader import template_source_loaders
  • django/utils/translation/trans_real.py

     
    77import gettext as gettext_module
    88from cStringIO import StringIO
    99
     10from django.core.exceptions import ImproperlyImplemented
     11from django.db.models.loading import app_cache_loading, get_apps
    1012from django.utils.safestring import mark_safe, SafeData
    1113from django.utils.thread_support import currentThread
    1214
     
    270272    """
    271273    global _default, _active
    272274    t = _active.get(currentThread(), None)
     275    if app_cache_loading() and \
     276       not isinstance(t, gettext_module.NullTranslations):
     277        raise ImproperlyImplemented(
     278            "django.utils.translation.%(func_name)s function called for "
     279            "translation of '%(message)s' when Django apps is still loading."
     280            "%(func_name)s function must be called after "
     281            "applications loading." % {'func_name': translation_function,
     282                                       'message': message})
    273283    if t is not None:
    274284        result = getattr(t, translation_function)(message)
    275285    else:
Back to Top