flask-ldapconn
flask-ldapconn copied to clipboard
Writing to binary valued attributes does not work
I'm trying to write binary data (python type bytes) to a jpegPhoto attribute and get errors:
class User(UserMixin, ldap.Entry):
...
jpegPhoto = ldap.Attribute('jpegPhoto')
jpeg = ... # binary data, bytes type
user.jpegPhoto = jpeg
user.save()
This results in "AttributeError: 'int' object has no attribute 'encode'" in Attribute.get_changes_tuple() because it thinks it's a multi-valued attribute of integers.
jpeg = ... # binary data, bytes type
user.jpegPhoto = [jpeg]
user.save()
A slight improvement, now the data is at least accepted as a single value, error is "AttributeError: 'bytes' object has no attribute 'encode'".
class BytesLDAPHackAdapter(bytes):
def encode(self, enc):
return self
jpeg = ... # binary data, bytes type
user.jpegPhoto = [BytesLDAPHackAdapter(jpeg)]
This work-around does the trick by providing a no-op encode member function. But it would be much better if LDAPConn would deal properly with bytes typed data. The problem is that bytes is iterable and not str as far as I can see.
Thank you for a nice library otherwise :-)