diff options
author | Chris Moyer <kopertop@gmail.com> | 2012-02-20 11:31:37 -0500 |
---|---|---|
committer | Chris Moyer <kopertop@gmail.com> | 2012-02-20 11:31:37 -0500 |
commit | d5a973bd3889a1e3a257e52c8e531a767770d472 (patch) | |
tree | 78814bb1b498a9af0a7792f8cafdfa79fe0b347c | |
parent | 97c40d193a069eab55e06abdb67aaad59b4e1e43 (diff) | |
download | boto-d5a973bd3889a1e3a257e52c8e531a767770d472.tar.gz |
Allow a DateTimeProperty to take either a Date or DateTime object
-rw-r--r-- | boto/sdb/db/manager/sdbmanager.py | 16 | ||||
-rw-r--r-- | boto/sdb/db/property.py | 9 |
2 files changed, 19 insertions, 6 deletions
diff --git a/boto/sdb/db/manager/sdbmanager.py b/boto/sdb/db/manager/sdbmanager.py index 8218f816..1a4baa8f 100644 --- a/boto/sdb/db/manager/sdbmanager.py +++ b/boto/sdb/db/manager/sdbmanager.py @@ -257,12 +257,22 @@ class SDBConverter(object): def encode_datetime(self, value): if isinstance(value, str) or isinstance(value, unicode): return value - return value.strftime(ISO8601) + if isinstance(value, date): + return value.isoformat() + else: + return value.strftime(ISO8601) def decode_datetime(self, value): + """Handles both Dates and DateTime objects""" + if value is None: + return value try: - return datetime.strptime(value, ISO8601) - except: + if "T" in value: + return datetime.strptime(value, ISO8601) + else: + value = value.split("-") + return date(int(value[0]), int(value[1]), int(value[2])) + except Exception, e: return None def encode_date(self, value): diff --git a/boto/sdb/db/property.py b/boto/sdb/db/property.py index 1929a027..698ec762 100644 --- a/boto/sdb/db/property.py +++ b/boto/sdb/db/property.py @@ -375,6 +375,9 @@ class FloatProperty(Property): return value is None class DateTimeProperty(Property): + """This class handles both the datetime.datetime object + And the datetime.date objects. It can return either one, + depending on the value stored in the database""" data_type = datetime.datetime type_name = 'DateTime' @@ -391,11 +394,11 @@ class DateTimeProperty(Property): return Property.default_value(self) def validate(self, value): - value = super(DateTimeProperty, self).validate(value) if value == None: return - if not isinstance(value, self.data_type): - raise TypeError, 'Validation Error, expecting %s, got %s' % (self.data_type, type(value)) + if isinstance(value, datetime.date): + return value + return super(DateTimeProperty, self).validate(value) def get_value_for_datastore(self, model_instance): if self.auto_now: |