def rules(self, context, date=None, user='', group=''): context_uid = uuid.UUID(IUUID(context)) query = Eq('context_uid', context_uid) & Eq('category', self.category) if date is not None: query = query & Le('valid_from', date) & Ge('valid_to', date) if self.for_attribute == FOR_USER: if group: msg = u'``group`` keyword must not be given if scope is user' raise ValueError(msg) if user: query = query & Eq('user', user) else: query = query & NotEq('user', '') elif self.for_attribute == FOR_GROUP: if user: msg = u'``user`` keyword must not be given if scope is group' raise ValueError(msg) if group: query = query & Eq('group', group) else: query = query & NotEq('group', '') else: if user or group: msg = u'``user`` and ``group`` keywords must not be given ' +\ u'if scope is general' raise ValueError(msg) query = query & Eq('user', '') & Eq('group', '') return self.rules_soup.query(query, sort_index='valid_from', reverse=True)
def _get_meetings_that_could_be_copied(request, context): """ Returns any meetings that could be copied (in one or another way) in this context. - If meeting is context, make sure that isn't included - Make sure the logged in user has the moderate meeting perm. """ docids = request.root.catalog.query( Eq('type_name', 'Meeting') & NotEq('uid', context.uid), sort_index='sortable_title', )[1] return request.resolve_docids(docids, perm=security.MODERATE_MEETING)
def test_negate(self): from repoze.catalog.query import NotEq inst = self._makeOne('index', 'val') self.assertEqual(inst.negate(), NotEq('index', 'val'))
def vocabulary_view(context, request): try: attributes = json.loads(request.params.get('attributes', '["title", "id"]')) except: attributes = ['title', 'id'] if 'UID' in attributes: # always put in anyways attributes.remove('UID') try: batch = json.loads(request.params.get('batch')) except: batch = DEFAULT_BATCH query = normalize_query(json.loads(request.params['query'])) criteria = parse_query(query) resolver = ResovlerFactory(context) if 'UID' in query: docids = query['UID'] if type(docids) not in (list, tuple): docids = [docids] # convert to ints new_docids = [] for docid in docids: try: new_docids.append(int(docid)) except: pass docids = new_docids numdocs = len(docids) else: criteria.append(Any('allowed', effective_principals(request))) if 'title' not in query: # we default to requiring a title in these results or # else we get a bunch of junky results criteria.append(NotEq('title', '')) catalog = find_catalog(context) numdocs, docids = catalog.query(And(*criteria)) if batch and ('size' not in batch or 'page' not in batch): batch = DEFAULT_BATCH if batch: # must be slicable for batching support page = int(batch['page']) # page is being passed in is 1-based start = (max(page - 1, 0)) * int(batch['size']) end = start + int(batch['size']) # Try __getitem__-based slice, then iterator slice. # The iterator slice has to consume the iterator through # to the desired slice, but that shouldn't be the end # of the world because at some point the user will hopefully # give up scrolling and search instead. try: docids = docids[start:end] except TypeError: docids = itertools.islice(docids, start, end) # build result items items = [] for docid in docids: result = resolver(docid) if result is None: continue data = { 'UID': docid } for attribute in attributes: attr = attribute if attribute in _attribute_mapping: attr = _attribute_mapping[attribute] if attr in ('Type', 'portal_type'): value = 'Page' if IImage.providedBy(result): value = 'Image' elif ICommunityFile.providedBy(result): value = 'File' elif IFolder.providedBy(result): value = 'Folder' elif attr == 'getURL': value = resource_url(result, request) elif attr == 'path': # a bit weird here... value = resource_path(result, request).split('/GET')[0] else: value = getattr(result, attr, None) data[attribute] = value items.append(data) return { 'results': items, 'total': numdocs }
def select(cls, attempt=0, sort_index=None, reverse=False, limit=None, *args, **kwargs): catalog = cls._get_catalog() qo = None for key in kwargs: key_parts = key.split('__', 1) original_key = key key = key_parts[0] if key not in catalog.keys(): print catalog.keys() raise NoIndex( 'The field %s is not in the list of indexed fields for %s' % (key, cls.__name__)) value = kwargs[original_key] if isinstance(value, ZODBModel): value = unicode(value) if len(key_parts) == 2: if key_parts[1] == 'gt': nqo = Gt(key, value) elif key_parts[1] == 'lt': nqo = Lt(key, value) elif key_parts[1] == 'gte': nqo = Ge(key, value) elif key_parts[1] == 'lte': nqo = Le(key, value) elif key_parts[1] == 'contains': nqo = Contains(key, value) elif key_parts[1] == 'ncontains': nqo = DoesNotContain(key, value) elif key_parts[1] == 'ne': nqo = NotEq(key, value) elif key_parts[1] == 'in': nqo = Any(key, value) elif key_parts[1] == 'nin': nqo = NotAny(key, value) else: raise Exception("Unknown comparator %s" % (key_parts[1])) else: nqo = Eq(key, value) if qo: qo = qo & nqo else: qo = nqo root = cls._get_root() if qo: _, results = catalog.query(qo, sort_index=sort_index, reverse=reverse, limit=limit) else: _, results = catalog.sort_result(root.keys(), sort_index=sort_index, reverse=reverse, limit=limit) try: return [root[x] for x in results] except KeyError, e: if attempt < 2: cls.index() return cls.select(attempt=attempt + 1, *args, **kwargs) raise e