[Zope-CMF] Accessing the "context"

robert rottermann robert at redcor.ch
Tue Oct 30 13:13:21 EDT 2007


Wichert Akkerman schrieb:
> Previously Charlie Clark wrote:
>> Am 30.10.2007 um 17:04 schrieb Andrew Sawyers:
>>
>>> If it's in a content type's class, why not:
>>> id = getattr(self, 'da_id', None)
>>> Just using self as the context should be fine.
>>> -- that of course presumes that the type is wrapped properly when  
>>> you call that within your method.
>> mm, currently getting None back so I guess I'm not wrapping things  
>> properly.
>>
>> This is a skeleton bit of code:
>>
>> class ContentType(PortalContent):
>>     """A content type with access to an external database"""
>>
>>     def __init__(self, id):
>> 	self.id = id
>>         DA = getattr(self, 'MyDA', None)
>> 	#self.conn = DA() # not working at the moment because DA is None
>>
>> Is it wrong to try and do this in my __init__ ? Regarding your other  
>> suggestion: yes, I probably will factor this out into a utility once  
>> I've worked out how to access the DA!!!
> 
> __init__ is indeed the wrong place: when the instance is created it is
> not placed in an acquisition context yet.

there are two places where you can put your code, in both cases
acquisition is fully functional.
if it is ATContentTyoe based you can add a method:

    #
    # AT specific, called after creation of the final
    # instance
    #
    security.declarePrivate('at_post_create_script')
    def at_post_create_script(self, member_id=None):
	da = getattr(self, 'MyDA', None)

or if it is a "mere" CMF content type:

    def manage_afterAdd(self, item, container):
        """
            Add self to the catalog.
            (Called when the object is created or moved.)
        """
	da = getattr(self, 'MyDA', None)


robert
-------------- next part --------------
A non-text attachment was scrubbed...
Name: robert.vcf
Type: text/x-vcard
Size: 178 bytes
Desc: not available
Url : http://mail.zope.org/pipermail/zope-cmf/attachments/20071030/86cbfabc/robert.vcf


More information about the Zope-CMF mailing list