[Zope-dev] Proposed interface: Restricted creation

Itamar Shtull-Trauring itamar@maxnm.com
Wed, 05 Jul 2000 11:47:46 +0300


(URL:
http://www.zope.org/Members/michel/Projects/Interfaces/RestrictedCreation)

Restricted Creation of Objects

The problem - certain types of objects should not be added except in certain
places or with certain restrictions. For example, we may want to make sure
Laundry objects are only added inside Luggage objects, and nowhere else. Or
we may want to make sure that an object is added only if there's a MailHost
in it's acquistion path. How do we ensure this?

I propose a Restricted Creation Interface - when an ObjectManager constructs
it's Add list in manage_main, it first checks with each addable class if
it's instances can be added in this specific ObjectManager.

We do this by calling a class method called _addableIn(), that receives as a
parameter the ObjectManager to which we may want to add objects. If the
result is not true, instances of this class can't be added, and won't be
listed in the Add list.

For example, the following code will make the Laundry class show up in the
Add menu only in objectmanagers whose meta_type is "Luggage":

class Laundry(SimpleItem):
    """ A Fruit class """

    def _addableIn(parent):
            if parent.meta_type == "Luggage": return 1
            else: return 0


I'm not sure how to get from meta_type to class, so I can't show working
code, but something like this in ObjectManager.py would do the trick:

    def filtered_meta_types(self, user=None):
        # Return a list of the types for which the user has
        # adequate permission to add that type of object.
        user=getSecurityManager().getUser()
        meta_types=[]
        if callable(self.all_meta_types):
            all=self.all_meta_types()
        else:
            all=self.all_meta_types
        for meta_type in all:
>           klass = getClassFromMetaType(meta_type)
>
>           # If the class has a function _addableIn, it must return true or
>           # that means the class doesn't want to be added to this ObjectManager:
>
>           if not hasattr(klass, '_addableIn') or klass._addableIn(self):
>               if meta_type.has_key('permission'):
>                   if user.has_permission(meta_type['permission'],self):
>                       meta_types.append(meta_type)
>               else:
>                   meta_types.append(meta_type)
        return meta_types

Comments?

-- 
Itamar S.T.  itamar@maxnm.com
Fingerprint = D365 7BE8 B81E 2B18 6534  025E D0E7 92DB E441 411C