def test_or_aggregate(self): filt = Filter.OR([ Filter.attribute('givenName').equal_to('bilbo'), Filter.attribute('sn').equal_to('baggens') ]) string = filt.to_string() assert string == '(|(givenName=bilbo)(sn=baggens))'
def filter(self, *args, **kwargs): """ If there are positional arguments, they must all be F() objects. This allows us to preconstruct an unbound filter and then use it later. Of the Django filter suffixes we support the following: * ``__iexact`` * ``__istartswith`` * ``__iendswith`` * ``__icontains`` * ``__in`` Note that only the case insensitive versions of these filters are supported. LDAP searches are case insensitive, so we make you use ``__i*`` versions to remind you of that. We do support one additional LDAP specific filter: ``__exists``. This will cause a filter to be added that just ensures that the returned objects have the associated attribute. Unlike in SQL, where every row contains all available columns, in LDAP, attributes can be absent entirely from the record. """ steps = self.__validate_positional_args(args) for key, value in kwargs.items(): if isinstance(value, str): value = value.strip() if key.endswith('__istartswith'): steps.append(Filter.attribute(self.get_attribute(key[:-13])).starts_with(value)) elif key.endswith('__iendswith'): steps.append(Filter.attribute(self.get_attribute(key[:-11])).ends_with(value)) elif key.endswith('__icontains'): steps.append(Filter.attribute(self.get_attribute(key[:-11])).contains(value)) elif key.endswith('__in'): if not isinstance(value, list): raise ValueError('When using the "__in" filter you must supply a list') in_steps = [] for v in value: in_steps.append(Filter.attribute(self.get_attribute(key[:-4])).equal_to(v)) steps.append(Filter.OR(in_steps)) elif key.endswith('__exists'): # This one doesn't exist as a Django field lookup steps.append(Filter.attribute(self.get_attribute(key[:-8])).present()) elif key.endswith('__iexact') or '__' not in key: # no suffix means do an __exact if '__' in key: key = key[:-8] if value is None: # If value is None, we search for the absence of that # attribute steps.append(Filter.NOT(Filter.attribute(self.get_attribute(key)))) else: steps.append(Filter.attribute(self.get_attribute(key)).equal_to(value)) else: raise F.UnknownSuffix('The search filter "{}" uses an unknown filter suffix') self.chain.append(Filter.AND(steps)) return self
def test_or_aggregate(self): filt = Filter.OR([ Filter.attribute('firstName').equal_to('Alice'), Filter.attribute('lastName').ends_with('Chains') ]) assert filt.match({'firstName': 'Alice', 'lastName': 'Chains'}) assert filt.match({'firstName': 'Alice', 'lastName': 'In-Chains'}) assert filt.match({'firstName': 'Bob', 'lastName': 'Chains'}) assert filt.match({'firstName': 'Alice'}) assert not filt.match({'firstName': 'Bob', 'lastName': 'Smith'}) assert not filt.match({'firstName': 'Bob'}) assert not filt.match({})
def __or__(self, other): self.chain = Filter.OR([self._filter, other._filter]) return F(manager=self.manager, f=self)