Skip to content

Commit

Permalink
Merge pull request #118 from dimagi/meta-dates
Browse files Browse the repository at this point in the history
fix meta datetime properties formatted as dates (not datetimes)
  • Loading branch information
millerdev committed Jul 10, 2014
2 parents c63a5d6 + ecd764a commit b62b120
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
12 changes: 7 additions & 5 deletions couchforms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from dimagi.utils.couch import CouchDocLockableMixIn

from dimagi.utils.indicators import ComputedDocumentMixin
from dimagi.utils.parsing import string_to_datetime
from dimagi.utils.parsing import string_to_datetime, json_format_datetime
from dimagi.utils.couch.safe_index import safe_index
from dimagi.utils.couch.database import get_safe_read_kwargs, SafeSaveDocument
from dimagi.utils.mixins import UnicodeMixIn
Expand Down Expand Up @@ -145,11 +145,9 @@ def uiversion(self):
def metadata(self):
if (const.TAG_META) in self.form:
def _clean(meta_block):
# couchdbkit chokes on dates that aren't actually dates
# so check their validity before passing them up
ret = copy(dict(meta_block))
for key in ret.keys():
#remove attributes from the meta block
# remove attributes from the meta block
if key.startswith('@'):
del ret[key]

Expand All @@ -161,13 +159,17 @@ def _clean(meta_block):
else:
ret['appVersion'] = unicode(meta_block['appVersion'])

# couchdbkit chokes on dates that aren't actually dates
# so check their validity before passing them up
if meta_block:
for key in ("timeStart", "timeEnd"):
if key in meta_block:
if meta_block[key]:
try:
#try to parse to ensure correctness
# try to parse to ensure correctness
parsed = string_to_datetime(meta_block[key])
# and set back in the right format in case it was a date, not a datetime
ret[key] = json_format_datetime(parsed)
except ValueError:
# we couldn't parse it
del ret[key]
Expand Down
10 changes: 10 additions & 0 deletions couchforms/tests/data/date_in_meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data xmlns="http://bihar.commcarehq.org/mcts/create_mcts_persona">
<meta xmlns="http://openrosa.org/jr/xforms">
<instanceID>c28f68a5-e920-4594-aea7-dbae3c3650b4</instanceID>
<timeEnd>2014-07-11</timeEnd>
<timeStart>2014-07-10</timeStart>
<userID>12345</userID>
</meta>
<foo>bar</foo>
</data>
11 changes: 10 additions & 1 deletion couchforms/tests/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from datetime import date
from datetime import date, datetime
from django.test import TestCase
from couchforms import create_xform_from_xml
from couchforms.models import XFormInstance
Expand Down Expand Up @@ -96,3 +96,12 @@ def testMetaAppVersionDict(self):
'deviceID': u'commconnect'
})
xform.delete()

def testMetaDateInDatetimeFields(self):
file_path = os.path.join(os.path.dirname(__file__), "data", "date_in_meta.xml")
xml_data = open(file_path, "rb").read()
with create_xform_from_xml(xml_data) as doc_id:
xform = XFormInstance.get(doc_id)
self.assertEqual(datetime(2014, 7, 10), xform.metadata.timeStart)
self.assertEqual(datetime(2014, 7, 11), xform.metadata.timeEnd)
xform.delete()

0 comments on commit b62b120

Please sign in to comment.