From d65b0f72de8d35617fe0554ddabc950c7f323eef Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 4 Feb 2013 14:35:52 +0100 Subject: Fixed #17379 -- Removed management commands deactivation of the locale. --- django/apps/registry.py | 5 ++ django/core/management/base.py | 70 +++++++++------------- django/core/management/commands/compilemessages.py | 1 - django/core/management/commands/makemessages.py | 1 - django/core/management/commands/makemigrations.py | 5 +- django/core/management/commands/migrate.py | 5 +- django/core/management/commands/runserver.py | 1 - django/core/management/templates.py | 3 - 8 files changed, 42 insertions(+), 49 deletions(-) (limited to 'django') diff --git a/django/apps/registry.py b/django/apps/registry.py index f522550d60..e01352b1de 100644 --- a/django/apps/registry.py +++ b/django/apps/registry.py @@ -124,6 +124,11 @@ class Apps: def check_apps_ready(self): """Raise an exception if all apps haven't been imported yet.""" if not self.apps_ready: + from django.conf import settings + # If "not ready" is due to unconfigured settings, accessing + # INSTALLED_APPS raises a more helpful ImproperlyConfigured + # exception. + settings.INSTALLED_APPS raise AppRegistryNotReady("Apps aren't loaded yet.") def check_models_ready(self): diff --git a/django/core/management/base.py b/django/core/management/base.py index 45e97c7398..d6b4e27b78 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -73,6 +73,21 @@ def handle_default_options(options): sys.path.insert(0, options.pythonpath) +def no_translations(handle_func): + """Decorator that forces a command to run with translations deactivated.""" + def wrapped(*args, **kwargs): + from django.utils import translation + saved_locale = translation.get_language() + translation.deactivate_all() + try: + res = handle_func(*args, **kwargs) + finally: + if saved_locale is not None: + translation.activate(saved_locale) + return res + return wrapped + + class OutputWrapper(TextIOBase): """ Wrapper around stdout/stderr @@ -171,19 +186,6 @@ class BaseCommand: is the list of application's configuration provided by the app registry. - ``leave_locale_alone`` - A boolean indicating whether the locale set in settings should be - preserved during the execution of the command instead of translations - being deactivated. - - Default value is ``False``. - - Make sure you know what you are doing if you decide to change the value - of this option in your custom command if it creates database content - that is locale-sensitive and such content shouldn't contain any - translations (like it happens e.g. with django.contrib.auth - permissions) as activating any locale might cause unintended effects. - ``stealth_options`` A tuple of any options the command uses which aren't defined by the argument parser. @@ -194,7 +196,6 @@ class BaseCommand: # Configuration shortcuts that alter various logic. _called_from_command_line = False output_transaction = False # Whether to wrap the output in a "BEGIN; COMMIT;" - leave_locale_alone = False requires_migrations_checks = False requires_system_checks = True # Arguments, common to all commands, which aren't defined by the argument @@ -323,33 +324,20 @@ class BaseCommand: if options.get('stderr'): self.stderr = OutputWrapper(options['stderr'], self.stderr.style_func) - saved_locale = None - if not self.leave_locale_alone: - # Deactivate translations, because django-admin creates database - # content like permissions, and those shouldn't contain any - # translations. - from django.utils import translation - saved_locale = translation.get_language() - translation.deactivate_all() - - try: - if self.requires_system_checks and not options.get('skip_checks'): - self.check() - if self.requires_migrations_checks: - self.check_migrations() - output = self.handle(*args, **options) - if output: - if self.output_transaction: - connection = connections[options.get('database', DEFAULT_DB_ALIAS)] - output = '%s\n%s\n%s' % ( - self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()), - output, - self.style.SQL_KEYWORD(connection.ops.end_transaction_sql()), - ) - self.stdout.write(output) - finally: - if saved_locale is not None: - translation.activate(saved_locale) + if self.requires_system_checks and not options.get('skip_checks'): + self.check() + if self.requires_migrations_checks: + self.check_migrations() + output = self.handle(*args, **options) + if output: + if self.output_transaction: + connection = connections[options.get('database', DEFAULT_DB_ALIAS)] + output = '%s\n%s\n%s' % ( + self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()), + output, + self.style.SQL_KEYWORD(connection.ops.end_transaction_sql()), + ) + self.stdout.write(output) return output def _run_checks(self, **kwargs): diff --git a/django/core/management/commands/compilemessages.py b/django/core/management/commands/compilemessages.py index fcbe24b1f3..bf704a4e8d 100644 --- a/django/core/management/commands/compilemessages.py +++ b/django/core/management/commands/compilemessages.py @@ -27,7 +27,6 @@ class Command(BaseCommand): help = 'Compiles .po files to .mo files for use with builtin gettext support.' requires_system_checks = False - leave_locale_alone = True program = 'msgfmt' program_options = ['--check-format'] diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index 4e47dd0687..a1e72c73ad 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -207,7 +207,6 @@ class Command(BaseCommand): build_file_class = BuildFile requires_system_checks = False - leave_locale_alone = True msgmerge_options = ['-q', '--previous'] msguniq_options = ['--to-code=utf-8'] diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py index 96b9b2adbc..14461418c1 100644 --- a/django/core/management/commands/makemigrations.py +++ b/django/core/management/commands/makemigrations.py @@ -4,7 +4,9 @@ from itertools import takewhile from django.apps import apps from django.conf import settings -from django.core.management.base import BaseCommand, CommandError +from django.core.management.base import ( + BaseCommand, CommandError, no_translations, +) from django.db import DEFAULT_DB_ALIAS, connections, router from django.db.migrations import Migration from django.db.migrations.autodetector import MigrationAutodetector @@ -51,6 +53,7 @@ class Command(BaseCommand): help='Exit with a non-zero status if model changes are missing migrations.', ) + @no_translations def handle(self, *app_labels, **options): self.verbosity = options['verbosity'] self.interactive = options['interactive'] diff --git a/django/core/management/commands/migrate.py b/django/core/management/commands/migrate.py index 7ca728fc97..0e27eaa19f 100644 --- a/django/core/management/commands/migrate.py +++ b/django/core/management/commands/migrate.py @@ -4,7 +4,9 @@ from importlib import import_module from django.apps import apps from django.core.checks import Tags, run_checks -from django.core.management.base import BaseCommand, CommandError +from django.core.management.base import ( + BaseCommand, CommandError, no_translations, +) from django.core.management.sql import ( emit_post_migrate_signal, emit_pre_migrate_signal, ) @@ -58,6 +60,7 @@ class Command(BaseCommand): issues.extend(super()._run_checks(**kwargs)) return issues + @no_translations def handle(self, *args, **options): self.verbosity = options['verbosity'] diff --git a/django/core/management/commands/runserver.py b/django/core/management/commands/runserver.py index d38491d0a9..0e0fd1ca2c 100644 --- a/django/core/management/commands/runserver.py +++ b/django/core/management/commands/runserver.py @@ -25,7 +25,6 @@ class Command(BaseCommand): # Validation is called explicitly each time the server is reloaded. requires_system_checks = False - leave_locale_alone = True stealth_options = ('shutdown_message',) default_addr = '127.0.0.1' diff --git a/django/core/management/templates.py b/django/core/management/templates.py index c55118260d..19da55f25b 100644 --- a/django/core/management/templates.py +++ b/django/core/management/templates.py @@ -32,9 +32,6 @@ class TemplateCommand(BaseCommand): requires_system_checks = False # The supported URL schemes url_schemes = ['http', 'https', 'ftp'] - # Can't perform any active locale changes during this command, because - # setting might not be available at all. - leave_locale_alone = True # Rewrite the following suffixes when determining the target filename. rewrite_template_suffixes = ( # Allow shipping invalid .py files without byte-compilation. -- cgit v1.3