def privateQueryCatalog(self, REQUEST=None, batch=False, b_size=None, full_objects=False, **kw): if REQUEST is None: REQUEST = self.context.REQUEST b_start = REQUEST.get('b_start', 0) related = [ i for i in self.context.getRelatedItems() if self.members.checkPermission(View, i) ] if not full_objects: uids = [r.UID() for r in related] query = dict(UID=uids) related = self.catalog(query) related = LazyCat([related]) limit = self.context.getLimitNumber() max_items = self.context.getItemCount() # Batch based on limit size if b_size is unspecified if max_items and b_size is None: b_size = int(max_items) else: b_size = b_size or 20 q = self.context.buildQuery() if q is None: results = LazyCat([[]]) else: # Allow parameters to further limit existing criterias q.update(kw) if not batch and limit and max_items and \ self.context.hasSortCriterion(): q.setdefault('sort_limit', max_items) if batch: q['b_start'] = b_start q['b_size'] = b_size __traceback_info__ = (self.context, q) results = self.catalog.unrestrictedSearchResults(q) if limit and not batch: if full_objects: return related[:max_items] + [ b._unrestrictedGetObject() for b in results[:max_items - len(related)] ] return related[:max_items] + results[:max_items - len(related)] elif full_objects: results = related + LazyCat( [[b._unrestrictedGetObject() for b in results]]) else: results = related + results if batch: batch = Batch(results, b_size, int(b_start), orphan=0) return batch return results
def queryCatalog(self, REQUEST=None, batch=False, b_size=None, full_objects=False, **kw): """Invoke the catalog using our criteria to augment any passed in query before calling the catalog. """ if REQUEST is None: REQUEST = getattr(self, 'REQUEST', {}) b_start = REQUEST.get('b_start', 0) pcatalog = getToolByName(self, 'portal_catalog') mt = getToolByName(self, 'portal_membership') related = [ i for i in self.getRelatedItems() \ if mt.checkPermission(View, i) ] if not full_objects: uids = [r.UID() for r in related] query = dict(UID=uids) related = pcatalog(query) related = LazyCat([related]) limit = self.getLimitNumber() max_items = self.getItemCount() # Batch based on limit size if b_size is unspecified if max_items and b_size is None: b_size = int(max_items) else: b_size = b_size or 20 q = self.buildQuery() if q is None: results = LazyCat([[]]) else: # Allow parameters to further limit existing criterias q.update(kw) if not batch and limit and max_items and self.hasSortCriterion(): q.setdefault('sort_limit', max_items) if batch: q['b_start'] = b_start q['b_size'] = b_size __traceback_info__ = (self, q) results = pcatalog.searchResults(q) if limit and not batch: if full_objects: return related[:max_items] + \ [b.getObject() for b in results[:max_items-len(related)]] return related[:max_items] + results[:max_items - len(related)] elif full_objects: results = related + LazyCat([[b.getObject() for b in results]]) else: results = related + results if batch: batch = Batch(results, b_size, int(b_start), orphan=0) return batch return results
def __call__(self, REQUEST=None, **kw): "Searchable interface" if REQUEST is not None: perms=[] user=REQUEST.AUTHENTICATED_USER for p in self.ac_inherited_permissions(): if user.has_permission(p[0], self): perms.append(p[0]) REQUEST.set('permissions',perms) results=[] for ph in self.helpValues(): results.append(apply(getattr(ph, '__call__'), (REQUEST,) , kw)) return LazyCat(results)
def __call__(self, REQUEST=None, **kw): "Searchable interface" if REQUEST is not None: perms = [] sm = getSecurityManager() for p in self.ac_inherited_permissions(all=True): if sm.checkPermission(p[0], self): perms.append(p[0]) REQUEST.set('permissions', perms) results = [] for ph in self.helpValues(): results.append(apply(getattr(ph, '__call__'), (REQUEST, ), kw)) return LazyCat(results)
def eval(catalog, query, sortSpecs=(), withSortValues=_notPassed): '''evaluate *query* for *catalog*; sort according to *sortSpecs*. *sortSpecs* is a sequence of sort specifications. A sort spec is either a ranking spec, an index name or a pair index name + sort direction ('asc/desc'). If *withSortValues* is not passed, it is set to 'True' when *sortSpecs* contains a ranking spec; otherwise, it is set to 'False'. If *withSortValues*, the catalog brains 'data_record_score_' is abused to communicate the sort value (a tuple with one component per sort spec). 'data_record_normalized_score_' is set to 'None' in this case. ''' cat = catalog._catalog rs = _eval(query, cat) if not rs: return LazyCat(()) sortSpecs, withSortValues = _normSortSpecs(sortSpecs, withSortValues, cat) if sortSpecs or withSortValues: rs = _sort(rs, sortSpecs, withSortValues) if hasattr(rs, 'keys'): rs = rs.keys() # a TreeSet does not have '__getitem__' return LazyMap(cat.__getitem__, rs)
def _createLSeq(self, *sequences): from Products.ZCatalog.Lazy import LazyCat return LazyCat(sequences)
def lazyAdd(self, other): if isinstance(other, SolrResponse): other = LazyCat([list(other)]) return Lazy._solr_original__add__(self, other)
def _deserialize(cls, data): return LazyCat(data)
def _getEntriesFor(self, contentId, version=None, implicit=True, inside=None): """Internal method to return all entries pointing to a specified item. If a version is provided, only entries with a start version less than and a stop version greater than that version will be provided. params: contentId: key for item. string. version: version of item from 'contentId'. string in the form '1.10' or list of ints like [1,10]. optional; if not provided, no version filtering. List param is a bit faster. implicit: get implicitly included content, being content inside an included collection. boolean. inside: the collection we are inside, if any, so that entries for that collection can be returned. tuple: (contentId, version). version is as above. returns: (global entries, contextual entries), where each is a (lazy) list of SelectedContent brains """ insideId = inside and inside[0] or None insideVersion = inside and inside[1] or None if version: if type(version) in StringTypes: version = [int(x) for x in version.split('.') ] # must provide list, not tuple! if type(version) is not ListType: version = list(version) if inside: if type(insideVersion) in StringTypes: try: insideVersion = [int(x) for x in insideVersion.split('.') ] # must provide list, not tuple! except ValueError: # not an int, probably an unpublished course if insideVersion == "**new**": insideVersion = [ 1, 1 ] # just set dummy version, no course entries will be found else: raise if type(insideVersion) is not ListType: insideVersion = list(insideVersion) query = {} query['portal_type'] = 'SelectedContent' query['id'] = contentId if version: query['getVersionStart'] = {'query': [version], 'range': 'max'} query['getVersionStop'] = {'query': [version], 'range': 'min'} # Note: the version stop here is supposed to be open-ended; we do this by relying on endless entries # being a different type to sort to the end instead of the beginning. See VersionField for details. directentries = self.searchResults(**query) content_catalog = getToolByName(self, 'content').catalog collections = [ c for c in content_catalog(containedModuleIds=contentId) if c.objectId != insideId ] latest_version_str = content_catalog( objectId=contentId)[0].version # TODO: only works for published # leaving here to draw out errors if version: # Filter out any collections that don't contain this version of the module. collectionIds = [] for c in collections: pcp = c.getObject().getContainedObject(contentId) if pcp.version == 'latest': pcp_version_str = latest_version_str else: pcp_version_str = pcp.version pcp_version_tup = [int(x) for x in pcp_version_str.split('.')] if pcp_version_tup == version: collectionIds.append(c.objectId) else: collectionIds = [c.objectId for c in collections] containingentries = self.searchResults({ 'portal_type': 'SelectedContent', 'id': collectionIds, 'getImplicit': True }) allentries = directentries + containingentries if inside: query['id'] = insideId query['getVersionStart'] = { 'query': [insideVersion], 'range': 'max' } query['getVersionStop'] = { 'query': [insideVersion], 'range': 'min' } contextual = self.searchResults(**query) else: contextual = LazyCat([]) return (allentries, contextual)
def my_catalog(request, **kwargs): calls.append(kwargs) return LazyCat([])