Skip to content

Commit

Permalink
Format CSV Imported dateTime values as ISO 8601 strings
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisRayM committed Dec 18, 2019
1 parent b0692d0 commit 0d4f2fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions onadata/libs/tests/utils/test_csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ def test_excel_date_conversion(self):
[u'6/12/2020 13:20', u'2019-03-11T16:00:51.147+02:00'])
self.assertEqual(
conv_datetime,
[u'2020-06-12T13:20:00.000000',
u'2019-03-11T16:00:51.147000+0200'])
[u'2020-06-12T13:20:00',
u'2019-03-11T16:00:51.147000+02:00'])
self.assertEqual(conv_dates, ['2019-03-01', '2019-02-26'])

def test_enforces_data_type(self):
Expand Down
17 changes: 14 additions & 3 deletions onadata/libs/utils/csv_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ def validate_csv(csv_file, xform):
csv_headers = csv_reader.fieldnames

# Make sure CSV headers have no spaces
# because these are converted to XLSForm names
# which cannot have spaces
if any(' ' in header for header in csv_headers):
return {
'error_msg': 'CSV file fieldnames should not contain spaces',
Expand Down Expand Up @@ -548,9 +550,18 @@ def validate_row(row, columns):
if valid:
if datatype in ['date', 'datetime']:
for key in data:
value = datetime.strftime(
data.get(key), '%Y-%m-%d' if datatype == 'date'
else '%Y-%m-%dT%H:%M:%S.%f%z')
# ODK XForms accept date and datetime values formatted in
# accordance to the XML 1.0 spec only deviating when it
# comes to the inclusion of the timezone offset in
# datetime values. This specification matches ISO 8601
value = data.get(key).isoformat()

if datatype == 'date' and value.endswith('T00:00:00'):
# Remove the Time string section from dates
# to follow along with the date datetype specification
# in the ODK XForms Spec
value = value.replace('T00:00:00', '')

data.update({key: value})

row.update(data)
Expand Down

0 comments on commit 0d4f2fc

Please sign in to comment.