[gen] Replaced attributes tool.enableAdvancedSearchForX by klass.searchAdvanced.show.

This commit is contained in:
Gaetan Delannay 2013-06-07 16:37:00 +02:00
parent 0d0d7c1123
commit 5a50d07e54
4 changed files with 41 additions and 29 deletions

View file

@ -298,7 +298,7 @@ class Import:
class Search: class Search:
'''Used for specifying a search for a given type.''' '''Used for specifying a search for a given type.'''
def __init__(self, name, group=None, sortBy='', sortOrder='asc', limit=None, def __init__(self, name, group=None, sortBy='', sortOrder='asc', limit=None,
default=False, colspan=1, translated=None, default=False, colspan=1, translated=None, show=True,
translatedDescr=None, **fields): translatedDescr=None, **fields):
self.name = name self.name = name
# Searches may be visually grouped in the portlet. # Searches may be visually grouped in the portlet.
@ -314,6 +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
# Condition for showing or not this search
self.show = show
# In the dict below, keys are indexed field names or names of standard # In the dict below, keys are indexed field names or names of standard
# indexes, and values are search values. # indexes, and values are search values.
self.fields = fields self.fields = fields
@ -331,6 +333,8 @@ class Search:
# 'SortableTitle', because index 'Title' is a TextIndex # 'SortableTitle', because index 'Title' is a TextIndex
# (for searchability) and can't be used for sorting. # (for searchability) and can't be used for sorting.
elif fieldName == 'state': return 'State' elif fieldName == 'state': return 'State'
elif fieldName == 'created': return 'Created'
elif fieldName == 'modified': return 'Modified'
elif fieldName in defaultIndexes: return fieldName elif fieldName in defaultIndexes: return fieldName
else: else:
return 'get%s%s'% (fieldName[0].upper(),fieldName[1:]) return 'get%s%s'% (fieldName[0].upper(),fieldName[1:])
@ -403,6 +407,12 @@ class Search:
criteria['sortBy'] = self.sortBy criteria['sortBy'] = self.sortBy
criteria['sortOrder'] = self.sortOrder criteria['sortOrder'] = self.sortOrder
def isShowable(self, klass, tool):
'''Is this Search instance (defined in p_klass) showable?'''
if self.show.__class__.__name__ == 'staticmethod':
return self.show.__get__(klass)(tool)
return self.show
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
class Type: class Type:
'''Basic abstract class for defining any appy type.''' '''Basic abstract class for defining any appy type.'''

View file

@ -174,35 +174,28 @@ class ClassDescriptor(Descriptor):
def getCreateMean(self, type='Import'): def getCreateMean(self, type='Import'):
'''Returns the mean for this class that corresponds to p_type, or '''Returns the mean for this class that corresponds to p_type, or
None if the class does not support this create mean.''' None if the class does not support this create mean.'''
if not self.klass.__dict__.has_key('create'): return None if not self.klass.__dict__.has_key('create'): return
else: else:
means = self.klass.create means = self.klass.create
if not means: return None if not means: return
if not isinstance(means, tuple) and not isinstance(means, list): if not isinstance(means, tuple) and not isinstance(means, list):
means = [means] means = [means]
for mean in means: for mean in means:
exec 'found = isinstance(mean, %s)' % type exec 'found = isinstance(mean, %s)' % type
if found: return mean if found: return mean
return None
@staticmethod @staticmethod
def getSearches(klass): def getSearches(klass, tool=None):
'''Returns the list of searches that are defined on this class.''' '''Returns the list of searches that are defined on this class. If
res = [] p_tool is given, we are at execution time (not a generation time),
and we may potentially execute search.show methods that allow to
conditionnaly include a search or not.'''
if klass.__dict__.has_key('search'): if klass.__dict__.has_key('search'):
searches = klass.__dict__['search'] searches = klass.__dict__['search']
if isinstance(searches, basestring): if not tool: return searches
res.append(gen.Search(searches)) # Evaluate attributes "show" for every search.
elif isinstance(searches, gen.Search): return [s for s in searches if s.isShowable(klass, tool)]
res.append(searches) return []
else:
# It must be a list of searches.
for search in searches:
if isinstance(search, basestring):
res.append(gen.Search(search))
else:
res.append(search)
return res
@staticmethod @staticmethod
def getSearch(klass, searchName): def getSearch(klass, searchName):
@ -210,7 +203,6 @@ class ClassDescriptor(Descriptor):
for search in ClassDescriptor.getSearches(klass): for search in ClassDescriptor.getSearches(klass):
if search.name == searchName: if search.name == searchName:
return search return search
return None
def addIndexMethod(self, field): def addIndexMethod(self, field):
'''For indexed p_field, this method generates a method that allows to '''For indexed p_field, this method generates a method that allows to

