Skip to content

Commit

Permalink
Ensure proper timezone handling of data passed to update() and create()
Browse files Browse the repository at this point in the history
This ensures data passed directly into the .update() and create()
methods of a DBDataWrite class is properly made timezone-aware by
converting to the modified datetime class, before being pushed to the
database.
  • Loading branch information
wagnerrp committed Sep 20, 2012
1 parent 3128dac commit 44ebd80
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions mythtv/bindings/python/MythTV/database.py
Expand Up @@ -133,7 +133,7 @@ def fromRaw(cls, raw, db=None):
return dbdata

def __setitem__(self, key, value):
for k,v in self._db.tablefields[self._table].items():
for k,v in self._field_order.items():
if k == key:
if v.type in ('datetime','timestamp'):
value = datetime.duck(value)
Expand Down Expand Up @@ -181,7 +181,7 @@ def __init__(self, data, db=None):

def _process(self, data):
data = DictData._process(self, data)
for key, val in self._db.tablefields[self._table].items():
for key, val in self._field_order.items():
if (val.type in ('datetime','timestamp')) \
and (data[key] is not None):
data[key] = datetime.fromnaiveutc(data[key])
Expand Down Expand Up @@ -331,6 +331,12 @@ def _create(self, data=None, cursor=None):
self._create(data, cursor)
return

if data is not None:
for k,v in self._field_order.items():
if (k in data) and (data[k] is not None) and \
(v.type in ('datetime','timestamp')):
data[k] = datetime.duck(data[k])

self._import(data)
data = self._sanitize(dict(self))
for key,val in data.items():
Expand Down Expand Up @@ -407,7 +413,13 @@ def update(self, *args, **keywords):

data = {}
data.update(*args, **keywords)
dict.update(self, self._sanitize(data))
data = self._sanitize(data)

for k in data:
if self._field_order[k].type in ('datetime', 'timestamp'):
data[k] = datetime.duck(data[k])

dict.update(self, data)
self._push()

def delete(self):
Expand Down

0 comments on commit 44ebd80

Please sign in to comment.