[Zope] Re: Adding __setitem__ to a formulator field?

Evan Simpson evan@4-am.com
Wed, 09 Oct 2002 13:12:08 -0500


Jeff Kowalczyk wrote:
> there are __getitem__() and get_value() methods for formulator fields. I'd like to
> experiment with __setitem__ and set_value(). The error message from my naive
> implementation in field.py is as follows:
> 
>     security.declareProtected('Access contents information', '__setitem__')
>     def __setitem__(self, key, value):
>         self[key] = value
>         self._p_set_changed(1)
>         return value

This approach should work for 'set_value', but '__setitem__' is a horse 
of a different kettle of fish.

With explicit methods, such as 'get_value' and 'set_value', name-based 
access control is sufficient, since there's no way to trigger them 
implicitly.  Since 'x[1] = 2' will implicitly access x's '__setitem__', 
you need to take one of two steps:

1. If you were willing to have uncontrolled write access to your 
objects, including setting and deletion of attributes, you could simply 
add _guarded_writes = 1 to the class.

2. For fine-grained control, add the following method to the class:

def __guarded_setitem__(self, index, value):
     if getSecurityManager().checkPermission(
         'Access contents information', self):
         self[index] = value
     else:
         raise Unauthorized('__setitem__')

Cheers,

Evan @ 4-am