diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-04-29 16:03:46 +0200 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2012-04-29 16:03:46 +0200 |
| commit | a15cfb2e451feb626d141461eb058ff760bed913 (patch) | |
| tree | 296763bd793ef0f3f331f3ad786bd757c6b6cf16 /django | |
| parent | 3e8b40f479a02e0f8c40ef3c7dae740082478b89 (diff) | |
Simplified timezones tests with settings_changed.
All relevant state is now properly reset whenever TIME_ZONE or USE_TZ
are changed in tests.
Diffstat (limited to 'django')
| -rw-r--r-- | django/test/signals.py | 24 | ||||
| -rw-r--r-- | django/utils/timezone.py | 1 |
2 files changed, 22 insertions, 3 deletions
diff --git a/django/test/signals.py b/django/test/signals.py index ed16831d42..81808dfa3c 100644 --- a/django/test/signals.py +++ b/django/test/signals.py @@ -1,27 +1,47 @@ +import os +import time + from django.conf import settings from django.db import connections from django.dispatch import receiver, Signal from django.template import context +from django.utils import timezone template_rendered = Signal(providing_args=["template", "context"]) setting_changed = Signal(providing_args=["setting", "value"]) + @receiver(setting_changed) def update_connections_time_zone(**kwargs): + if kwargs['setting'] == 'TIME_ZONE': + # Reset process time zone + if hasattr(time, 'tzset'): + if kwargs['value']: + os.environ['TZ'] = kwargs['value'] + else: + os.environ.pop('TZ', None) + time.tzset() + + # Reset local time zone cache + timezone._localtime = None + + # Reset the database connections' time zone if kwargs['setting'] == 'USE_TZ' and settings.TIME_ZONE != 'UTC': USE_TZ, TIME_ZONE = kwargs['value'], settings.TIME_ZONE elif kwargs['setting'] == 'TIME_ZONE' and not settings.USE_TZ: USE_TZ, TIME_ZONE = settings.USE_TZ, kwargs['value'] - else: # no need to change the database connnections' time zones + else: + # no need to change the database connnections' time zones return - tz = 'UTC' if USE_TZ else TIME_ZONE for conn in connections.all(): + conn.settings_dict['TIME_ZONE'] = tz tz_sql = conn.ops.set_time_zone_sql() if tz_sql: conn.cursor().execute(tz_sql, [tz]) + @receiver(setting_changed) def clear_context_processors_cache(**kwargs): if kwargs['setting'] == 'TEMPLATE_CONTEXT_PROCESSORS': diff --git a/django/utils/timezone.py b/django/utils/timezone.py index 0bba97e736..d9d9636023 100644 --- a/django/utils/timezone.py +++ b/django/utils/timezone.py @@ -95,7 +95,6 @@ utc = pytz.utc if pytz else UTC() # In order to avoid accessing the settings at compile time, # wrap the expression in a function and cache the result. -# If you change settings.TIME_ZONE in tests, reset _localtime to None. _localtime = None def get_default_timezone(): |
