Permalink
Browse files

fields.py/prep_localized_datetime(): Fix scoping issue.

  • Loading branch information...
1 parent 2b903a3 commit 0ccee7e54214d5ab9960c7c330b978dff0d1a86f @dgoldenichec committed Jul 19, 2010
Showing with 44 additions and 36 deletions.
  1. +44 −36 timezones/fields.py
View
@@ -110,47 +110,55 @@ def get_db_prep_lookup(self, lookup_type, value):
def prep_localized_datetime(sender, **kwargs):
for field in sender._meta.fields:
if not isinstance(field, LocalizedDateTimeField) or field.timezone is None:
+ field = None
continue
- dt_field_name = "_datetimezone_%s" % field.attname
- def get_dtz_field(instance):
- return getattr(instance, dt_field_name)
- def set_dtz_field(instance, dt):
- if dt.tzinfo is None:
- dt = default_tz.localize(dt)
- time_zone = field.timezone
- if isinstance(field.timezone, basestring):
- tz_name = instance._default_manager.filter(
- pk=model_instance._get_pk_val()
- ).values_list(field.timezone)[0][0]
- try:
- time_zone = pytz.timezone(tz_name)
- except:
- time_zone = default_tz
- if time_zone is None:
- # lookup failed
- time_zone = default_tz
- #raise pytz.UnknownTimeZoneError(
- # "Time zone %r from relation %r was not found"
- # % (tz_name, field.timezone)
- #)
- elif callable(time_zone):
- tz_name = time_zone()
- if isinstance(tz_name, basestring):
+
+ # Beware python scoping and for loops (for loops don't introduce new scope)
+ # http://stackoverflow.com/questions/233673/lexical-closures-in-python
+ def gen_getset_dtz_field(field):
+ dt_field_name = "_datetimezone_%s" % field.attname
+
+ def get_dtz_field(instance):
+ return getattr(instance, dt_field_name)
+
+ def set_dtz_field(instance, dt):
+ if dt.tzinfo is None:
+ dt = default_tz.localize(dt)
+ time_zone = field.timezone
+ if isinstance(field.timezone, basestring):
+ tz_name = instance._default_manager.filter(
+ pk=model_instance._get_pk_val()
+ ).values_list(field.timezone)[0][0]
try:
time_zone = pytz.timezone(tz_name)
except:
time_zone = default_tz
- else:
- time_zone = tz_name
- if time_zone is None:
- # lookup failed
- time_zone = default_tz
- #raise pytz.UnknownTimeZoneError(
- # "Time zone %r from callable %r was not found"
- # % (tz_name, field.timezone)
- #)
- setattr(instance, dt_field_name, dt.astimezone(time_zone))
- setattr(sender, field.attname, property(get_dtz_field, set_dtz_field))
+ if time_zone is None:
+ # lookup failed
+ time_zone = default_tz
+ #raise pytz.UnknownTimeZoneError(
+ # "Time zone %r from relation %r was not found"
+ # % (tz_name, field.timezone)
+ #)
+ elif callable(time_zone):
+ tz_name = time_zone()
+ if isinstance(tz_name, basestring):
+ try:
+ time_zone = pytz.timezone(tz_name)
+ except:
+ time_zone = default_tz
+ else:
+ time_zone = tz_name
+ if time_zone is None:
+ # lookup failed
+ time_zone = default_tz
+ #raise pytz.UnknownTimeZoneError(
+ # "Time zone %r from callable %r was not found"
+ # % (tz_name, field.timezone)
+ #)
+ setattr(instance, dt_field_name, dt.astimezone(time_zone))
+ return (get_dtz_field, set_dtz_field)
+ setattr(sender, field.attname, property(*gen_getset_dtz_field(field)))
## RED_FLAG: need to add a check at manage.py validation time that
## time_zone value is a valid query keyword (if it is one)

0 comments on commit 0ccee7e

Please sign in to comment.