Skip to content

Commit

Permalink
Merge pull request #493 from erans/master
Browse files Browse the repository at this point in the history
Fix: a generic way to parse all the input JSON and make sure we replace ISODate to Python date times.
  • Loading branch information
arikfr committed Jul 15, 2015
2 parents 41b9b21 + 5fc7c49 commit 7702b05
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions redash/query_runner/mongodb.py
Expand Up @@ -41,7 +41,6 @@ def default(self, o):

return super(MongoDBJSONEncoder, self).default(o)


# Simple query example:
#
# {
Expand Down Expand Up @@ -148,6 +147,16 @@ def _get_column_by_name(self, columns, column_name):

return None

def _fix_dates(self, data):
for k in data:
if isinstance(data[k], list):
for i in range(0, len(data[k])):
self._fix_dates(data[k][i])
elif isinstance(data[k], dict):
self._fix_dates(data[k])
else:
if isinstance(data[k], (str, unicode)):
self._convert_date(data, k)

def _convert_date(self, q, field_name):
m = date_regex.findall(q[field_name])
Expand All @@ -167,6 +176,7 @@ def run_query(self, query):

try:
query_data = json.loads(query)
self._fix_dates(query_data)
except ValueError:
return None, "Invalid query format. The query is not a valid JSON."

Expand All @@ -176,33 +186,13 @@ def run_query(self, query):
collection = query_data["collection"]

q = None
if "query" in query_data:
q = query_data["query"]
for k in q:
if q[k] and type(q[k]) in [str, unicode]:
logging.debug(q[k])
self._convert_date(q, k)
elif q[k] and type(q[k]) is dict:
for k2 in q[k]:
if type(q[k][k2]) in [str, unicode]:
self._convert_date(q[k], k2)

f = None

aggregate = None
if "aggregate" in query_data:
aggregate = query_data["aggregate"]
for step in aggregate:
if "$match" in step:
for field in step["$match"]:
if type(step["$match"][field]) in [str, unicode]:
logging.debug(step["$match"][field])
self._convert_date(step["$match"], field)
elif type(step["$match"][field]) is dict:
for field2 in step["$match"][field]:
if type(step["$match"][field][field2]) in (str, unicode):
self._convert_date(step["$match"][field], field2)
elif "$sort" in step:
if "$sort" in step:
sort_list = []
for sort_item in step["$sort"]:
sort_list.append((sort_item["name"], sort_item["direction"]))
Expand Down

0 comments on commit 7702b05

Please sign in to comment.