Skip to content

Commit

Permalink
Merge pull request geopython#263 from bird-house/fix-json-datetime
Browse files Browse the repository at this point in the history
Fix json datetime encoding exception
  • Loading branch information
jachym authored Apr 23, 2017
2 parents 24901c0 + 1868e71 commit 03a766f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
10 changes: 9 additions & 1 deletion pywps/app/WPSRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lxml.etree
from werkzeug.exceptions import MethodNotAllowed
import base64
import datetime
from pywps import WPS
from pywps._compat import text_type, PY2
from pywps.app.basic import xpath_ns
Expand Down Expand Up @@ -303,6 +304,13 @@ def check_and_set_language(self, language):
def json(self):
"""Return JSON encoded representation of the request
"""
class ExtendedJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.date) or isinstance(obj, datetime.time):
encoded_object = obj.isoformat()
else:
encoded_object = json.JSONEncoder.default(self, obj)
return encoded_object

obj = {
'operation': self.operation,
Expand All @@ -317,7 +325,7 @@ def json(self):
'raw': self.raw
}

return json.dumps(obj, allow_nan=False)
return json.dumps(obj, allow_nan=False, cls=ExtendedJSONEncoder)

@json.setter
def json(self, value):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import os
import tempfile
import datetime
import unittest
from pywps import Format
from pywps.validator import get_validator
Expand Down Expand Up @@ -295,6 +296,33 @@ def test_json_out(self):
self.assertEqual(len(out['allowed_values']), 3, '3 allowed values')
self.assertEqual(out['allowed_values'][0]['value'], 1, 'allowed value 1')

def test_json_out_datetime(self):
inpt = LiteralInput(
identifier="datetime",
mode=2,
data_type='dateTime')
inpt.data = "2017-04-20T12:30:00"
out = inpt.json
self.assertEqual(out['data'], datetime.datetime(2017, 4, 20, 12, 30, 0), 'datetime set')

def test_json_out_time(self):
inpt = LiteralInput(
identifier="time",
mode=2,
data_type='time')
inpt.data = "12:30:00"
out = inpt.json
self.assertEqual(out['data'], datetime.time(12, 30, 0), 'time set')

def test_json_out_date(self):
inpt = LiteralInput(
identifier="date",
mode=2,
data_type='date')
inpt.data = "2017-04-20"
out = inpt.json
self.assertEqual(out['data'], datetime.date(2017, 4, 20), 'date set')


class LiteralOutputTest(unittest.TestCase):
"""LiteralOutput test cases"""
Expand Down
56 changes: 54 additions & 2 deletions tests/test_wpsrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import lxml.etree
from pywps.app import WPSRequest
import tempfile
import datetime
import json


class WPSRequestTest(unittest.TestCase):
Expand All @@ -21,7 +23,6 @@ def setUp(self):
x.write("ahoj")
x.close()


def test_json_in(self):

obj = {
Expand All @@ -46,7 +47,7 @@ def test_json_in(self):
'identifier': 'myliteral',
'type': 'literal',
'data_type': 'integer',
'allowed_values': [ {'type':'anyvalue'} ],
'allowed_values': [{'type': 'anyvalue'}],
'data': 1
}]
},
Expand All @@ -62,6 +63,57 @@ def test_json_in(self):
self.assertListEqual(self.request.inputs['myliteral'][0].allowed_values, [], 'Any value set')
self.assertTrue(self.request.inputs['myliteral'][0].any_value, 'Any value set')

def test_json_inout_datetime(self):
obj = {
'operation': 'getcapabilities',
'version': '1.0.0',
'language': 'eng',
'identifiers': 'moinmoin',
'store_execute': True,
'status': True,
'lineage': True,
'inputs': {
'datetime': [{
'identifier': 'datetime',
'type': 'literal',
'data_type': 'dateTime',
'data': '2017-04-20T12:00:00',
'allowed_values': [{'type': 'anyvalue'}],
}],
'date': [{
'identifier': 'date',
'type': 'literal',
'data_type': 'date',
'data': '2017-04-20',
'allowed_values': [{'type': 'anyvalue'}],
}],
'time': [{
'identifier': 'time',
'type': 'literal',
'data_type': 'time',
'data': '09:00:00',
'allowed_values': [{'type': 'anyvalue'}],
}],
},
'outputs': {},
'raw': False
}

self.request = WPSRequest()
self.request.json = obj

self.assertEqual(self.request.inputs['datetime'][0].data, datetime.datetime(2017, 4, 20, 12), 'Datatime set')
self.assertEqual(self.request.inputs['date'][0].data, datetime.date(2017, 4, 20), 'Data set')
self.assertEqual(self.request.inputs['time'][0].data, datetime.time(9, 0, 0), 'Time set')

# dump to json and reload
dump = self.request.json
self.request.json = json.loads(dump)

self.assertEqual(self.request.inputs['datetime'][0].data, datetime.datetime(2017, 4, 20, 12), 'Datatime set')
self.assertEqual(self.request.inputs['date'][0].data, datetime.date(2017, 4, 20), 'Data set')
self.assertEqual(self.request.inputs['time'][0].data, datetime.time(9, 0, 0), 'Time set')


def load_tests(loader=None, tests=None, pattern=None):
if not loader:
Expand Down

0 comments on commit 03a766f

Please sign in to comment.