forked from Github-Mirrors/canaille
refactor: move LDAPObjectQuery in its own file
This commit is contained in:
parent
222d7676ca
commit
93fa708b1c
2 changed files with 54 additions and 50 deletions
|
@ -1,6 +1,4 @@
|
|||
import itertools
|
||||
import typing
|
||||
from collections.abc import Iterable
|
||||
|
||||
import ldap.dn
|
||||
import ldap.filter
|
||||
|
@ -9,6 +7,7 @@ from ldap.controls.readentry import PostReadControl
|
|||
from canaille.backends.models import BackendModel
|
||||
|
||||
from .backend import Backend
|
||||
from .ldapobjectquery import LDAPObjectQuery
|
||||
from .utils import cardinalize_attribute
|
||||
from .utils import ldap_to_python
|
||||
from .utils import listify
|
||||
|
@ -60,54 +59,6 @@ class LDAPObjectMetaclass(type):
|
|||
cls.ldap_to_python_class[oc] = cls
|
||||
|
||||
|
||||
class LDAPObjectQuery:
|
||||
def __init__(self, klass, items):
|
||||
self.klass = klass
|
||||
self.items = items
|
||||
|
||||
def __len__(self):
|
||||
return len(self.items)
|
||||
|
||||
def __getitem__(self, item):
|
||||
if isinstance(item, slice):
|
||||
return (self.decorate(obj[1]) for obj in self.items[item])
|
||||
|
||||
return self.decorate(self.items[item][1])
|
||||
|
||||
def __iter__(self):
|
||||
return (self.decorate(obj[1]) for obj in self.items)
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Iterable):
|
||||
return all(
|
||||
a == b
|
||||
for a, b in itertools.zip_longest(
|
||||
iter(self), iter(other), fillvalue=object()
|
||||
)
|
||||
)
|
||||
return super().__eq__(other)
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self.items)
|
||||
|
||||
def decorate(self, args):
|
||||
klass = self.guess_class(self.klass, args["objectClass"])
|
||||
obj = klass()
|
||||
obj.state = args
|
||||
obj.exists = True
|
||||
return obj
|
||||
|
||||
def guess_class(self, klass, object_classes):
|
||||
if klass == LDAPObject:
|
||||
models = [
|
||||
LDAPObjectMetaclass.ldap_to_python_class[oc.decode()]
|
||||
for oc in object_classes
|
||||
if oc.decode() in LDAPObjectMetaclass.ldap_to_python_class
|
||||
]
|
||||
return models[0]
|
||||
return klass
|
||||
|
||||
|
||||
class LDAPObject(BackendModel, metaclass=LDAPObjectMetaclass):
|
||||
_object_class_by_name = None
|
||||
_attribute_type_by_name = None
|
||||
|
|
53
canaille/backends/ldap/ldapobjectquery.py
Normal file
53
canaille/backends/ldap/ldapobjectquery.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
import itertools
|
||||
from collections.abc import Iterable
|
||||
|
||||
|
||||
class LDAPObjectQuery:
|
||||
def __init__(self, klass, items):
|
||||
self.klass = klass
|
||||
self.items = items
|
||||
|
||||
def __len__(self):
|
||||
return len(self.items)
|
||||
|
||||
def __getitem__(self, item):
|
||||
if isinstance(item, slice):
|
||||
return (self.decorate(obj[1]) for obj in self.items[item])
|
||||
|
||||
return self.decorate(self.items[item][1])
|
||||
|
||||
def __iter__(self):
|
||||
return (self.decorate(obj[1]) for obj in self.items)
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Iterable):
|
||||
return all(
|
||||
a == b
|
||||
for a, b in itertools.zip_longest(
|
||||
iter(self), iter(other), fillvalue=object()
|
||||
)
|
||||
)
|
||||
return super().__eq__(other)
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self.items)
|
||||
|
||||
def decorate(self, args):
|
||||
klass = self.guess_class(self.klass, args["objectClass"])
|
||||
obj = klass()
|
||||
obj.state = args
|
||||
obj.exists = True
|
||||
return obj
|
||||
|
||||
def guess_class(self, klass, object_classes):
|
||||
from .ldapobject import LDAPObject
|
||||
from .ldapobject import LDAPObjectMetaclass
|
||||
|
||||
if klass == LDAPObject:
|
||||
models = [
|
||||
LDAPObjectMetaclass.ldap_to_python_class[oc.decode()]
|
||||
for oc in object_classes
|
||||
if oc.decode() in LDAPObjectMetaclass.ldap_to_python_class
|
||||
]
|
||||
return models[0]
|
||||
return klass
|
Loading…
Reference in a new issue