def convert(cls, toks): crit = toks[0] # Extract attribute name. attr_name = cls.__prepare_identifier(crit.name) # Extract operator name. op_name = cls.__prepare_identifier(crit.operator) if op_name.startswith("not_"): op_name = op_name[4:] negate = True else: negate = False # Extract attribute value. if len(crit.value) == 0: raise ValueError('Criterion does not define a value.') elif len(crit.value) == 1 \ and ICollectionResource.providedBy(crit.value[0]): # pylint: disable=E1101 attr_value = crit.value[0] value_is_resource = True else: attr_value = cls.__prepare_values(crit.value) value_is_resource = False spec_gen = cls.spec_map[op_name] if op_name == CONTAINED.name or value_is_resource: spec = spec_gen(**{attr_name:attr_value}) if negate: spec = ~spec else: # Create a spec for each value and concatenate with OR. spec = cls.__make_spec(spec_gen, attr_name, attr_value, negate) return spec
def convert(cls, toks): crit = toks[0] # Extract attribute name. attr_name = cls.__prepare_identifier(crit.name) # Extract operator name. op_name = cls.__prepare_identifier(crit.operator) if op_name.startswith("not_"): op_name = op_name[4:] negate = True else: negate = False # Extract attribute value. if len(crit.value) == 0: raise ValueError('Criterion does not define a value.') elif len(crit.value) == 1 \ and ICollectionResource.providedBy(crit.value[0]): # pylint: disable=E1101 attr_value = crit.value[0] value_is_resource = True else: attr_value = cls.__prepare_values(crit.value) value_is_resource = False spec_gen = cls.spec_map[op_name] if op_name == CONTAINED.name or value_is_resource: spec = spec_gen(**{attr_name: attr_value}) if negate: spec = ~spec else: # Create a spec for each value and concatenate with OR. spec = cls.__make_spec(spec_gen, attr_name, attr_value, negate) return spec
def _contained_op(self, spec): if ICollectionResource.providedBy(spec.attr_value): # pylint:disable=E1101 # FIXME: This is a hack that allows us to query for containment # of a member in an arbitrary collection (not supported # by SQLAlchemy yet). spec.attr_name = spec.attr_name + '.id' spec.attr_value = [rc.id for rc in spec.attr_value] return self.__build(spec.attr_name, 'in_', spec.attr_value)
def is_satisfied_by(self, candidate): cand_value = self._get_candidate_value(candidate) if IMemberResource.providedBy(self.attr_value): # pylint: disable=E1101 attr_value = self.attr_value.get_entity() elif ICollectionResource.providedBy(self.attr_value): # pylint: disable=E1101 attr_value = self.attr_value.get_aggregate() else: attr_value = self.attr_value return self.operator.apply(cand_value, attr_value)
def make_proxy(self, accessor, relationship_direction, options=None): # Note: We ignore the options; if something was passed here, # it came from a parent data element data traversal proxy. url = self._data.get_url() rc = url_to_resource(url) if ICollectionResource.providedBy(rc): # pylint:disable=E1101 reg = get_current_registry() prx_fac = reg.getUtility(IDataTraversalProxyFactory) prx = prx_fac.make_proxy([mb.get_entity() for mb in rc], accessor, relationship_direction) else: prx = LinkedDomainDataTraversalProxy(rc.get_entity(), accessor, relationship_direction) return prx
def convert(cls, toks): crit = toks[0] # Extract attribute name. attr_name = cls.__prepare_identifier(crit.name) # Extract operator name. op_name = cls.__prepare_identifier(crit.operator) if op_name.startswith("not_"): op_name = op_name[4:] negate = True else: negate = False # Extract attribute value. if len(crit.value) == 0: raise ValueError('Criterion does not define a value.') # elif len(crit.value) == 1 and isinstance(crit.value, ParseResults): # # URLs - convert to resource. # url_val = crit.value # try: # rc = url_to_resource(url_val.resource) # except: # raise ValueError('Could not convert "%s" to a resource.' # % url_val.resource) # if not url_val.query == '': # if not ICollectionResource.providedBy(rc): # pylint: disable=E1101 # raise ValueError('Member resources can not have a ' # 'query string.') # rc.filter = url_val.query # attr_value = rc # value_is_resource = True elif len(crit.value) == 1 \ and ICollectionResource.providedBy(crit.value[0]): # pylint: disable=E1101 attr_value = crit.value[0] value_is_resource = True else: attr_value = cls.__prepare_values(crit.value) value_is_resource = False spec_gen = cls.spec_map[op_name] if op_name == CONTAINED.name or value_is_resource: spec = spec_gen(**{attr_name:attr_value}) if negate: spec = ~spec else: # Create a spec for each value and concatenate with OR. spec = cls.__make_spec(spec_gen, attr_name, attr_value, negate) return spec
def make_proxy(self, accessor, relationship_direction, relation_operation, options=None): # Note: We ignore the options; if something was passed here, # it came from a parent data element data traversal proxy. url = self._data.get_url() rc = url_to_resource(url) if ICollectionResource.providedBy(rc): # pylint:disable=E1101 reg = get_current_registry() prx_fac = reg.getUtility(IDataTraversalProxyFactory) prx = prx_fac.make_proxy([mb.get_entity() for mb in rc], accessor, relationship_direction, relation_operation) else: prx = LinkedDomainDataTraversalProxy(rc.get_entity(), accessor, relationship_direction, relation_operation) return prx
def __prepare_criterion(self, attr, op, val): if op is None: # (None, '$exists'): if IMemberResource.providedBy(val): # pylint: disable=E1101 attr = '%s.$id' % attr val = getattr(val.get_entity(), '_id') crit = {attr:val} # if op is None: # crit = {attr:val} # else: # crit = {attr:{op:val}} elif op == '$in': if ICollectionResource.providedBy(val): # pylint: disable=E1101 val = [getattr(mb.get_entity(), '_id') for mb in val] attr = '%s.$id' % attr crit = {attr:{op:val}} elif op == IN_RANGE.name: from_value, to_value = val crit = {'$and':[{attr:{'$gte':from_value}}, {attr:{'$lte':to_value}}]} else: crit = {attr:{op:val}} return crit