Hi All,

I am trying to write a model that will encrypt all data at the
database level, but can be accessed easily through normal attributes.
I stumbled across the hybrid_property feature and it works fine, as
long as I don't try and filter() or filter_by() my hybrid_property.
The thing is it'd be quite nice to filter by the hybrid_properties as
these are the friendly names for encrypted data in the database.
Here's a cut-down version of my class:

----------------------------------------------------------------------
class User(object):
    username_enc = Column(String(128))

    @hybrid_attribute
    def username(self):
        return decrypt_string(self.username_enc)

    @username.setter
    def username(self, value):
        return self.username_enc = encrypt_string(value)

    @username.expression
    def username(cls):
        return func(decrypt_string(cls.username_enc))

user1 = User()
user1.username = 'test1'
print user1.username           # works
print user1.username_enc    # works
----------------------------------------------------------------------

This works fine on the object itself for getting and setting values,
but if I try to do the following it fails in my "encrypt_string"
function complaining that the type should be a string and not a
InstrumentedAttribute.

User.query.filter(User.username == 'test1').first()

The error message regarding "InstrumentedAttribute" is because
base64.b64decode() expects a buffer, rather than an
InstrumentedAttribute to decode, but what I don't understand is how to
get it to filter based on the encrypted version of the username when
the front-end code tries to filter on 'username'

SQLAlchemy: 0.7.6
Python: 2.7


Any help would be appreciated.

Cheers,
David.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to