[Zope-CMF] How to get Type in manage_afterAdd?
Jon Edwards
jon@pcgs.freeserve.co.uk
Thu, 31 Jan 2002 19:02:11 -0000
Thanks Florent,
I found a workround, by adding a line to the CatalogTool.catalog_object
instead -
vars['org_type'] = object.unrestrictedTraverse(object.org_id).Type()
seems kinda clunky, but works (ish) so far.
I have a simpler (I think) manage_afterAdd question. I'm auto-adding a
Document to every folder/organisation, by putting -
addDocument(self, id = 'index.htm')
in the manage_afterAdd section (I'm using .htm to make it easier for
Dreamweaver). Then I realised manage_afterAdd is also called whenever an
object is copy/pasted somewhere. So I changed it to -
if not hasattr(self, 'index.htm'):
addDocument(self, id = 'index.htm')
so that it won't try to re-add the Document if it already exists.
But it's not working! It's still trying to add index.htm again when I paste
the folder and causing an error. Can someone please correct my syntax? :-)
Thanks
Cheers, Jon
> -----Original Message-----
> From: Florent Guillaume [mailto:fg@nuxeo.com]
> Sent: 31 January 2002 18:33
> To: jon@pcgs.freeserve.co.uk
> Subject: Re: [Zope-CMF] How to get Type in manage_afterAdd?
>
>
> [sent to zope-dev]
>
> This problem is due to a deep bug in the current implementation of the
> Types Tool: a created object has no way to know its portal_type during
> construction (the constructor doesn't have this information, thus nor
> __init__ nor the hooks like manage_afterAdd can have it).
>
> I thought that this bug was already in the Tracker but I don't find it.
> Anyone knows ?
>
> Anyway I'd propose the following: we need, in constructInstance, to
> somehow pass the portal_type to the object constructor. Unfortunately
> this has to be backwards-compatible with all the constructors out there
> that don't know about it. I see two possible ugly solutions to do it:
>
> - do "constructor.__portal_type = portal_type" before calling
> constructor(...), and the constructor does something like:
> def manage_addFoo(...):
> try:
> portal_type = manage_addFoo.__portal_type
> manage_addFoo.__portal_type = None
> except:
> portal_type = None
> ...
>
> - in constructInstance, use introspection (getargspec) to look
> if the constructor accepts a portal_type=xxx named parameter,
> and pass it if it does.
>
> Anyone sees something cleaner ? :-)
>
> Florent