Re: [Zope-CMF] Accessing the "context"
Am 30.10.2007 um 18:13 schrieb robert rottermann: there are two places where you can put your code, in both cases acquisition is fully functional. Thanks very much! Now hit an error that my database connection object can't be pickled but at least I can access it! :-D Charlie -- Charlie Clark Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-938-5360 GSM: +49-178-782-6226 ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
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 begin:vcard fn:Robert Rottermann n:Rottermann;Robert tel;work:++41 31 333 10 20 tel;fax:++41 31 333 10 23 tel;home:++41 31 333 36 03 x-mozilla-html:FALSE version:2.1 end:vcard ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Previously Charlie Clark wrote: > > Am 30.10.2007 um 17:40 schrieb Wichert Akkerman: > > >__init__ is indeed the wrong place: when the instance is created it is > >not placed in an acquisition context yet. > > That would indeed explain things!!! Is it okay to call a method which > does this from __init__ ? No, since the acquisition context is only created when the object is insert into the hierarchy somewhere (sloppy terminology here, I know). And that can only happen after the instance has been fully created. As an example: obj = Object() # obj has no acquisition context folder.obj = obj # folder.obj will have an acquisition context In other words: in __init__ and everything you call from __init__ the instance will not have an aq context. The context does not magically appear. Wichert. -- Wichert Akkerman <[EMAIL PROTECTED]>It is simple to make things. http://www.wiggy.net/ It is hard to make things simple. ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Am 30.10.2007 um 17:40 schrieb Wichert Akkerman: __init__ is indeed the wrong place: when the instance is created it is not placed in an acquisition context yet. That would indeed explain things!!! Is it okay to call a method which does this from __init__ ? def __init__(self, id): self.id = id self.set_connection() security.declarePublic('set_connection') def set_connection(self): DA = (self, 'myDA', None) This is also not working. :-/ Thanks very much for your help. Charlie -- Charlie Clark Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-938-5360 GSM: +49-178-782-6226 ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
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. Wichert. -- Wichert Akkerman <[EMAIL PROTECTED]>It is simple to make things. http://www.wiggy.net/ It is hard to make things simple. ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
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!!! Charlie -- Charlie Clark Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-938-5360 GSM: +49-178-782-6226 ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Charlie Clark wrote: Am 30.10.2007 um 16:53 schrieb Wichert Akkerman: getattr(context, 'my_da_or_what_ever', None) works a bit better. Doesn't that only work within a PythonScript? I need access from within a content type, ie. real Python code? It's called acquisition and it works everywhere. Try it. Yes, but the context isn't available everywhere. How do I get hold of that? Charlie -- Charlie Clark Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-938-5360 GSM: +49-178-782-6226 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. Andrew ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Wichert Akkerman wrote: da = getattr('my_da_or_what_ever', context, None) getattr(context, 'my_da_or_what_ever', None) works a bit better. Note that getattr('foo', context, None) is wrong. Wichert has the correct syntax; I didn't even pay attention to the syntax originally. :) Doesn't that only work within a PythonScript? I need access from within a content type, ie. real Python code? It's called acquisition and it works everywhere. Try it. Wichert. Andrew ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Am 30.10.2007 um 16:53 schrieb Wichert Akkerman: getattr(context, 'my_da_or_what_ever', None) works a bit better. Doesn't that only work within a PythonScript? I need access from within a content type, ie. real Python code? It's called acquisition and it works everywhere. Try it. Yes, but the context isn't available everywhere. How do I get hold of that? Charlie -- Charlie Clark Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-938-5360 GSM: +49-178-782-6226 ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Wichert Akkerman wrote: Previously Charlie Clark wrote: Am 30.10.2007 um 16:19 schrieb robert rottermann: da = getattr('my_da_or_what_ever', context, None) getattr(context, 'my_da_or_what_ever', None) works a bit better. Doesn't that only work within a PythonScript? I need access from within a content type, ie. real Python code? It's called acquisition and it works everywhere. Try it. It only works if the context is wrapped properly. :) Wichert. Andrew ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Charlie Clark wrote: Am 30.10.2007 um 16:19 schrieb robert rottermann: da = getattr('my_da_or_what_ever', context, None) Doesn't that only work within a PythonScript? I need access from within a content type, ie. real Python code? That will work within your content class code as well. That's probably your easiest route; I would probably abstract it into a tool/adapter/utility - all depending on what you're using in your stack (i.e. five). I'm not up to speed on cmf 2 changes and the model in that stack. Andrew Charlie -- Charlie Clark Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-938-5360 GSM: +49-178-782-6226 ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Previously Charlie Clark wrote: > > Am 30.10.2007 um 16:19 schrieb robert rottermann: > > > > >da = getattr('my_da_or_what_ever', context, None) getattr(context, 'my_da_or_what_ever', None) works a bit better. > Doesn't that only work within a PythonScript? I need access from > within a content type, ie. real Python code? It's called acquisition and it works everywhere. Try it. Wichert. -- Wichert Akkerman <[EMAIL PROTECTED]>It is simple to make things. http://www.wiggy.net/ It is hard to make things simple. ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Am 30.10.2007 um 16:19 schrieb robert rottermann: da = getattr('my_da_or_what_ever', context, None) Doesn't that only work within a PythonScript? I need access from within a content type, ie. real Python code? Charlie -- Charlie Clark Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-938-5360 GSM: +49-178-782-6226 ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Am 30.10.2007 um 15:41 schrieb Andreas Jung: Dear all, a simple question with hopefully a simple answer! How do I access objects from an object's context or hierarchy? Acquisition? Traversal using restrictedTraverse()? Yes, but I'm not sure which. Say I have a DA, myDA, in my portal root and I have content type that needs to access this to store some attributes in an external database, how do I get hold of it? import Aqcuisition class MyContentType(): def set_connection(self): self.connection = Acquisition.ac_aquire('myDA') raises an AttributeError I realise that this is probably down to me not getting Aqcuisition but this looks like how it should work going on the description of Aquisition. Charlie -- Charlie Clark Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-938-5360 GSM: +49-178-782-6226 ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
Charlie Clark schrieb: > Dear all, > > a simple question with hopefully a simple answer! How do I access > objects from an object's context or hierarchy? Specifically I'd like to > be able to access a ZopeDA connection for a site. I > if you know the id of the object you are looking for beforehand you can get it by just requesting it from the context like: da = getattr('my_da_or_what_ever', context, None) if da: ... s it correct that I > have to register this somewhere to be able to access it from a portal > object? > no hth robert begin:vcard fn:Robert Rottermann n:Rottermann;Robert tel;work:++41 31 333 10 20 tel;fax:++41 31 333 10 23 tel;home:++41 31 333 36 03 x-mozilla-html:FALSE version:2.1 end:vcard ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
Re: [Zope-CMF] Accessing the "context"
--On 30. Oktober 2007 14:46:44 +0100 Charlie Clark <[EMAIL PROTECTED]> wrote: Dear all, a simple question with hopefully a simple answer! How do I access objects from an object's context or hierarchy? Acquisition? Traversal using restrictedTraverse()? -aj pgpXFZBCguPi5.pgp Description: PGP signature ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests
[Zope-CMF] Accessing the "context"
Dear all, a simple question with hopefully a simple answer! How do I access objects from an object's context or hierarchy? Specifically I'd like to be able to access a ZopeDA connection for a site. Is it correct that I have to register this somewhere to be able to access it from a portal object? Charlie -- Charlie Clark Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-938-5360 GSM: +49-178-782-6226 ___ Zope-CMF maillist - Zope-CMF@lists.zope.org http://mail.zope.org/mailman/listinfo/zope-cmf See http://collector.zope.org/CMF for bug reports and feature requests