2012-04-29 22:03:46 +08:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
2012-03-13 14:59:04 +08:00
|
|
|
from django.conf import settings
|
2012-03-14 07:29:15 +08:00
|
|
|
from django.db import connections
|
2012-04-09 21:24:57 +08:00
|
|
|
from django.dispatch import receiver, Signal
|
2012-04-29 22:03:46 +08:00
|
|
|
from django.utils import timezone
|
2012-10-28 04:38:15 +08:00
|
|
|
from django.utils.functional import empty
|
2008-08-06 23:32:46 +08:00
|
|
|
|
|
|
|
template_rendered = Signal(providing_args=["template", "context"])
|
2011-05-18 20:08:53 +08:00
|
|
|
|
|
|
|
setting_changed = Signal(providing_args=["setting", "value"])
|
2012-03-13 14:59:04 +08:00
|
|
|
|
2012-07-21 19:49:07 +08:00
|
|
|
# Most setting_changed receivers are supposed to be added below,
|
|
|
|
# except for cases where the receiver is related to a contrib app.
|
|
|
|
|
2012-04-29 22:03:46 +08:00
|
|
|
|
2012-04-09 21:24:57 +08:00
|
|
|
@receiver(setting_changed)
|
2012-03-14 07:29:15 +08:00
|
|
|
def update_connections_time_zone(**kwargs):
|
2012-04-29 22:03:46 +08:00
|
|
|
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
|
2012-03-14 07:29:15 +08:00
|
|
|
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']
|
2012-04-29 22:03:46 +08:00
|
|
|
else:
|
|
|
|
# no need to change the database connnections' time zones
|
2012-03-14 07:29:15 +08:00
|
|
|
return
|
|
|
|
tz = 'UTC' if USE_TZ else TIME_ZONE
|
|
|
|
for conn in connections.all():
|
2012-04-29 22:03:46 +08:00
|
|
|
conn.settings_dict['TIME_ZONE'] = tz
|
2012-03-14 07:29:15 +08:00
|
|
|
tz_sql = conn.ops.set_time_zone_sql()
|
|
|
|
if tz_sql:
|
|
|
|
conn.cursor().execute(tz_sql, [tz])
|
|
|
|
|
2012-04-29 22:03:46 +08:00
|
|
|
|
2012-04-09 21:24:57 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def clear_context_processors_cache(**kwargs):
|
|
|
|
if kwargs['setting'] == 'TEMPLATE_CONTEXT_PROCESSORS':
|
2012-08-22 03:52:25 +08:00
|
|
|
from django.template import context
|
2012-04-09 21:24:57 +08:00
|
|
|
context._standard_context_processors = None
|
2012-07-21 19:49:07 +08:00
|
|
|
|
|
|
|
|
2012-09-16 03:39:08 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def clear_template_loaders_cache(**kwargs):
|
|
|
|
if kwargs['setting'] == 'TEMPLATE_LOADERS':
|
|
|
|
from django.template import loader
|
|
|
|
loader.template_source_loaders = None
|
|
|
|
|
|
|
|
|
2012-08-22 03:52:25 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def clear_serializers_cache(**kwargs):
|
|
|
|
if kwargs['setting'] == 'SERIALIZATION_MODULES':
|
|
|
|
from django.core import serializers
|
|
|
|
serializers._serializers = {}
|
|
|
|
|
|
|
|
|
2012-07-21 19:49:07 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def language_changed(**kwargs):
|
|
|
|
if kwargs['setting'] in ('LOCALE_PATHS', 'LANGUAGE_CODE'):
|
|
|
|
from django.utils.translation import trans_real
|
|
|
|
trans_real._default = None
|
|
|
|
if kwargs['setting'] == 'LOCALE_PATHS':
|
|
|
|
trans_real._translations = {}
|
2012-10-28 04:38:15 +08:00
|
|
|
|
|
|
|
@receiver(setting_changed)
|
|
|
|
def file_storage_changed(**kwargs):
|
|
|
|
if kwargs['setting'] in ('MEDIA_ROOT', 'DEFAULT_FILE_STORAGE'):
|
|
|
|
from django.core.files.storage import default_storage
|
|
|
|
default_storage._wrapped = empty
|