Set _allowed_meta_types on folder instances without creating a new product?
Is there a way to set the class attribute sometimes used in products class myProduct(...): _allowed_meta_types = ('Image','File') On a regular folder instance, without creating a plethora of specialized folder classes?
Jeff Kowalczyk writes:
Is there a way to set the class attribute sometimes used in products
class myProduct(...): _allowed_meta_types = ('Image','File')
On a regular folder instance, without creating a plethora of specialized folder classes? Yes.
Make an External Method. Call it on the folder and give it as parameter the meta types your want to allow. The External Method will look like: def changeAllowedMetaTypes(self,mts): '''set '_allowed_meta_types' to *mts*.''' self._allowed_meta_types= tuple(mts) return '_allowed_meta_types changed' You call it like URL_TO_YOUR_FOLDER/changeAllowedMetaTypes?mts:list=Image&mts:list=File Dieter
On Fri, Apr 26, 2002 at 10:25:33PM +0200, Dieter Maurer wrote:
URL_TO_YOUR_FOLDER/changeAllowedMetaTypes?mts:list=Image&mts:list=File
Ah, the joys and wonders of the modern world! Command-line access to the Web! :))) Oleg. -- Oleg Broytmann http://www.zope.org/Members/phd/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
Make an External Method. Call it on the folder and give it as parameter the meta types your want to allow.
I've created changeAllowedMetaTypes.py and installed it as an External method. from string import join def changeAllowedMetaTypes(self, mts): """set _allowed_meta_types to mts""" self._allowed_meta_types = tuple(mts) self._p_changed = 1 return '_allowed_meta_types changed to ' + join(self._allowed_meta_types) + ' on ' + self.id I'm calling it like http://localhost:8080/myFolder/Images/changeAllowedMetaTypes?mts:list=Im age&mts:list=Folder And it's returning '_allowed_meta_types changed to Image Folder on Images', so the call seems to be succeeding. However, the folder Images still shows the full metatype list when I return to it. Am I missing something?
Jeff Kowalczyk writes:
... And it's returning '_allowed_meta_types changed to Image Folder on Images', so the call seems to be succeeding. However, the folder Images still shows the full metatype list when I return to it. Am I missing something? I told you how to define "_allowed_meta_types" on a per instance basis but did not check that "_allowed_meta_types" has the desired effect.
When your message above arrived, I tried to find a use of "_allowed_meta_types", but the Zope sources do not reference it. Where do you know about it? Dieter
I told you how to define "_allowed_meta_types" on a per instance basis but did not check that "_allowed_meta_types" has the desired effect. When your message above arrived, I tried to find a use of "_allowed_meta_types", but the Zope sources do not reference it. Where do you know about it?
I've been using it in mxmObjectManager derived product classes to limit the Metatype list. class PurchaseOrder(PropertyManager, mxmObjectManager.mxmObjectManager): "A PurchaseOrder" meta_type = 'Purchase Order' _allowed_meta_types = ('Invoice','Item','Location','Shipment',) It works exactly as expected, but it would be very useful to apply this to existing folders which are specialized in purpose, but require no special class of their own. So I went looking into OFS ObjectManager and the mxmObjectManager source, to see what Zope's native mechanism for this was: mxmObjectManager.py ... # A cleaner way to filter metatypes # __allowed_meta_types = ('DTML Document','DTML Method') def all_meta_types(self): """ Returns what meta_types can be added to the objectmanager """ if hasattr(self, '_allowed_meta_types'): result = [] for metaType in Products.meta_types: if metaType['name'] in self._allowed_meta_types: result.append(metaType) return result else: return Products.meta_types And OFS/ObjectManager.py def all_meta_types(self, interfaces=None): ... 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: 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 I'm more confused than before. Setting __all_allowed_metatypes causes a "has key" error on the folder.
Jeff Kowalczyk writes:
I told you how to define "_allowed_meta_types" on a per instance basis but did not check that "_allowed_meta_types" has the desired effect. When your message above arrived, I tried to find a use of "_allowed_meta_types", but the Zope sources do not reference it. Where do you know about it?
I've been using it in mxmObjectManager derived product classes to limit the Metatype list. ... It works exactly as expected, but it would be very useful to apply this to existing folders which are specialized in purpose, but require no special class of their own. To apply it to existing folder, you need to redefine their "all_meta_types" method - in a similar spirit the "mxmObjectManager" did...
Dieter
it would be very useful to apply this to existing folders which are specialized in purpose, but require no special class of their own.
[Dieter] To apply it to existing folder, you need to redefine their "all_meta_types" method - in a similar spirit the "mxmObjectManager" did... How do I redefine a method (as opposed to setting a property) on an existing object? I don't want to subclass Folder into a full metatype for each combination of metatypes allowed, but I could see making a series of def methods: scripts or a product module that I could 'attach' as overrides to instances of regular, already created Folders as needed. Is that possible?
Jeff Kowalczyk writes:
it would be very useful to apply this to existing folders which are specialized in purpose, but require no special class of their own.
[Dieter] To apply it to existing folder, you need to redefine their "all_meta_types" method - in a similar spirit the "mxmObjectManager" did...
How do I redefine a method (as opposed to setting a property) on an existing object?
I don't want to subclass Folder into a full metatype for each combination of metatypes allowed, but I could see making a series of def methods: scripts or a product module that I could 'attach' as overrides to instances of regular, already created Folders as needed. Is that possible? I did not meant, that you modify "all_meta_types" for each folder type you want to restrict but that you combine the "_allowed_meta_types" with a single modified "all_meta_types" that respects this attribute when it is available.
Your options for such a modification: * directly modify the source you want to retain a patch in order to be able to apply it to new Zope releases or you work with CVS (and its "import" command). * make a monkey patch (this modifies the class at initialization time) There are documentation about this at Zope.org. "TransparentFolder" is an example how to make a monkey patch for "ObjectManager". Dieter
participants (3)
-
Dieter Maurer -
Jeff Kowalczyk -
Oleg Broytmann