def get_prep_lookup(self, lookup_type, value): if lookup_type in ["gt", "gte", "lt", "lte", "exact", "iexact"]: return "%s" % escape_ldap_filter(value) elif lookup_type in ["range", "in"]: return [escape_ldap_filter(v) for v in value] raise TypeError("DateTimeField has invalid lookup: %s" % lookup_type)
def get_prep_lookup(self, lookup_type, value): if lookup_type in ['gt','gte','lt','lte','exact','iexact']: return "%s" % escape_ldap_filter(value) elif lookup_type in ['range','in'] : return [escape_ldap_filter(v) for v in value] raise TypeError("DateTimeField has invalid lookup: %s" % lookup_type)
def get_prep_lookup(self, lookup_type, value): "Perform preliminary non-db specific lookup checks and conversions" if lookup_type in 'contains': return escape_ldap_filter(value) elif lookup_type == 'startswith': return "%s*" % escape_ldap_filter(value) raise TypeError("ListField has invalid lookup: %s" % lookup_type)
def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False): "Returns field's value prepared for database lookup." if lookup_type in ['gt','gte','lt','lte','exact','iexact']: return ["%s" % escape_ldap_filter(value)] elif lookup_type in ['range','in'] : return [escape_ldap_filter(v) for v in value] raise TypeError("DateTimeField has invalid lookup: %s" % lookup_type)
def get_prep_lookup(self, lookup_type, value): "Perform preliminary non-db specific lookup checks and conversions" if lookup_type == 'exact': if not isinstance(value, bool): if (not value) or (isinstance(value, six.string_types) and value.lower() in("false", "0")): return escape_ldap_filter("FALSE") return escape_ldap_filter("TRUE") return escape_ldap_filter(value) raise TypeError("BooleanField has invalid lookup: %s" % lookup_type)
def get_prep_lookup(self, lookup_type, value): "Perform preliminary non-db specific lookup checks and conversions" if lookup_type in ["endswith", "iendswith"]: return "*%s" % escape_ldap_filter(value) elif lookup_type in ["startswith", "istartswith"]: return "%s*" % escape_ldap_filter(value) elif lookup_type in ["contains", "icontains"]: return "*%s*" % escape_ldap_filter(value) elif lookup_type in ["exact","iexact"]: return escape_ldap_filter(value) raise TypeError("ListField has invalid lookup: %s" % lookup_type)
def get_prep_lookup(self, lookup_type, value): "Perform preliminary non-db specific lookup checks and conversions" if lookup_type == 'endswith': return "*%s" % escape_ldap_filter(value) elif lookup_type == 'startswith': return "%s*" % escape_ldap_filter(value) elif lookup_type in ['contains', 'icontains']: return "*%s*" % escape_ldap_filter(value) elif lookup_type == 'exact': return escape_ldap_filter(value) elif lookup_type == 'in': return [escape_ldap_filter(v) for v in value] raise TypeError("CharField has invalid lookup: %s" % lookup_type)
def get_prep_lookup(self, lookup_type, value): "Perform preliminary non-db specific lookup checks and conversions" if lookup_type == 'endswith': return "*%s" % escape_ldap_filter(value) elif lookup_type == 'startswith': return "%s*" % escape_ldap_filter(value) elif lookup_type in ['contains', 'icontains']: return "*%s*" % escape_ldap_filter(value) elif lookup_type in ['exact', 'iexact']: return escape_ldap_filter(value) elif lookup_type == 'in': return [escape_ldap_filter(v) for v in value] raise TypeError("CharField has invalid lookup: %s" % lookup_type)
def get_db_prep_lookup(self, lookup_type, value): "Returns field's value prepared for database lookup." if lookup_type == 'endswith': return ["*%s" % escape_ldap_filter(value)] elif lookup_type == 'startswith': return ["%s*" % escape_ldap_filter(value)] elif lookup_type in ['contains', 'icontains']: return ["*%s*" % escape_ldap_filter(value)] elif lookup_type == 'exact': return [escape_ldap_filter(value)] elif lookup_type == 'in': return [escape_ldap_filter(v) for v in value] raise TypeError("CharField has invalid lookup: %s" % lookup_type)
def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False): "Returns field's value prepared for database lookup." if lookup_type == 'endswith': return ["*%s" % escape_ldap_filter(value)] elif lookup_type == 'startswith': return ["%s*" % escape_ldap_filter(value)] elif lookup_type in ['contains', 'icontains']: return ["*%s*" % escape_ldap_filter(value)] elif lookup_type == 'exact': return [escape_ldap_filter(value)] elif lookup_type == 'in': return [escape_ldap_filter(v) for v in value] raise TypeError("CharField has invalid lookup: %s" % lookup_type)
def get_prep_lookup(self, lookup_type, value): "Perform preliminary non-db specific lookup checks and conversions" if lookup_type in ('exact', 'gte', 'lte'): return value elif lookup_type == 'in': return [escape_ldap_filter(v) for v in value] raise TypeError("IntegerField has invalid lookup: %s" % lookup_type)
def query_as_ldap(query, compiler, connection): """Convert a django.db.models.sql.query.Query to a LdapLookup.""" # starting with django 1.6 we can receive empty querysets if hasattr(query, 'is_empty') and query.is_empty(): return # FIXME(rbarrois): this could be an extra Where clause filterstr = ''.join(['(objectClass=%s)' % cls for cls in query.model.object_classes]) # FIXME(rbarrois): Remove this code as part of #101 if (len(query.where.children) == 1 and not isinstance(query.where.children[0], WhereNode) and query.where.children[0].lhs.target.column == 'dn'): lookup = query.where.children[0] if lookup.lookup_name != 'exact': raise LdapDBError("Unsupported dn lookup: %s" % lookup.lookup_name) return LdapLookup( base=lookup.rhs, scope=ldap.SCOPE_BASE, filterstr='(&%s)' % filterstr, ) sql, params = compiler.compile(query.where) if sql: filterstr += '(%s)' % (sql % tuple(escape_ldap_filter(param) for param in params)) return LdapLookup( base=query.model.base_dn, scope=query.model.search_scope, filterstr='(&%s)' % filterstr, )
def _where_as_ldap(self, where): query = django_query.Query(model=FakeModel) compiler = ldapdb_compiler.SQLCompiler( query=query, connection=connections['ldap'], using=None, ) pattern, params = compiler.compile(where) return '(%s)' % (pattern % tuple(escape_ldap_filter(param) for param in params))
def get_prep_lookup(self, lookup_type, value): "Perform preliminary non-db specific lookup checks and conversions" if value not in (False, True): raise TypeError("Invalid value") if lookup_type == 'exact': if value: return escape_ldap_filter(self._group()) else: raise NotImplementedError( "Negative lookups on ACLField are not yet implemented") raise TypeError("ACLField has invalid lookup: %s" % lookup_type)
def test_escape(self): self.assertEqual(escape_ldap_filter(u'fôöbàr'), u'fôöbàr') self.assertEqual(escape_ldap_filter('foo*bar'), 'foo\\2abar') self.assertEqual(escape_ldap_filter('foo(bar'), 'foo\\28bar') self.assertEqual(escape_ldap_filter('foo)bar'), 'foo\\29bar') self.assertEqual(escape_ldap_filter('foo\\bar'), 'foo\\5cbar') self.assertEqual(escape_ldap_filter('foo\\bar*wiz'), 'foo\\5cbar\\2awiz')
def test_escape(self): self.assertEqual(escape_ldap_filter(u"fôöbàr"), u"fôöbàr") self.assertEqual(escape_ldap_filter("foo*bar"), "foo\\2abar") self.assertEqual(escape_ldap_filter("foo(bar"), "foo\\28bar") self.assertEqual(escape_ldap_filter("foo)bar"), "foo\\29bar") self.assertEqual(escape_ldap_filter("foo\\bar"), "foo\\5cbar") self.assertEqual(escape_ldap_filter("foo\\bar*wiz"), "foo\\5cbar\\2awiz")
def query_as_ldap(query, compiler, connection): """Convert a django.db.models.sql.query.Query to a LdapLookup.""" if query.is_empty(): return if query.model._meta.model_name == 'migration' and not hasattr(query.model, 'object_classes'): # FIXME(rbarrois): Support migrations return # FIXME(rbarrois): this could be an extra Where clause filterstr = ''.join(['(objectClass=%s)' % cls for cls in query.model.object_classes]) # FIXME(rbarrois): Remove this code as part of #101 if (len(query.where.children) == 1 and not isinstance(query.where.children[0], WhereNode) and query.where.children[0].lhs.target.column == 'dn'): lookup = query.where.children[0] if not isinstance(lookup, ExactLookup): # TODO: follow #101 # raise LdapDBError("Unsupported dn lookup: %s" % lookup.lookup_name) # PATCH that fixes Admin Action: delete entries kwargs = copy.copy(lookup.__dict__) kwargs.pop('bilateral_transforms') kwargs.pop('contains_aggregate') kwargs['rhs'] = kwargs['rhs'][0] lookup = ExactLookup(**kwargs) return LdapLookup( base=lookup.rhs, scope=ldap.SCOPE_BASE, filterstr='(&%s)' % filterstr, ) sql, params = compiler.compile(query.where) if sql: filterstr += '(%s)' % (sql % tuple(escape_ldap_filter(param) for param in params)) return LdapLookup( base=query.model.base_dn, scope=query.model.search_scope, filterstr='(&%s)' % filterstr, )
def query_as_ldap(query, compiler, connection): """Convert a django.db.models.sql.query.Query to a LdapLookup.""" if query.is_empty(): return if query.model._meta.model_name == 'migration' and not hasattr(query.model, 'object_classes'): # FIXME(rbarrois): Support migrations return # FIXME(rbarrois): this could be an extra Where clause filterstr = ''.join(['(objectClass=%s)' % cls for cls in query.model.object_classes]) # FIXME(rbarrois): Remove this code as part of #101 if (len(query.where.children) == 1 and not isinstance(query.where.children[0], WhereNode) and query.where.children[0].lhs.target.column == 'dn'): lookup = query.where.children[0] if lookup.lookup_name != 'exact': raise LdapDBError("Unsupported dn lookup: %s" % lookup.lookup_name) return LdapLookup( base=lookup.rhs, scope=ldap.SCOPE_BASE, filterstr='(&%s)' % filterstr, ) sql, params = compiler.compile(query.where) if sql: filterstr += '(%s)' % (sql % tuple(escape_ldap_filter(param) for param in params)) return LdapLookup( base=query.model.base_dn, scope=query.model.search_scope, filterstr='(&%s)' % filterstr, )
def get_prep_lookup(self, lookup_type, value): "Perform preliminary non-db specific lookup checks and conversions" if lookup_type == 'contains': return "*%s*" % escape_ldap_filter(value) raise TypeError("ListField has invalid lookup: %s" % lookup_type)
def get_prep_lookup(self, lookup_type, value): "Perform preliminary non-db specific lookup checks and conversions" if lookup_type == 'exact': return escape_ldap_filter(value) raise TypeError("BooleanField has invalid lookup: %s" % lookup_type)