View file

@ -723,22 +723,23 @@ class ToolMixin(BaseMixin):
default = None # Also retrieve the default one here. default = None # Also retrieve the default one here.
groups = {} # The already encountered groups groups = {} # The already encountered groups
page = Page('main') # A dummy page required by class GroupDescr page = Page('main') # A dummy page required by class GroupDescr
searches = ClassDescriptor.getSearches(appyClass) # Get the searches statically defined on the class
# Add dynamic searches if defined on p_appyClass searches = ClassDescriptor.getSearches(appyClass, tool=self.appy())
# Get the dynamically computed searches
if hasattr(appyClass, 'getDynamicSearches'): if hasattr(appyClass, 'getDynamicSearches'):
searches += appyClass.getDynamicSearches(self.appy()) searches += appyClass.getDynamicSearches(self.appy())
for search in searches: for search in searches:
# Create the search descriptor # Create the search descriptor
searchDescr = SearchDescr(search, className, self).get() sDescr = SearchDescr(search, className, self).get()
if not search.group: if not search.group:
# Insert the search at the highest level, not in any group. # Insert the search at the highest level, not in any group.
res.append(searchDescr) res.append(sDescr)
else: else:
groupDescr = search.group.insertInto(res, groups, page, gDescr = search.group.insertInto(res, groups, page, className,
className, forSearch=True) forSearch=True)
GroupDescr.addWidget(groupDescr, searchDescr) GroupDescr.addWidget(gDescr, sDescr)
# Is this search the default search? # Is this search the default search?
if search.default: default = searchDescr if search.default: default = sDescr
return Object(searches=res, default=default).__dict__ return Object(searches=res, default=default).__dict__
def getSearch(self, className, name, descr=False): def getSearch(self, className, name, descr=False):
@ -766,6 +767,15 @@ class ToolMixin(BaseMixin):
if descr: res = SearchDescr(res, className, self).get() if descr: res = SearchDescr(res, className, self).get()
return res return res
def advancedSearchEnabledFor(self, className):
'''Is advanced search visible for p_klass ?'''
klass = self.getAppyClass(className)
# By default, advanced search is enabled.
if not hasattr(klass, 'searchAdvanced'): return True
# Evaluate attribute "show" on this Search instance representing the
# advanced search.
return klass.searchAdvanced.isShowable(klass, self.appy())
def getQueryUrl(self, contentType, searchName, startNumber=None): def getQueryUrl(self, contentType, searchName, startNumber=None):
'''This method creates the URL that allows to perform a (non-Ajax) '''This method creates the URL that allows to perform a (non-Ajax)
request for getting queried objects from a search named p_searchName request for getting queried objects from a search named p_searchName

View file

@ -90,7 +90,7 @@
</tal:icons> </tal:icons>
<br/> <br/>
<tal:search condition="python: tool.getAttr('enableAdvancedSearchFor%s' % rootClass)"> <tal:search condition="python: tool.advancedSearchEnabledFor(rootClass)">
<tal:comment replace="nothing">Live search</tal:comment> <tal:comment replace="nothing">Live search</tal:comment>
<form tal:attributes="action string: $appUrl/config/do"> <form tal:attributes="action string: $appUrl/config/do">
<input type="hidden" name="action" value="SearchObjects"/> <input type="hidden" name="action" value="SearchObjects"/>