[Zope-CMF] Add properties to already existing content types

Raphael Ritz r.ritz@biologie.hu-berlin.de
Wed, 18 Jun 2003 08:55:18 +0200


Rainer Thaden wrote:

>Hi,
>
>i had to add some properties to a file system based class which
>already had some instances in the ZODB.
>I did it like this:
>I defined a function do_update in the class and called the method on
>all instances.
>Here's the code:
>
>    def do_update(self):
>        id = self.id
>
>        container = self.aq_parent
>        ob = self
>        
>        ob.__dict__ = self.__dict__
>        ob.__dict__['property1']=''
>        ob.__dict__['property2']=''
>        
>        container.manage_delObjects(ids=[id])
>        container._setObject(id, ob)
>
>The problem is that the new properties are tuples by default. When i
>debug in the edit method of the class and assign
>
>property1='bla'
>
>then i get property1=('bla') at the first time.
>When i assign it a second time i get the correct result.
>
>Am i doing something wrong? Maybe the update method is a bit fragile?
>  
>
Portal Content inherits from PropertyManager.
Have you tried just using 'manage_addProperty'?

Raphael

Or how about this:

>From tseaver@zope.com  Fri Aug  2 18:50:39 2002
>From: tseaver@zope.com (Tres Seaver)
>Date: 02 Aug 2002 13:50:39 -0400
>Subject: [Zope-CMF] Additional property to portal_type
>In-Reply-To: <008301c239b6$93c50930$315d4cd5@domowy>
>References: <008301c239b6$93c50930$315d4cd5@domowy>
>Message-ID: <1028310640.6076.3.camel@beauty>
>
>On Thu, 2002-08-01 at 19:44, Pawel Lewicki wrote:
>
>> Is there any simpler way to add an extra property to default CMFDocument
>> (e.g.) than overriding the whole class?
>
>ScriptableTypeInfo is your friend:
>
>http://cmf.zope.org/Members/tseaver/how_tos/using_scriptable_type_info
>
>You can add properties after creating the object using
>'manage_addProperty', e.g.::
>
>  product.manage_addProperty( 'some_prop', 'initial value', 'string' )
>
>Tres.
>-- 
>===============================================================
>Tres Seaver                                tseaver@zope.com
>Zope Corporation      "Zope Dealers"       http://www.zope.com
>
>  
>
Or this:

There is a very easy way to do this built into CMF, the Scriptable Type
Information.

In the Types Tool, create a new Scriptable Type Information, deriving it
from the base type you need. Name it "MyType" for instance. As
"Constructor path", type the name of a Python Script, for instance
MyType_create. Create this Python Script in the Types Tool:

##parameters=container, id, *args, **kw
# create the base object
container.invokeFactory('Some Base Portal Type', id)
ob = getattr(container, id)
# now add the properties you need
ob.manage_addProperty('foo', 1, 'int')
# finally return the object
return ob

The only thing you have to be aware of is that during script execution,
the portal_type of your object will be the base portal type and not yet
the one of your final type.

Florent
-- 
Florent Guillaume, Nuxeo (Paris, France)
+33 1 40 33 79 87  http://nuxeo.com  mailto:fg@nuxeo.com <mailto:fg@nuxeo.com>