[Zope-dev] Re: PPropertyHandlers

Phillip J. Eby pje@telecommunity.com
Sat, 06 May 2000 18:16:23 -0500


At 04:59 PM 5/5/00 +0000, Jason Spisak wrote:
>
>Which brings me to ask what I can do to bring PropertyHandlers to a today
>solution?  Do you have a model that I try to cut my teeth on?  Or would you
>like me to just clam up and wait until you sort things out. :-)

Here's something to play with.  It's not tested yet, not even tried to
compile it yet.  But it should give a basic idea of how PropertyHandlers
might be implemented.  One would use it like this:


class MyZopeObject(...):

    myPropertyA = BasicPropertyHandler(
        id='myPropertyA', attr='_A'
        title='First Property',
        readPerm='Access MyZopeObject data',
        writePerm='Configure MyZopeObjects',
        dataType='int'
    )

You could then in DTML call "someobject.myPropertyA.set(3)", or use
"someobject.myPropertyA.editForm()" to slap in an input box on a properties
form. To get the value of the property, just use <dtml-var myPropertyA> or
<dtml-var "myPropertyA()"> on the object.  To read the value, you would
need "Access MyZopeObject data" permission, and to set it you would need
"Configure MyZopeObjects" permission.  These permissions should be included
in your class's __ac_permissions__ or otherwise defined in the Product so
that roles could be mapped to them.  The value of myPropertyA would
actually be stored in the "_A" attribute of MyZopeObject instances.  Last,
but not least, properties possessing titles makes it easier to make a form
that just walks through an object's properties and displays a prompt (using
the title attribute) and an input field (using the editForm() method).

Anyway, here's the sample code that will probably be our basis for eventual
implementation.  It doesn't handle all of the property types ZPublisher
supports, and it doesn't support anything that well.  There is no concept
(yet) of how one knows what properties an object has.  But it does showcase
permission management support.  One could, with a fair bit of work, build
from this base a full-fledged Zope object capable of being configured
through-the-web and placed on the Methods tab of a ZClass.  Enjoy.


from Globals import default__class_init__
from Acquisition import Implicit
from AccessControl.PermissionRole import PermissionRole
dataFormats = {}

class BasicPropertyHandler(Implicit):
    """Encapsulate set/get marshalling and HTML editing of a property"""

    def __init__(self, id, attr, title='', inputFormat=None,
                    readPerm='Read properties', writePerm='Set properties',
                    dataType='string'):

        self._Read_properties_Permission = readPerm
        self._Set_properties_Permission = writePerm
        self.id=id
        self.title=title or id
        self.attr_name = attr
        self.dataType = dataType

        if inputFormat: 
            self.inputFormat = inputFormat
        else:
            self.inputFormat = dataFormats.get(dataType,
                '<input name="%(id)s:%(type)s" type="text" value="%(value)s">'
            )

    def __call__(self):
        """Get the attribute value"""
        return getattr(self.aq_parent,self.attr_name)

    def set(self,value):
        """Set the attribute value"""
        # type conversion here??
        setattr(self.aq_parent,self.attr_name,value)

    def editForm(self):
        """Return an HTML snippet for editing"""
        return self.inputFormat % {
            'id':self.id,
            'type':self.dataType,
            'value': self()
        }

    __ac_permissions__ = (
        ('Read properties', ()),
        ('Set properties', ('set','editForm')),
    )

    # This is because we can't set roles on __call__  :(
    __roles__ = PermissionRole('Read properties')


default__class_init__(BasicPropertyHandler)