[gen] Bugfix in the query engine.

This commit is contained in:
Gaetan Delannay 2013-04-30 11:12:02 +02:00
parent e5d6887b65
commit 049ddb20b4
2 changed files with 20 additions and 13 deletions

View file

@ -314,8 +314,8 @@ class Search:
# use it instead of trying to translate from labels. # use it instead of trying to translate from labels.
self.translated = translated self.translated = translated
self.translatedDescr = translatedDescr self.translatedDescr = translatedDescr
# In the dict below, keys are indexed field names and values are # In the dict below, keys are indexed field names or names of standard
# search values. # indexes, and values are search values.
self.fields = fields self.fields = fields
@staticmethod @staticmethod
@ -336,13 +336,14 @@ class Search:
return 'get%s%s'% (fieldName[0].upper(),fieldName[1:]) return 'get%s%s'% (fieldName[0].upper(),fieldName[1:])
@staticmethod @staticmethod
def getSearchValue(fieldName, fieldValue): def getSearchValue(fieldName, fieldValue, klass):
'''Returns a transformed p_fieldValue for producing a valid search '''Returns a transformed p_fieldValue for producing a valid search
value as required for searching in the index corresponding to value as required for searching in the index corresponding to
p_fieldName.''' p_fieldName.'''
if fieldName in ('title', 'SearchableText'): field = getattr(klass, fieldName, None)
# Title and SearchableText are TextIndex indexes. We must split if (field and (field.getIndexType() == 'TextIndex')) or \
# p_fieldValue into keywords. (fieldName == 'SearchableText'):
# For TextIndex indexes. We must split p_fieldValue into keywords.
res = Keywords(fieldValue).get() res = Keywords(fieldValue).get()
elif isinstance(fieldValue, basestring) and fieldValue.endswith('*'): elif isinstance(fieldValue, basestring) and fieldValue.endswith('*'):
v = fieldValue[:-1] v = fieldValue[:-1]
@ -369,7 +370,7 @@ class Search:
res = fieldValue res = fieldValue
return res return res
def updateSearchCriteria(self, criteria, advanced=False): def updateSearchCriteria(self, criteria, klass, advanced=False):
'''This method updates dict p_criteria with all the search criteria '''This method updates dict p_criteria with all the search criteria
corresponding to this Search instance. If p_advanced is True, corresponding to this Search instance. If p_advanced is True,
p_criteria correspond to an advanced search, to be stored in the p_criteria correspond to an advanced search, to be stored in the
@ -387,7 +388,8 @@ class Search:
if not advanced: if not advanced:
attrName = Search.getIndexName(fieldName) attrName = Search.getIndexName(fieldName)
# Express the field value in the way needed by the index # Express the field value in the way needed by the index
criteria[attrName]= Search.getSearchValue(fieldName, fieldValue) criteria[attrName] = Search.getSearchValue(fieldName,
fieldValue, klass)
else: else:
criteria[fieldName]= fieldValue criteria[fieldName]= fieldValue
# Add a sort order if specified # Add a sort order if specified
@ -821,6 +823,7 @@ class Type:
# we consider it to be an index type. It allows to bypass the standard # we consider it to be an index type. It allows to bypass the standard
# way to decide what index type must be used. # way to decide what index type must be used.
if isinstance(self.indexed, str): return self.indexed if isinstance(self.indexed, str): return self.indexed
if self.name == 'title': return 'TextIndex'
return 'FieldIndex' return 'FieldIndex'
def getIndexValue(self, obj, forSearch=False): def getIndexValue(self, obj, forSearch=False):

View file

@ -348,12 +348,13 @@ class ToolMixin(BaseMixin):
If p_refObject and p_refField are given, the query is limited to the If p_refObject and p_refField are given, the query is limited to the
objects that are referenced from p_refObject through p_refField.''' objects that are referenced from p_refObject through p_refField.'''
params = {'ClassName': className} params = {'ClassName': className}
appyClass = self.getAppyClass(className, wrapper=True)
if not brainsOnly: params['batch'] = True if not brainsOnly: params['batch'] = True
# Manage additional criteria from a search when relevant # Manage additional criteria from a search when relevant
if searchName: search = self.getSearch(className, searchName) if searchName: search = self.getSearch(className, searchName)
if search: if search:
# Add in params search and sort criteria. # Add in params search and sort criteria.
search.updateSearchCriteria(params) search.updateSearchCriteria(params, appyClass)
# Determine or override sort if specified. # Determine or override sort if specified.
if sortBy: if sortBy:
params['sort_on'] = Search.getIndexName(sortBy, usage='sort') params['sort_on'] = Search.getIndexName(sortBy, usage='sort')
@ -362,7 +363,7 @@ class ToolMixin(BaseMixin):
# If defined, add the filter among search parameters. # If defined, add the filter among search parameters.
if filterKey: if filterKey:
filterKey = Search.getIndexName(filterKey) filterKey = Search.getIndexName(filterKey)
filterValue = Search.getSearchValue(filterKey, filterValue) filterValue = Search.getSearchValue(filterKey,filterValue,appyClass)
params[filterKey] = filterValue params[filterKey] = filterValue
# TODO This value needs to be merged with an existing one if already # TODO This value needs to be merged with an existing one if already
# in params, or, in a first step, we should avoid to display the # in params, or, in a first step, we should avoid to display the
@ -372,6 +373,7 @@ class ToolMixin(BaseMixin):
params['UID'] = getattr(refObject, refField.name).data params['UID'] = getattr(refObject, refField.name).data
# Use index "Allowed" if noSecurity is False # Use index "Allowed" if noSecurity is False
if not noSecurity: params['Allowed'] = self.getAllowedValue() if not noSecurity: params['Allowed'] = self.getAllowedValue()
print params
brains = self.getPath("/catalog")(**params) brains = self.getPath("/catalog")(**params)
if brainsOnly: if brainsOnly:
# Return brains only. # Return brains only.
@ -602,9 +604,11 @@ class ToolMixin(BaseMixin):
if 'className' not in rq.form: return res if 'className' not in rq.form: return res
klass = self.getAppyClass(rq.form['className']) klass = self.getAppyClass(rq.form['className'])
if not hasattr(klass, 'searchAdvanced'): return res if not hasattr(klass, 'searchAdvanced'): return res
# In this attribute, we have the Search instance representing automatic # In klass.searchAdvanced, we have the Search instance representing
# advanced search criteria. # default advanced search criteria.
klass.searchAdvanced.updateSearchCriteria(res, advanced=True) wrapperClass = self.getAppyClass(rq.form['className'], wrapper=True)
klass.searchAdvanced.updateSearchCriteria(res, wrapperClass,
advanced=True)
return res return res
transformMethods = {'uppercase': 'upper', 'lowercase': 'lower', transformMethods = {'uppercase': 'upper', 'lowercase': 'lower',