Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceWidget does not handle searches with null/None #1014

Merged
merged 1 commit into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ Changelog

**Fixed**

- #1014 ReferenceWidget does not handle searches with null/None
- #1008 Previous results from same batch are always displayed in reports
- #1012 ARs and Samples from other clients are listed when logged in as contact
- #1013 ARs and Samples from other clients are listed when logged in as contact
- #991 New client contacts do not have access to their own AR Templates
- #996 Hide checkbox labels on category expansion
- #990 Fix client analysisspecs view
Expand Down
35 changes: 30 additions & 5 deletions bika/lims/adapters/referencewidgetvocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# Copyright 2018 by it's authors.
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.

import ast
import json

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

# N.B. We don't use json.loads to avoid unicode conversion, which will
# fail in the catalog search for some cases
# json.loads does unicode conversion, which will fail in the catalog
# search for some cases. So we need to convert the strings to utf8
# see: https://github.com/senaite/bika.lims/issues/443
base_query = ast.literal_eval(self.request['base_query'])
search_query = ast.literal_eval(self.request.get('search_query', "{}"))
base_query = json.loads(self.request['base_query'])
search_query = json.loads(self.request.get('search_query', "{}"))
base_query = self.to_utf8(base_query)
search_query = self.to_utf8(search_query)

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

return brains

def to_utf8(self, data):
"""
Convert unicode values to strings even if they belong to lists or dicts.
:param data: an object.
:return: The object with all unicode values converted to string.
"""
# if this is a unicode string, return its string representation
if isinstance(data, unicode):
return data.encode('utf-8')

# if this is a list of values, return list of string values
if isinstance(data, list):
return [self.to_utf8(item) for item in data]

# if this is a dictionary, return dictionary of string keys and values
if isinstance(data, dict):
return {
self.to_utf8(key): self.to_utf8(value)
for key, value in data.iteritems()
}
# if it's anything else, return it in its original form

return data