class AnyCriterium(TextCriterium): """ Full text criterium for 'any-fulltext' index.""" zope.interface.implements(interfaces.ICriteriumDescription) indexOrName = 'any-fulltext' label = _('any-fulltext-label', u"Free Text (Any Field)") description = _('any-fulltext-desc', u"Matches in any (or almost any) field.") ui_weight = 1
class ListViewColumn(column.Column): """ The quid attribute of an example object.""" header = _('listview-column', u"Item") weight = 20 def renderCell(self, item): return zope.component.getMultiAdapter((item, self.request), name='list').__call__()
class LabelColumn(column.LinkColumn): """ The quid attribute of an example object.""" header = _('label-column', u"Item type") weight = 10 def getLinkContent(self, item): label = zope.component.getMultiAdapter((item, self.request), name='label').__call__() return translate(label, context=self.request)
class FlagsColumn(column.Column): """ The flags of a example.""" header = _(u"flags") weight = 99999 def renderCell(self, item): flags = zope.component.getMultiAdapter( (item, self.request, self.table), IContentProvider, name='flags') flags.update() return flags.render()
class TypeCriterium(SearchCriterium): """ Search criterium for 'type-field' field index. Because we use this to limit the extent of a search result to a certain content type, this criterium 'and'-connects. """ indexOrName = 'type-field' label = _('type-field-label', u"Search Target") connectorName = CONNECTOR_AND
def getLabelsAndDescriptions(self): for name, factory in self.getCriteriaInOrder(): crit = factory() try: desc = getattr(ICriteriumDescription(factory), 'description', u"") except Exception: desc = None crit_type = u"" if ITextCriterium.providedBy(crit): crit_type = _(u"Fulltext Index") yield (name, crit, desc, crit_type)
class IdCriteriumDescription(object): """ An adapter for the Id Criterium. """ zope.interface.implements(interfaces.ICriteriumDescription) zope.component.adapts(IdCriterium) def __init__(self, context): self.context = context description = _( 'id-field-desc', u"Matches if an item's unique id-number equals the query.") ui_weight = 5
def _updateForm(self): form = self.request.form criteria_count = 0 self.status = [] if form.get(self.prefix + 'button.search', u"") == 'search': fltr = zope.component.createObject( str(form.get(self.prefix + u"filter", self.filterFactoryName))) #if not fltr: # fltr = QuotationtoolSearchFilter() for i in range(len(list(self.query))): connector = form.get(self.prefix + unicode(i) + u'.connector', u"OR") criterium = form.get(self.prefix + unicode(i) + u'.criterium', u"") query = form.get(self.prefix + unicode(i) + u'.query', u"") if not (criterium and query): continue try: crit = fltr.createCriterium(criterium) except Exception, err: self.status.append( _("Bad criterium '$CRITERIUM'.", mapping={'CRITERIUM': criterium})) continue #try: # ISearchCreterium['connectorName'].validate(connector) #except Exception: # self.status.append(_("Bad connector '$CONNECTOR'.", # mapping = {'CONNECTOR': connector})) # return crit.value = query if criteria_count == 0: crit.connectorName = ICriteriaChainSpecifier( fltr).first_criterium_connector_name else: crit.connectorName = connector fltr.addCriterium(crit) criteria_count += 1 # get criteria from search form extensions extensions = zope.component.getMultiAdapter( (self.context, self.request, self), interfaces.ISearchFormExtension, name='searchform-extension') extensions.update() extended_criteria = [] for extension in extensions.viewlets: if interfaces.ICriteriaReturningForm.providedBy(extension): extended_criteria += extension.getCriteria( fltr, criteria_count, self.status) for crit in extended_criteria: fltr.addCriterium(crit) criteria_count += 1 # no input if not criteria_count: self.status.append(_(u"No values provided.")) return # limit to filter target using type-field index or what ever ITypeExtent(fltr).delimit() try: query = fltr.generateQuery() result = query.searchResults() except TypeError: self.status.append( _('One of the search filters is setup improperly.')) # Return an empty result, since an error must have occurred return except parsetree.ParseError, error: self.status.append(_('Invalid search text.')) return
class SearchFormMixin(object): """ A mixin class for search forms.""" filterFactoryName = 'quotationtool.search.searcher.QuotationtoolSearchFilter' label = _('search-form-label', u"Search") prefix = 'search.' status = [] session_name = 'last' nonui_criteria = ('type-field', ) # criteria non present on the UI options_info = True filter_choice = True @property def action(self): return self.request.getURL() @property def query(self): for factory in self.getCriteriaInOrder(): if not factory[0] in self.nonui_criteria: yield factory[0] @property def criteria(self): for factory in self.getCriteriaInOrder(): if not factory[0] in self.nonui_criteria: yield factory[1]() def getCriteriaInOrder(self): fltr = zope.component.createObject(self.filterFactoryName) factories = fltr.criteriumFactories def getWeight(factory): try: desc = ICriteriumDescription(factory[1]()) except Exception: desc = None return getattr(desc, 'ui_weight', ICriteriumDescription['ui_weight'].default) return sorted(factories, cmp=lambda x, y: cmp(getWeight(x), getWeight(y))) @property def filters(self): for key, fltr in zope.component.getFactoriesFor(ISearchFilter): yield key, fltr def getLabelsAndDescriptions(self): for name, factory in self.getCriteriaInOrder(): crit = factory() try: desc = getattr(ICriteriumDescription(factory), 'description', u"") except Exception: desc = None crit_type = u"" if ITextCriterium.providedBy(crit): crit_type = _(u"Fulltext Index") yield (name, crit, desc, crit_type) def _updateForm(self): form = self.request.form criteria_count = 0 self.status = [] if form.get(self.prefix + 'button.search', u"") == 'search': fltr = zope.component.createObject( str(form.get(self.prefix + u"filter", self.filterFactoryName))) #if not fltr: # fltr = QuotationtoolSearchFilter() for i in range(len(list(self.query))): connector = form.get(self.prefix + unicode(i) + u'.connector', u"OR") criterium = form.get(self.prefix + unicode(i) + u'.criterium', u"") query = form.get(self.prefix + unicode(i) + u'.query', u"") if not (criterium and query): continue try: crit = fltr.createCriterium(criterium) except Exception, err: self.status.append( _("Bad criterium '$CRITERIUM'.", mapping={'CRITERIUM': criterium})) continue #try: # ISearchCreterium['connectorName'].validate(connector) #except Exception: # self.status.append(_("Bad connector '$CONNECTOR'.", # mapping = {'CONNECTOR': connector})) # return crit.value = query if criteria_count == 0: crit.connectorName = ICriteriaChainSpecifier( fltr).first_criterium_connector_name else: crit.connectorName = connector fltr.addCriterium(crit) criteria_count += 1 # get criteria from search form extensions extensions = zope.component.getMultiAdapter( (self.context, self.request, self), interfaces.ISearchFormExtension, name='searchform-extension') extensions.update() extended_criteria = [] for extension in extensions.viewlets: if interfaces.ICriteriaReturningForm.providedBy(extension): extended_criteria += extension.getCriteria( fltr, criteria_count, self.status) for crit in extended_criteria: fltr.addCriterium(crit) criteria_count += 1 # no input if not criteria_count: self.status.append(_(u"No values provided.")) return # limit to filter target using type-field index or what ever ITypeExtent(fltr).delimit() try: query = fltr.generateQuery() result = query.searchResults() except TypeError: self.status.append( _('One of the search filters is setup improperly.')) # Return an empty result, since an error must have occurred return except parsetree.ParseError, error: self.status.append(_('Invalid search text.')) return self.status.append(u"Found %d objects" % len(result)) session = ISearchSession(self.request) session.addFilter(self.session_name, fltr) session.addFilter(IResultSpecifier(fltr).session_name, fltr) self.request.response.redirect( IResultSpecifier(fltr).resultURL(self.context, self.request))
class IdCriterium(SearchCriterium): """ Search criterium for 'id-field' index.""" indexOrName = 'id-field' label = _('id-field-label', u"ID")
ignore_empty_criteria = True def resultURL(self, context, request): site = None for path_element in [context] + getParents(context): if IQuotationtoolSite.providedBy(path_element): site = path_element return absoluteURL(site, request) + u"/@@searchResult.html" session_name = 'any' quotationtool_search_filter_factory = zope.component.factory.Factory( QuotationtoolSearchFilter, _('QuotationtoolSearchFilter-title', u"Any content"), _('QuotationtoolSearchFilter-desc', u"Search for all content types.")) class AnyCriterium(TextCriterium): """ Full text criterium for 'any-fulltext' index.""" zope.interface.implements(interfaces.ICriteriumDescription) indexOrName = 'any-fulltext' label = _('any-fulltext-label', u"Free Text (Any Field)") description = _('any-fulltext-desc', u"Matches in any (or almost any) field.")