# Copyright 2018 Brian May## This file is part of python-tldap.## python-tldap is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## python-tldap is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with python-tldap If not, see <http://www.gnu.org/licenses/>.""" Django specific database helper functions. """fromtldapimportQfromtldap.databaseimportChangeset,Database,LdapObjectClass,get_onefromtldap.django.modelsimportCountersfromtldap.exceptionsimportObjectDoesNotExistdef_check_exists(database:Database,table:LdapObjectClass,key:str,value:str):""" Check if a given LDAP object exists. """try:get_one(table,Q(**{key:value}),database=database)returnTrueexceptObjectDoesNotExist:returnFalse
[docs]defsave_account(changes:Changeset,table:LdapObjectClass,database:Database)->Changeset:""" Modify a changes to add an automatically generated uidNumber. """d={}settings=database.settingsuid_number=changes.get_value_as_single('uidNumber')ifuid_numberisNone:scheme=settings['NUMBER_SCHEME']first=settings.get('UID_FIRST',10000)d['uidNumber']=Counters.get_and_increment(scheme,"uidNumber",first,lambdan:not_check_exists(database,table,'uidNumber',n))changes=changes.merge(d)returnchanges
[docs]defsave_group(changes:Changeset,table:LdapObjectClass,database:Database)->Changeset:""" Modify a changes to add an automatically generated gidNumber. """d={}settings=database.settingsgid_number=changes.get_value_as_single('gidNumber')ifgid_numberisNone:scheme=settings['NUMBER_SCHEME']first=settings.get('GID_FIRST',10000)d['gidNumber']=Counters.get_and_increment(scheme,"gidNumber",first,lambdan:not_check_exists(database,table,'gidNumber',n))changes=changes.merge(d)returnchanges