Hello,

I have created an SQLAlchemy type which represents a postgresql aclitem (which 
represents postgresql access control lists). I am able to load and save 
newly-created ACLItems from the database, however, modifying the values of an 
instance of the type does not dirty it for flushing. Is there some decorator 
for dirtying accessors to the type instance convenience methods?

Specifically, modifying any of "grantee", "grantor", "permissions", and 
"grant_option", does not trigger a proper update.

Cheers,
M

import sqlalchemy.types as types
import re
import sqlalchemy.exc

#include/utils/acl.h
#define ACL_ALL_RIGHTS_STR      "arwdDxtXUCTc" 

class ACLItem(types.UserDefinedType):

    def 
__init__(self,grantee=None,permissions=None,grantor=None,grant_option=False):
        #note that sqlalchemy calls this with None arguments for processing
        self.grantee = grantee
        self.permissions = []
        if permissions:
            for p in permissions:
                self.permissions.append(p)
        self.grantor = grantor
        self.grant_option = grant_option

    def get_col_spec(self):
        return 'aclitem'

    def bind_processor(self,dialect):
        def acl2string(aclitem):
            return aclitem._as_pgsql_string()
        return acl2string

    def compare_values(self,a,b):
        return a._as_pgsql_string() == b._as_pgsql_string()

    def _as_pgsql_string(self):
        #convert to string 'user <grantee>=<perms>/<grantor>'
        string_perms = ''
        for perm in self.permissions: 
            string_perms += perm
            
        if self.grant_option:
            grant_option = '*'
        else:
            grant_option = ''
        return "user %s=%s%s/%s" % 
(self.grantee,string_perms,grant_option,self.grantor)        

    @classmethod
    def _from_pgsql_string(klass,aclstring):
        "grantee=perms*/grantor"
        matches = re.match('([^=]+)=([^/\*]+)(\*?)/(\w+)',aclstring)
        if matches is None:
            raise sqlalchemy.exc.DataError(aclstring,[],'')
        grantee = matches.group(1)
        permissions = matches.group(2)
        grant_option = len(matches.group(3))
        grantor = matches.group(4)
        return ACLItem(grantee,permissions,grantor,grant_option)        

    def result_processor(self,dialect,column_type):
        def string2acl(aclstring):
            return ACLItem._from_pgsql_string(aclstring)
        return string2acl

    def has_permission(self,permission_test):
        return permission_test in self.permissions

    def set_permission(self,permission,on=True):
        if not self.has_permission(permission):
            if on:
                self.permissions.append(permission)
            else:
                self.permissions.remove(permission)

    def clear_permissions(self):    
        del self.permissions[:]

    def __str__(self):
        return self._as_pgsql_string()

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

Reply via email to