def test_set_showroom_vars_correctly(self, browser): catalog = api.portal.get_tool('portal_catalog') base = create(Builder('dossier')) search_view = getMultiAdapter((self.portal, self.request), name="search") # Batch 1 create(Builder('document').within(base).titled(u'A Foo')) create(Builder('dossier').within(base).titled(u'B Foo')) # Batch 2 create(Builder('document').within(base).titled(u'C Foo')) create(Builder('document').within(base).titled(u'D Foo')) # Batch 3 create(Builder('dossier').within(base).titled(u'E Foo')) create(Builder('document').within(base).titled(u'F Foo')) brains = catalog({'sort_on': 'sortable_title', 'SearchableText':"Foo"}) search_view.calculate_showroom_configuration(IContentListing(brains[:2])) self.assertEqual(1, search_view.number_of_documents) self.assertEqual(0, search_view.offset) search_view.calculate_showroom_configuration(IContentListing(brains[2:4])) self.assertEqual(2, search_view.number_of_documents) self.assertEqual(0, search_view.offset) search_view.calculate_showroom_configuration(IContentListing(brains[4:])) self.assertEqual(1, search_view.number_of_documents) self.assertEqual(0, search_view.offset)
def test_set_showroom_vars_correctly(self, browser): self.login(self.regular_user, browser=browser) catalog = api.portal.get_tool('portal_catalog') search_view = getMultiAdapter((self.portal, self.request), name="search") brains = catalog({ 'portal_type': 'opengever.document.document', 'sort_on': 'sortable_title', 'SearchableText': "Foo" }) brains = [ obj2brain(obj) for obj in [self.document, self.dossier, self.subdocument] ] search_view.calculate_showroom_configuration(IContentListing(brains)) self.assertEqual(2, search_view.number_of_documents) self.assertEqual(0, search_view.offset) brains = [obj2brain(obj) for obj in [self.task, self.subdossier]] search_view.calculate_showroom_configuration(IContentListing(brains)) self.assertEqual(0, search_view.number_of_documents) self.assertEqual(0, search_view.offset)
def catalogResults(self, query, batch=True, b_size=20, b_start=0): """ Make the search on portal_catalog instead solr """ try: results = self.doSearch(query) except ParseError: return {} res_dict = {} filtered_results = [] res_dict = {'tot_results_len': results.actual_result_count} active_tab = self.context.REQUEST.form.get('filter_tab') if active_tab: filtered_results = self.doFilteredSearch(active_tab, query) else: if self.tabs_order[0] != 'all': for tab_id in self.tabs_order: filtered_results = self.doFilteredSearch(tab_id, query) if filtered_results: break filtered_infos, available_tabs = self.getFilterInfos( results, filtered_results) if filtered_results: results = IContentListing(filtered_results) else: results = IContentListing(results) if batch: results = Batch(results, b_size, b_start) res_dict['results'] = results if filtered_infos: res_dict['indexes_dict'] = filtered_infos res_dict['tabs'] = available_tabs return res_dict
def _makequery(self, query=None, batch=False, b_start=0, b_size=30, sort_on=None, sort_order=None, limit=0, brains=False): """Parse the (form)query and return using multi-adapter""" parsedquery = queryparser.parseFormquery( self.context, query, sort_on, sort_order) if not parsedquery: if brains: return [] else: return IContentListing([]) catalog = getToolByName(self.context, self.catalog_name) if batch: parsedquery['b_start'] = b_start parsedquery['b_size'] = b_size elif limit: parsedquery['sort_limit'] = limit if 'path' not in parsedquery: parsedquery['path'] = {'query': ''} parsedquery['path']['query'] = getNavigationRoot(self.context) + \ parsedquery['path']['query'] results = catalog(parsedquery) if not brains: results = IContentListing(results) if batch: results = Batch(results, b_size, b_start) return results
def _makequery(self, query=None, batch=False, b_start=0, b_size=30, sort_on=None, sort_order=None, limit=0, brains=False): """Parse the (form)query and return using multi-adapter""" parsedquery = queryparser.parseFormquery(self.context, query, sort_on, sort_order) index_modifiers = getUtilitiesFor(IParsedQueryIndexModifier) for name, modifier in index_modifiers: if name in parsedquery: new_name, query = modifier(parsedquery[name]) parsedquery[name] = query # if a new index name has been returned, we need to replace # the native ones if name != new_name: del parsedquery[name] parsedquery[new_name] = query # Check for valid indexes catalog = getToolByName(self.context, 'portal_catalog') valid_indexes = [ index for index in parsedquery if index in catalog.indexes() ] # We'll ignore any invalid index, but will return an empty set if none # of the indexes are valid. if not valid_indexes: logger.warning( "Using empty query because there are no valid indexes used.") parsedquery = {} if not parsedquery: if brains: return [] else: return IContentListing([]) if batch: parsedquery['b_start'] = b_start parsedquery['b_size'] = b_size elif limit: parsedquery['sort_limit'] = limit if 'path' not in parsedquery: parsedquery['path'] = {'query': ''} results = catalog(**parsedquery) if not brains: results = IContentListing(results) if batch: results = Batch(results, b_size, start=b_start) return results
def _makequery(self, query=None, batch=False, b_start=0, b_size=30, sort_on=None, sort_order=None, limit=0, brains=False): """Parse the (form)query and return using multi-adapter""" parsedquery = queryparser.parseFormquery( self.context, query, sort_on, sort_order) if not parsedquery: if brains: return [] else: return IContentListing([]) catalog = getToolByName(self.context, 'portal_catalog') if batch: parsedquery['b_start'] = b_start parsedquery['b_size'] = b_size elif limit: parsedquery['sort_limit'] = limit if 'path' not in parsedquery: parsedquery['path'] = {'query': ''} # The Subject field in Plone currently uses a utf-8 encoded string. # When a catalog query tries to compare a unicode string from the # parsedquery with existing utf-8 encoded string indexes unindexing # will fail with a UnicodeDecodeError. To prevent this from happening # we always encode the Subject query. # XXX: As soon as Plone uses unicode for all indexes, this code can # be removed. if 'Subject' in parsedquery: query = parsedquery['Subject']['query'] # query can be a unicode string or a list of unicode strings. if isinstance(query, unicode): parsedquery['Subject']['query'] = query.encode("utf-8") elif isinstance(query, list): # We do not want to change the collections' own query string, # therefore we create a new copy of the list. copy_of_query = list(query) # Iterate over all query items and encode them if they are # unicode strings i = 0 for item in copy_of_query: if isinstance(item, unicode): copy_of_query[i] = item.encode("utf-8") i += 1 parsedquery['Subject']['query'] = copy_of_query else: pass results = catalog(parsedquery) if not brains: results = IContentListing(results) if batch: results = Batch(results, b_size, b_start) return results
def experimentslisting(self): site_path = queryUtility(IPloneSiteRoot).getPhysicalPath() b_start = self.request.get('b_start', 0) b_size = self.request.get('b_size', 20) experiment_type = self.request.get('datasets.filter.experimenttype', None) query = { 'path': { 'query': '/'.join(site_path + (defaults.EXPERIMENTS_FOLDER_ID, )) }, 'object_provides': experiment_type, 'sort_on': 'created', 'sort_order': 'descending', # provide batch hints to catalog 'b_start': b_start, 'b_size': b_size } text = self.request.get('datasets.filter.text') if text: query['SearchableText'] = text pc = getToolByName(self.context, 'portal_catalog') results = pc.searchResults(query) from Products.CMFPlone import Batch return Batch(IContentListing(results), b_size, b_start)
def _serialize_excerpts(self, meeting, item): excerpt_data = [] docs = IContentListing(item.get_excerpt_documents(unrestricted=True)) source_dossier_excerpt = item.get_source_dossier_excerpt() meeting_dossier = self.meeting.get_dossier() for doc in docs: data = {'link': doc.render_link()} if not source_dossier_excerpt and item.has_proposal: if self.meeting.is_editable(): data['return_link'] = meeting.get_url( view='agenda_items/{}/return_excerpt?document={}'. format(item.agenda_item_id, doc.uuid())) elif source_dossier_excerpt and doc == source_dossier_excerpt: data['is_excerpt_in_source_dossier'] = True if self._can_add_task_to_meeting_dossier(): data['create_task_url'] = addTokenToUrl( '{}/++add++opengever.task.task?paths:list={}'.format( meeting_dossier.absolute_url(), doc.getPath())) excerpt_data.append(data) return excerpt_data
def _makequery(self, query=None, batch=False, b_start=0, b_size=30, sort_on=None, sort_order=None, limit=0, brains=False, custom_query=None): results = super(QueryBuilder, self)._makequery(query, batch=False, b_start=b_start, b_size=b_size, sort_on=sort_on, sort_order=sort_order, limit=limit, brains=True, custom_query=custom_query) sorting = self.request.form.get('sorting', '') # if sorting is None make it an empty list sorting = isinstance(sorting, basestring) and sorting.split(',') or [] # apply the custom sorting to the resultset according to # our sorting list positions = {j: i for i, j in enumerate(sorting)} results = sorted(results, key=lambda item: positions.get(item.UID, 999)) if not brains: results = IContentListing(results) if batch: results = Batch(results, b_size, start=b_start) return results
def get_examples(context, limit=None, osh_related_only=False): """Return ToolExample objects contained in the provided context. :param context: object where to look for tool examples. :param limit: limit the number of results :returns: A list of published tool examples, sorted by date, latest first. :rtype: ContentListing object """ path = context.getPhysicalPath() path = "/".join(path) query = { 'path': { "query": path }, 'portal_type': 'osha.campaigntoolkit.toolexample', 'review_state': 'published', 'sort_on': 'Date', 'sort_order': 'reverse' } if osh_related_only: query['OSH_related'] = True if context == context.portal_url.getPortalObject(): return IContentListing(context.portal_catalog(query)[:limit]) if not context.restrictedTraverse('@@folderListing', None): return None if limit is not None: query['sort_limit'] = limit return context.restrictedTraverse('@@folderListing')(**query)[:limit] else: return context.restrictedTraverse('@@folderListing')(**query)
def results(self, query=None, batch=True, b_size=10, b_start=0, use_content_listing=True): """ Get properly wrapped search results from the catalog. Everything in Plone that performs searches should go through this view. 'query' should be a dictionary of catalog parameters. """ if query is None: query = {} query['portal_type'] = 'llentry' # if self.request.form.get('customer','') == 'All': # del self.request.form['customer'] if batch: query['b_start'] = b_start = int(b_start) query['b_size'] = b_size query = self.filter_query(query) advanced_query = self.transform_query(query) if query is None: results = [] else: catalog = getToolByName(self.context, 'portal_catalog') try: results = catalog.evalAdvancedQuery(advanced_query) except ParseError: return [] if use_content_listing: results = IContentListing(results) if batch: results = Batch(results, b_size, b_start) return results
def recorded_incidents(self): catalog = api.portal.get_tool(name='portal_catalog') items = catalog(object_provides=IIncidentRecord.__identifier__, sort_on='modified', sort_order='reverse', limit=50)[:50] return IContentListing(items)
def results(self, query=None, batch=True, b_size=10, b_start=0): results = _orig_results(self, query, batch, b_size, b_start) if not results: return results if query is None: query = {} query = self.filter_query(query) if not 'SearchableText' in query: return results if query.get('sort_on', ''): return results if batch: items = results._sequence._basesequence else: items = results._basesequence searchtext = query.get('SearchableText', '').lower().strip() sortkey = relevance_sortkey_factory(searchtext) items = list(sorted(items, key=sortkey, reverse=True)) results = IContentListing(items) if batch: results = Batch(results, b_size, b_start) return results
def results(self, batch=True, b_size=16, b_start=0): """Return latests media on the site""" query = { 'sort_on': 'Date', 'sort_order': 'reverse', } if batch: b_start = int(b_start) text = self.request.form.get('SearchableText', '') if text: query['SearchableText'] = quote_chars(text) portal_type = self.request.form.get('portal_type', '') portal_type = self.filter_types(portal_type) query['portal_type'] = self.filter_types(portal_type) created = self.request.form.get('created', {}) if self.valid_period(created): query['created'] = created # TODO: include results in current context only results = api.content.find(**query) results = IContentListing(results) if batch: results = Batch(results, b_size, b_start) return results
def __getitem__(self, index): """`x.__getitem__(index)` <==> `x[index]` """ if isinstance(index, slice): return IContentListing( self._basesequence[index.start:index.stop:index.step]) return IContentListingObject(self._basesequence[index])
def __call__(self, **kw): query = {} query.update(**kw) query.setdefault('portal_type', 'Product') catalog = getToolByName(self.context, 'portal_catalog') results = catalog(query) return IContentListing(results)
def results(self, query=None, batch=True, b_size=10, b_start=0): """Get properly wrapped search results from the catalog. 'query' should be a dictionary of catalog parameters. """ # Disable theming for ajax requests if 'ajax' in self.request.form: del self.request.form['ajax'] self.request.response.setHeader('X-Theme-Disabled', 'True') if query is None: query = {} query['b_start'] = b_start = int(b_start) query['b_size'] = b_size query = self.filter_query(query) if query is None: results = [] else: query.update({'qt': 'hlsearch'}) catalog = getToolByName(self.context, 'portal_catalog') try: results = catalog(**query) except ParseError: logger.exception('Exception while searching') return [] except SolrException: logger.exception('Exception while searching') return [] self.solr_response = results results = IContentListing(results) if batch: results = Batch(results, b_size, b_start) return results
def results(self, query=None, batch=True, b_size=10, b_start=0): """ Get properly wrapped search results from the catalog. Everything in Plone that performs searches should go through this view. 'query' should be a dictionary of catalog parameters. """ if query is None: query = {} if batch: query['b_start'] = b_start = int(b_start) query['b_size'] = b_size query = self.filter_query(query) if query is None: results = [] else: catalog = getToolByName(self.context, 'portal_catalog') try: results = catalog(**query) except ParseError: return [] results = IContentListing(results) if batch: results = Batch(results, b_size, b_start) return results
def results(self, **kwargs): """ Faceted results """ kwargs['batch'] = False results = self.query(**kwargs) results = IContentListing(results) return results
def manually_generated_excerpts(self): docs = [ excerpt.resolve_document() for excerpt in self.model.excerpt_documents ] return IContentListing(docs)
def documents(self): """ Return containing documents and related documents """ def _get_documents(): """ Return documents in this task and subtasks """ documents = getToolByName(self.context, 'portal_catalog')( portal_type=[ 'opengever.document.document', 'ftw.mail.mail', ], path=dict(depth=2, query='/'.join(self.context.getPhysicalPath())), ) return [document.getObject() for document in documents] def _get_related_documents(): """ Return related documents in this task """ # Related documents related_documents = [] for item in self.context.relatedItems: obj = item.to_object if obj.portal_type in [ 'opengever.document.document', 'ftw.mail.mail' ]: obj._v__is_relation = True related_documents.append(obj) return related_documents # merge and sort the two different lists document_list = _get_documents() + _get_related_documents() document_list.sort(lambda a, b: cmp(b.modified(), a.modified())) return IContentListing(document_list)
def getFoldersAndImages(self): """Get folders and images""" catalog = getToolByName(self, 'portal_catalog') results = self.results(batch=False) _mapping = {'results': results, 'images': {}, 'others': []} portal_atct = getToolByName(self, 'portal_atct') image_types = getattr(portal_atct, 'image_types', []) for item in results: item_path = item.getPath() if item.isPrincipiaFolderish: query = { 'portal_type': image_types, 'path': item_path, } _mapping['images'][item_path] = IContentListing(catalog(query)) elif item.portal_type in image_types: _mapping['images'][item_path] = [ item, ] else: _mapping['others'].append(item._brain) _mapping['total_number_of_images'] = sum( map(len, _mapping['images'].values())) return _mapping
def __getslice__(self, i, j): """`x.__getslice__(i, j)` <==> `x[i:j]` Use of negative indices is not supported. No longer used in Python 3, but still part of zope.interface.interfaces.IReadSequence """ return IContentListing(self._basesequence[i:j])
def _serialize_submitted_documents(self, item): """Returns a list of html strings (the complete document link). """ if not item.has_proposal: return [] docs = IContentListing(item.proposal.resolve_submitted_documents()) return [doc.render_link() for doc in sorted(docs, key=lambda doc: doc.title_or_id().lower())]
def documents(self): return IContentListing( self.catalog([ 'opengever.document.document', 'ftw.mail.mail', ], sort_on='sortable_title', sort_order='asc')[:self.document_limit])
def _serialize_submitted_documents(self, item): """Returns a list of html strings (the complete document link). """ if not item.has_proposal: return [] docs = IContentListing(item.proposal.resolve_submitted_documents()) return [doc.render_link() for doc in docs]
def results(self, **kwargs): """ Faceted results """ kwargs['batch'] = False results = self.query(**kwargs) if isinstance(results, GeneratorType): results = [i for i in results] results = IContentListing(results) return results
def _items(self, brains): return [{ 'title': item.Title(), 'url': item.getURL(), 'description': item.Description(), 'date': self._date(item), 'end': self._end(item), 'image': self.image(item), } for item in IContentListing(brains)]
def conferenceTrack(self): results = [] for rel in self.context.conferencetrack: if rel.isBroken(): # skip broken relations continue obj = rel.to_object if api.user.has_permission('View', obj=obj): results.append(obj) return IContentListing(results)
def workshopLeaders(self): results = [] for rel in self.context.speaker: if rel.isBroken(): # skip broken relations continue obj = rel.to_object if api.user.has_permission('View', obj=obj): results.append(obj) return IContentListing(results)
def getSearchDestinationList(self, type_=None): """ Return list of destinatons """ catalog = getToolByName(self.context, 'portal_catalog') portal = api.portal.get() container_path = '/'.join(portal.getPhysicalPath()) query = (MatchGlob('Title', self.request.get('SearchableText', '') + '*') | \ MatchGlob('Description', self.request.get('SearchableText', '') + '*')) & \ Eq('path', container_path) & \ In('portal_type', self._get_container_types()) obj = lambda o: o if ISiteRoot.providedBy(o) else o.getObject() results = IContentListing(catalog.evalAdvancedQuery(query)) results = [self._info_from_content(x) for x in results if self._is_container_selectable(obj(x), type_)] results.sort(lambda x, y: cmp(x['title'], y['title'])) return simplejson.dumps(results)
def get_tree(self, uid=None, type_=None): if type_ is None: type_ = self.request.get('type_', None) catalog = getToolByName(self.context, 'portal_catalog') container_path = '' if uid is not None: container = catalog(UID=uid) if len(container) == 1: container = container[0] container_path = container.getPath() if not container_path: portal = api.portal.get() container_path = '/'.join(portal.getPhysicalPath()) query = {'portal_type': self._get_container_types(), 'path': {'query': container_path, 'depth': 1}} results = IContentListing(catalog(**query)) results = [self._info_from_content(x, type_) for x in results] results.sort(lambda x, y: cmp(x['title'], y['title'])) return results