class IRERSiteSearchIndexesSettings(Interface): """Settings used in the control panel for sitesearch: Indexes to show """ available_indexes = schema.Tuple( title=_(u'Indexes for search'), description=_( 'help_sitesearch_available_indexes', default= u"Insert a list of indexes that should be used for faceted navigation." ), value_type=PersistentObject(IIndexesValueField, title=_(u"Index in search")), required=False, default=(), missing_value=()) indexes_order = schema.Tuple( title=_(u'Indexes order'), description=_( 'help_indexes_order', default= u"Select the indexes that you want to show for faceted navigation. You can define also the order. \"All\" tab shows all results." ), value_type=schema.Choice( vocabulary=u"rer.sitesearch.vocabularies.SearchIndexesVocabulary"), required=False, default=("Subject", ), missing_value=())
class IRERSiteSearchTabsSettings(Interface): """Settings used in the control panel for sitesearch: Tab list """ tabs_mapping = schema.Tuple( title=_(u'Search tabs'), description=_( 'help_tabs_mapping', default= u"Insert a list of tabs to show on search results. Each tab can contain different content_types." ), value_type=PersistentObject(ITabsValueField, title=_(u"Search tab")), required=False, default=(), missing_value=()) tabs_order = schema.Tuple( title=_(u'Tabs order'), description=_( 'help_tabs_order', default= u"Select the tabs that you want to show in the search and define an order." ), value_type=schema.Choice( vocabulary=u"rer.sitesearch.vocabularies.SearchTabsVocabulary"), required=False, default=("All", ), missing_value=())
def validateSearchableText(self, key, text): """Check if SearchableText is too long or has too long words""" validation_messages = [] max_word_len = self.getRegistryInfos('max_word_len') max_words = self.getRegistryInfos('max_words') words = text.split() if len(words) > max_words: validation_messages.append( translate(_( 'search_limit_words_label', default= u'"${word}" (and any subsequent words) was ignored because we limit queries to ${max_words} words.', mapping={ 'word': words[max_words].decode('utf-8'), 'max_words': max_words }), context=self.request)) words = words[:max_words] text = " ".join(words) for word in words: if len(word) > max_word_len: validation_messages.append( translate(_( 'search_limit_word_characters_label', default= u'"${word}" is a too long word and was ignored. Try using a shorter word.', mapping={'word': word.decode('utf-8')}), context=self.request)) text = text.replace(word, '') #fix whitespaces text = quote_chars(' '.join(text.split())) self.request.form[key] = text return text, validation_messages
def sort_options(self): """ Sorting options for search results view. """ return ( SortOption(self.request, _(u'relevance'), ''), SortOption(self.request, _(u'date (newest first)'), 'Date', reverse=True), SortOption(self.request, _(u'alphabetically'), 'sortable_title'), )
class IIndexesValueField(Interface): index_title = schema.ASCIILine(title=_("sitesearch_index_title", default=u"Index title"), description=_("sitesearch_index_title_help", default=u"Insert a title for this search filter. You can provide translations for this title with a proper translation file for rer.sitesearch domain."), required=True) index = schema.Choice( title=_("sitesearch_allowable", default=u"Allowable indexes in catalog"), description=_("sitesearch_allowable_help", default=u'Select an index.'), required=True, vocabulary=u"rer.sitesearch.vocabularies.IndexesVocabulary", )
class ITabsValueField(Interface): tab_title = schema.ASCIILine(title=_("sitesearch_tab_title", default=u"Tab title"), description=_("sitesearch_tab_title_help", default=u"Insert a title for this tab. You can provide translations for this title with a proper translation file for rer.sitesearch domain."), required=True) portal_types = schema.Tuple( title=_("sitesearch_tab_portal_types", default=u"Portal types"), description=_("sitesearch_tab_portal_types_help", default=u'Select which portal_types shows on this tab.'), required=True, value_type=FOLDER_TYPES_VALUESTYPE, )
class IRERSiteSearchHiddensIndexesSettings(Interface): """Settings used in the control panel for sitesearch: Hidden indexes """ hidden_indexes = schema.Tuple( title=_(u'Hidden indexes'), description=_( 'help_sitesearch_hidden_indexes', default= u"Insert a list of indexes that can't be used for faceted search, but we need to keep." ), value_type=PersistentObject(IIndexesValueField, title=_(u"Hidden indexes")), required=False, default=(), missing_value=())
def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage( _(u"Edit cancelled"), "info" ) self.request.response.redirect( "%s/%s" % (self.context.absolute_url(), self.control_panel_view) )
def getResultsLen(self, results_dict): """ Return the updated and translated len results. @param: results_dict """ results = results_dict.get('results', None) if not results: return 0 results_len = results.sequence_length total_len = results_dict.get('tot_results_len', results_len) if not results_len and not total_len: return 0 if results_len == total_len: return str(results_len) else: return translate( _( "${results_len} on ${total_len}", mapping={ 'results_len': results_len, 'total_len': total_len, }, ), context=self.request, )
def handleSave(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return changes = self.applyChanges(data) IStatusMessage(self.request).addStatusMessage(_(u"Changes saved"), "info") self.context.REQUEST.RESPONSE.redirect("@@sitesearch-settings")
class RERSiteSearchSettingsEditForm(controlpanel.RegistryEditForm): """Media settings form. """ schema = IRERSiteSearchSettings groups = (FormGeneral, FormTabs, FormIndexes, FormHiddenIndexes) id = "RERSiteSearchSettingsEditForm" label = _(u"Site Search settings") description = _( u"help_sitesearch_settings_editform", default=u"Set site search configurations", ) def updateWidgets(self): """ Hide some fields """ super(RERSiteSearchSettingsEditForm, self).updateWidgets() qi = api.portal.get_tool(name='portal_quickinstaller') if not qi.isProductInstalled('collective.solr'): # hide solr enable setting, if collective.solr isn't installed for group in self.groups: solr_search = group.fields.get('solr_search_enabled') if solr_search: solr_search.mode = HIDDEN_MODE @button.buttonAndHandler(_('Save'), name='save') def handleSave(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return self.applyChanges(data) IStatusMessage(self.request).addStatusMessage(_(u"Changes saved"), "info") self.context.REQUEST.RESPONSE.redirect("@@sitesearch-settings") @button.buttonAndHandler(_('Cancel'), name='cancel') def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage(_(u"Edit cancelled"), "info") self.request.response.redirect( "%s/%s" % (self.context.absolute_url(), self.control_panel_view))
def tabs_mapping(self): tabs_map = self.getRegistryInfos('tabs_mapping') tabs_dict = {'all': {'title': _(u'All')}} for tab in tabs_map: tab_title = tab.tab_title tab_id = tab_title.lower().replace(' ', '-') tabs_dict[tab_id] = { 'title': tab_title, 'portal_types': tab.portal_types, } return tabs_dict
class IRERSiteSearchGeneralSettings(Interface): """Settings used in the control panel for sitesearch: General settings """ max_word_len = schema.Int( title=_(u'Maximum number of characters in a single word'), description=_( 'help_max_word_len', default= u"Set what is the maximum length of a single search word. Longer words will be omitted from the search." ), default=128, required=False, ) max_words = schema.Int( title=_(u'Maximum number of words in search query'), description=_( 'help_max_words', default= u"Set what is the maximum number of words in the search query. The other words will be omitted from the search." ), default=32, required=False, ) solr_search_enabled = schema.Bool( title=_(u'Enable search with SOLR'), description=_( 'help_solr_search_enabled', default= u"If enabled and collective.solr is correctly set, SOLR search engine will be used instead Plone catalog." ), required=False, )
def filter_query(self): """ Make some query filtering. """ request = self.request query = {} validation_messages = [] # text = request.form.get('SearchableText', '') valid_keys = self.valid_keys + tuple(self.catalog.indexes()) for k, v in request.form.items(): if k == 'SearchableText': v, text_validation = self.validateSearchableText(k, v) validation_messages.extend(text_validation) if v: query[k] = self.setFilteredIndex(k, v, valid_keys) if not query: validation_messages.append( translate( _( 'search_no_query_label', default=u'You need to pass some query value.', ), context=self.request, )) return query, validation_messages # don't filter on created at all if we want all results created = query.get('created') if created: if created.get('query'): if created['query'][0] <= EVER: del query['created'] # respect `types_not_searched` setting types = query.get('portal_type', []) if 'query' in types: types = types['query'] query['portal_type'] = self.filter_types(types) # respect effective/expiration date query['show_inactive'] = False # respect navigation root if 'path' not in query: query['path'] = getNavigationRoot(self.context) return query, validation_messages
def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage(_(u"Edit cancelled"), "info") self.request.response.redirect( "%s/%s" % (self.context.absolute_url(), self.control_panel_view))
class FormHiddenIndexes(group.Group): label = _(u"Hidden indexes") fields = field.Fields(IRERSiteSearchHiddensIndexesSettings)
class FormIndexes(group.Group): label = _(u"Indexes") fields = field.Fields(IRERSiteSearchIndexesSettings)
class FormTabs(group.Group): label = _(u"Tabs") fields = field.Fields(IRERSiteSearchTabsSettings)
class FormGeneral(group.Group): label = _(u"General settings") fields = field.Fields(IRERSiteSearchGeneralSettings)