flask_open_directory.query package¶
Submodules¶
flask_open_directory.query.base_query module¶
-
class
flask_open_directory.query.base_query.
BaseQuery
(open_directory=None, model=None, search_base=None, search_filter=None, connection=None, ldap_attributes=None)[source]¶ Bases:
flask_open_directory.query.query_abc.QueryABC
Implementation of
QueryABC
abstract class. This is used to search an ldap connection and convert the entries into aModelABC
subclass.This also adds a helpful context manager
BaseQuery.connection_ctx()
, which is helpful if you do not have a flask application running, it will try to create a connection with theopen_directory
set on the instance for the search.-
all
(connection=None, convert=True)[source]¶ Query the connection and return a tuple of all items found for the current state of the query. This will return an empty tuple if a connection was not established or there was no entries to match the filter criteria.
Parameters: - connection (
Optional
[Connection
]) – An optionalldap3.Connection
to use for the search. - convert – Whether to convert the
ldap3.Entry
‘s to themodel
set on a class. Default isTrue
Return type: Tuple
[Any
]- connection (
-
connection
¶ An
ldap3.Connection
for the query. If this is not set explicitly, then we will see if anopen_directory
is set on the instance and return it’sconnection
, which could still beNone
, if there is no flask application running.If you don’t know wheter you are working inside of the application context or not, then it is safer to use the
BaseQuery.connection_ctx()
method.Raises: TypeError – If explicitly setting this to something other than an ldap3.Connection
.Return type: ContextManager
[]
-
connection_ctx
()[source]¶ A context manager that tries to find a connection to use. If a connection is found on the instance or the
open_directory
of an instance, then that will be used. If not it will try to create one using an instancesopen_directory
.If a connection is not able to be found this will yield
None
, so you should check before using the connection.Example:
>>> with query.connection_ctx() as conn: ... model = query.first(conn)
Return type: Optional
[Connection
]
-
first
(connection=None, convert=True)[source]¶ Query the connection and return the first item found for the current state of the query. This will return
None
if a connection was not established or there was no entry to match the filter criteria.Parameters: - connection (
Optional
[Connection
]) – An optionalldap3.Connection
to use for the search. - convert – Whether to convert the
ldap3.Entry
to themodel
set on a class. Default isTrue
Return type: Any
- connection (
-
ldap_attributes
¶ The ldap attributes to return with the query. These should be a list/tuple of strings that are attributes in the ldap database.
If none have been set explicitly on an instance, then we will check if there is a
model
, and use it’sldap_keys
for this value.Return type: Union
[None
,Iterable
[str
]]
-
model
¶ A
ModelABC
subclass used for search criterea and to in converting returned entries into the desired model.The attribute can be set with either a class or an instance of a class, but we always store a reference to the class.
Return type: Any
-
open_directory
¶ A
OpenDirectoryABC
subclass used for the connection for the query.Return type: Any
-
search_base
¶ A string representing where to search.
This property will try to derive this value from several places. If it set explicitly, then that will be the preferred return value.
If it is not set explicitly, then we will check if there is an
OpenDirectory
set for the instance. If so we will use thebase_dn
property from theopen_directory
. We will further check if amodel
has been set for the instance and we will add it’squery_cn
.Return type: Optional
[str
]
-
search_filter
¶ The ldap filter string for the query. If not set then we will return the class’s
_default_search_filter
which is ‘(objectClass=*)’Return type: str
-
flask_open_directory.query.query_abc module¶
-
class
flask_open_directory.query.query_abc.
QueryABC
[source]¶ Bases:
object
This
abc
has all the methods that should be implemented for an object to be considered a validQuery
subclass.All of the methods must be implemented to create a subclass, or to pass an
isinstance
orissubclass
check.-
all
(connection=None)[source]¶ Searches the ldap connection returning tuple of all the entries found. Or an empty tuple.
Parameters: connection ( Optional
[Connection
]) – An optional connection to use for the search.Return type: Tuple
[Any
]
-
connection
¶ Return the ldap connection for the query.
Return type: Union
[None
,Connection
]
-
first
(connection=None)[source]¶ Searches the ldap connection returning the first entry, if any are found or
None
if not.Parameters: connection ( Optional
[Connection
]) – An optional connection to use for the search.Return type: Any
-
ldap_attributes
¶ Return the ldap attributes to return for the query.
Return type: Iterable
[str
]
-
search_base
¶ Return the ldap serach base dn string for the query.
Return type: str
-
search_filter
¶ Return the ldap search filter string for the query.
Return type: str
-
Module contents¶
-
class
flask_open_directory.query.
QueryABC
[source]¶ Bases:
object
This
abc
has all the methods that should be implemented for an object to be considered a validQuery
subclass.All of the methods must be implemented to create a subclass, or to pass an
isinstance
orissubclass
check.-
all
(connection=None)[source]¶ Searches the ldap connection returning tuple of all the entries found. Or an empty tuple.
Parameters: connection ( Optional
[Connection
]) – An optional connection to use for the search.Return type: Tuple
[Any
]
-
connection
¶ Return the ldap connection for the query.
Return type: Union
[None
,Connection
]
-
first
(connection=None)[source]¶ Searches the ldap connection returning the first entry, if any are found or
None
if not.Parameters: connection ( Optional
[Connection
]) – An optional connection to use for the search.Return type: Any
-
ldap_attributes
¶ Return the ldap attributes to return for the query.
Return type: Iterable
[str
]
-
search_base
¶ Return the ldap serach base dn string for the query.
Return type: str
-
search_filter
¶ Return the ldap search filter string for the query.
Return type: str
-
-
class
flask_open_directory.query.
Query
(open_directory=None, model=None, search_base=None, search_filter=None, connection=None, ldap_attributes=None)[source]¶ Bases:
flask_open_directory.query.base_query.BaseQuery
Extends the
BaseQuery
with some helper methods. The helpers typically return the query when called for method chaining.This allows a query to be mostly setup by another object, and the caller provide the criteria for the search.
Example:
>>> query = Query(open_directory=OpenDirectory()) >>> query(User).filter(username='testuser').first() User(...) >>> query.all() # reuse the same criteria, only get all the users [User(...), ...] >>> q = query(Group) # change the ``model`` on a query >>> q == query True >>> q.filter(group_name='testgroup').first() Group(...)
-
filter
(*args, **kwargs)[source]¶ Set’s the search filter for an instance, returning the query for method chaining.
This method will accept a string or keyword arguments as parameters.
If there is a string passed in it is set as the filter on the instance. There is no parsing or validation done on a string if it’s passed in, which can cause errors when performing the query if it is an invalid filter string.
If there is no string passed in then keyword arguments are parsed with the current
model
set on an instance. This means that you can build a query using either the model’s attribute name or the ldap entry key.Example: >>> from open_directory_utils.model import User >>> from open_directory_utils.query import Query >>> q = Query(search_base='dc=example,dc=com', model=User) >>> q.filter(username='testuser') Query(...) >>> print(q.search_filter) '(uid=testuser)' >>> q.filter(uid='anotheruser') Query(...) >>> print(q.search_filter) '(uid=anotheruser)' >>> q.filter('(cn='Test User')') >>> print(q.search_filter) '(cn=Test User)'
Return type:
-
-
class
flask_open_directory.query.
BaseQuery
(open_directory=None, model=None, search_base=None, search_filter=None, connection=None, ldap_attributes=None)[source]¶ Bases:
flask_open_directory.query.query_abc.QueryABC
Implementation of
QueryABC
abstract class. This is used to search an ldap connection and convert the entries into aModelABC
subclass.This also adds a helpful context manager
BaseQuery.connection_ctx()
, which is helpful if you do not have a flask application running, it will try to create a connection with theopen_directory
set on the instance for the search.-
all
(connection=None, convert=True)[source]¶ Query the connection and return a tuple of all items found for the current state of the query. This will return an empty tuple if a connection was not established or there was no entries to match the filter criteria.
Parameters: - connection (
Optional
[Connection
]) – An optionalldap3.Connection
to use for the search. - convert – Whether to convert the
ldap3.Entry
‘s to themodel
set on a class. Default isTrue
Return type: Tuple
[Any
]- connection (
-
connection
¶ An
ldap3.Connection
for the query. If this is not set explicitly, then we will see if anopen_directory
is set on the instance and return it’sconnection
, which could still beNone
, if there is no flask application running.If you don’t know wheter you are working inside of the application context or not, then it is safer to use the
BaseQuery.connection_ctx()
method.Raises: TypeError – If explicitly setting this to something other than an ldap3.Connection
.Return type: ContextManager
[]
-
connection_ctx
()[source]¶ A context manager that tries to find a connection to use. If a connection is found on the instance or the
open_directory
of an instance, then that will be used. If not it will try to create one using an instancesopen_directory
.If a connection is not able to be found this will yield
None
, so you should check before using the connection.Example:
>>> with query.connection_ctx() as conn: ... model = query.first(conn)
Return type: Optional
[Connection
]
-
first
(connection=None, convert=True)[source]¶ Query the connection and return the first item found for the current state of the query. This will return
None
if a connection was not established or there was no entry to match the filter criteria.Parameters: - connection (
Optional
[Connection
]) – An optionalldap3.Connection
to use for the search. - convert – Whether to convert the
ldap3.Entry
to themodel
set on a class. Default isTrue
Return type: Any
- connection (
-
ldap_attributes
¶ The ldap attributes to return with the query. These should be a list/tuple of strings that are attributes in the ldap database.
If none have been set explicitly on an instance, then we will check if there is a
model
, and use it’sldap_keys
for this value.Return type: Union
[None
,Iterable
[str
]]
-
model
¶ A
ModelABC
subclass used for search criterea and to in converting returned entries into the desired model.The attribute can be set with either a class or an instance of a class, but we always store a reference to the class.
Return type: Any
-
open_directory
¶ A
OpenDirectoryABC
subclass used for the connection for the query.Return type: Any
-
search_base
¶ A string representing where to search.
This property will try to derive this value from several places. If it set explicitly, then that will be the preferred return value.
If it is not set explicitly, then we will check if there is an
OpenDirectory
set for the instance. If so we will use thebase_dn
property from theopen_directory
. We will further check if amodel
has been set for the instance and we will add it’squery_cn
.Return type: Optional
[str
]
-
search_filter
¶ The ldap filter string for the query. If not set then we will return the class’s
_default_search_filter
which is ‘(objectClass=*)’Return type: str
-