def clean(self, value): value = super(LocalizedDateTimeField, self).clean(value) if value is None: # field was likely not required return None if not hasattr(self.timezone, 'localize'): self.timezone = pytz.timezone(smart_str(get_timezone())) value = self.timezone.localize(value) return value
def get_timezone_for_instance(self, instance): timezoneish = self.timezone # 1. Callable? if callable(timezoneish): timezoneish = timezoneish(instance) # 2. Attribute? if isinstance(timezoneish, basestring) and hasattr(instance, timezoneish): timezoneish = getattr(instance, timezoneish) # Callable (i.e. object method) ? if callable(timezoneish): timezoneish = timezoneish() # 3. At this point, timezoneish should be a string or a tzinfo-object return get_timezone(timezoneish, default_tz)
def __init__(self, timezone=None, *args, **kwargs): super(LocalizedDateTimeField, self).__init__(*args, **kwargs) self.timezone = timezone or get_timezone()
from django.conf import settings from django.db import models from django.db.models import signals from django.utils.encoding import smart_unicode, smart_str import pytz from timezones import zones from timezones.utils import coerce_timezone_value, validate_timezone_max_length, get_timezone MAX_TIMEZONE_LENGTH = getattr(settings, "MAX_TIMEZONE_LENGTH", 100) default_tz = pytz.timezone(getattr(settings, "TIME_ZONE", "UTC")) INTERNAL_TZ = get_timezone(getattr(settings, 'TIMEZONES_INTERNAL_TZ', pytz.utc)) class TimeZoneField(models.CharField): __metaclass__ = models.SubfieldBase def __init__(self, *args, **kwargs): validate_timezone_max_length(MAX_TIMEZONE_LENGTH, zones.ALL_TIMEZONE_CHOICES) defaults = { "max_length": MAX_TIMEZONE_LENGTH, "default": settings.TIME_ZONE, "choices": zones.PRETTY_TIMEZONE_CHOICES }