Skip to content

Commit 5c20622

Browse files
xisparamonski
authored andcommitted
ReferenceWidget does not handle searches with null/None (#1014)
ast does not convert "null" to python's None and the following Traceback is thrown: Traceback (innermost last): Module ZPublisher.Publish, line 138, in publish Module ZPublisher.mapply, line 77, in mapply Module ZPublisher.Publish, line 48, in call_object Module bika.lims.browser.widgets.referencewidget, line 204, in __call__ Module bika.lims.browser.client.ajax, line 31, in __call__ Module bika.lims.adapters.referencewidgetvocabulary, line 44, in __call__ Module ast, line 80, in literal_eval Module ast, line 63, in _convert Module ast, line 62, in <genexpr> Module ast, line 60, in _convert Module ast, line 79, in _convert ValueError: malformed string ast.literal_eval has been replaced by built-in json.loads, and to overcome the issue about unicodes: #443 a function that converts unicodes to strings recursively has been added, as suggested in the same issue above
1 parent 9907a09 commit 5c20622

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

CHANGES.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ Changelog
2121

2222
**Fixed**
2323

24+
- #1014 ReferenceWidget does not handle searches with null/None
2425
- #1008 Previous results from same batch are always displayed in reports
25-
- #1012 ARs and Samples from other clients are listed when logged in as contact
26+
- #1013 ARs and Samples from other clients are listed when logged in as contact
2627
- #991 New client contacts do not have access to their own AR Templates
2728
- #996 Hide checkbox labels on category expansion
2829
- #990 Fix client analysisspecs view

bika/lims/adapters/referencewidgetvocabulary.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# Copyright 2018 by it's authors.
66
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
77

8-
import ast
98
import json
109

1110
from zope.interface import implements
@@ -35,11 +34,13 @@ def __call__(self, result=None, specification=None, **kwargs):
3534
catalog_name = _c(self.request.get('catalog_name', 'portal_catalog'))
3635
catalog = getToolByName(self.context, catalog_name)
3736

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
4039
# 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)
4344

4445
# first with all queries
4546
contentFilter = dict((k, v) for k, v in base_query.items())
@@ -129,3 +130,27 @@ def __call__(self, result=None, specification=None, **kwargs):
129130
brains = _brains
130131

131132
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

Comments
 (0)