|
5 | 5 | # Copyright 2018 by it's authors.
|
6 | 6 | # Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
|
7 | 7 |
|
8 |
| -import ast |
9 | 8 | import json
|
10 | 9 |
|
11 | 10 | from zope.interface import implements
|
@@ -35,11 +34,13 @@ def __call__(self, result=None, specification=None, **kwargs):
|
35 | 34 | catalog_name = _c(self.request.get('catalog_name', 'portal_catalog'))
|
36 | 35 | catalog = getToolByName(self.context, catalog_name)
|
37 | 36 |
|
38 |
| - # N.B. We don't use json.loads to avoid unicode conversion, which will |
39 |
| - # fail in the catalog search for some cases |
| 37 | + # json.loads does unicode conversion, which will fail in the catalog |
| 38 | + # search for some cases. So we need to convert the strings to utf8 |
40 | 39 | # see: https://github.com/senaite/bika.lims/issues/443
|
41 |
| - base_query = ast.literal_eval(self.request['base_query']) |
42 |
| - search_query = ast.literal_eval(self.request.get('search_query', "{}")) |
| 40 | + base_query = json.loads(self.request['base_query']) |
| 41 | + search_query = json.loads(self.request.get('search_query', "{}")) |
| 42 | + base_query = self.to_utf8(base_query) |
| 43 | + search_query = self.to_utf8(search_query) |
43 | 44 |
|
44 | 45 | # first with all queries
|
45 | 46 | contentFilter = dict((k, v) for k, v in base_query.items())
|
@@ -129,3 +130,27 @@ def __call__(self, result=None, specification=None, **kwargs):
|
129 | 130 | brains = _brains
|
130 | 131 |
|
131 | 132 | return brains
|
| 133 | + |
| 134 | + def to_utf8(self, data): |
| 135 | + """ |
| 136 | + Convert unicode values to strings even if they belong to lists or dicts. |
| 137 | + :param data: an object. |
| 138 | + :return: The object with all unicode values converted to string. |
| 139 | + """ |
| 140 | + # if this is a unicode string, return its string representation |
| 141 | + if isinstance(data, unicode): |
| 142 | + return data.encode('utf-8') |
| 143 | + |
| 144 | + # if this is a list of values, return list of string values |
| 145 | + if isinstance(data, list): |
| 146 | + return [self.to_utf8(item) for item in data] |
| 147 | + |
| 148 | + # if this is a dictionary, return dictionary of string keys and values |
| 149 | + if isinstance(data, dict): |
| 150 | + return { |
| 151 | + self.to_utf8(key): self.to_utf8(value) |
| 152 | + for key, value in data.iteritems() |
| 153 | + } |
| 154 | + # if it's anything else, return it in its original form |
| 155 | + |
| 156 | + return data |
0 commit comments