def clean_publish_dt(self): date = self.cleaned_data['publish_dt'] if date: date = localize_date(date, from_tz=self.timezone, to_tz=settings.TIME_ZONE) return date
def __init__(self, *args, **kwargs): # checking for user argument here for a more # localized form self.user = None self.timezone = None if 'user' in kwargs: self.user = kwargs.pop('user', None) super(PostForm, self).__init__(*args, **kwargs) # add span class to charfields for field in self.fields.values(): if isinstance(field, forms.fields.CharField): if 'class' in field.widget.attrs: field.widget.attrs['class'] = "%s %s" % ( field.widget.attrs['class'], 'span8', ) else: field.widget.attrs['class'] = 'span8' # disable publish_dt if draft if not self.instance.status or self.instance.status == 'draft': self.fields['publish_dt'].widget.attrs['disabled'] = 'disabled' # get the published dt publish_dt = self.instance.publish_dt if not publish_dt: publish_dt = datetime.now() # adjust the date/time to the users preference if self.user and not self.user.is_anonymous(): user_settings = Setting.objects.get(user=self.user) self.timezone = user_settings.timezone publish_dt = localize_date(publish_dt, to_tz=self.timezone) else: self.timezone = 'US/Central' publish_dt = localize_date(publish_dt, to_tz=self.timezone) self.initial['publish_dt'] = publish_dt
def localize(dt, user): from_tz = settings.TIME_ZONE to_tz = "US/Central" if not dt: return None # get the users timezone if not user.is_anonymous(): user_settings = Setting.objects.get(user=user) to_tz = user_settings.timezone return localize_date(dt, from_tz=from_tz, to_tz=to_tz)