What's the best way to replace value of attribute which can handle multiple values?

Zhang Huangbin zhbmaillistonly at gmail.com
Wed Sep 9 11:22:07 CEST 2009


Thanks for your reply, Michael. :)

On Sep 9, 2009, at 4:54 PM, Michael Ströder wrote:
>> If cn=cn2 is not exist, [(ldap.MOD_DELETE, 'cn', 'cn2')] will raise  
>> an
>> error.
>
> Could you please post the error raised and mention with which server  
> you're
> testing?
>
> I'd try [(ldap.MOD_DELETE, 'cn',['cn2'])]

The same error if cn=cn2 not exist: ldap.NO_SUCH_ATTRIBUTE.

>
>> If cn=cn2 is not exist, [(ldap.MOD_ADD, 'cn', 'cn4')] will add  
>> cn=cn4,
>> but can't delete 'cn=cn2'.
>
> Try this: [(ldap.MOD_ADD, 'cn',['cn4'])]

It can't delete cn=cn2. And same error raised while cn=cn4 exists:  
ldap.TYPE_OR_VALUE_EXISTS.

I created a function to perform similar request moment ago, but still  
looking for a better way:

-------
class LDAPWrap:
     def __init__(self):
         ... skip some lines ...
         self.conn = ldap.initialize(xxx)
         ... skip some lines ...

     def addOrDelAttrValue(self, dn, attr, value, type):
         """Used to add or replace value of attribute which can handle  
multiple values.

         @type: add, delete.
         """
         self.dn = ldap.filter.escape_filter_chars(dn)
         if type == 'add':
             try:
                 self.conn.modify_s(self.dn, [(ldap.MOD_ADD, attr,  
value)])
                 return (True, 'SUCCESS')
             except ldap.TYPE_OR_VALUE_EXISTS:
                 return (True, 'SUCCESS')
             except Exception, e:
                 return (False, str(e))
         elif type == 'delete':
             try:
                 self.conn.modify_s(self.dn, [(ldap.MOD_DELETE, attr,  
value)])
                 return (True, 'SUCCESS')
             except ldap.NO_SUCH_ATTRIBUTE:
                 return (True, 'SUCCESS')
             except Exception, e:
                 return (False, str(e))
-----

Thanks very much :)

-- 
Best Regards.

Zhang Huangbin

- Open Source Mail Server Solution for Red Hat(R) Enterprise Linux,
   CentOS, Debian, Ubuntu: http://www.iredmail.org/





More information about the python-ldap mailing list