adding dtml/zpt methods to product classes
Greetings Zopistas! I have been a Zope tinkerer for a few years and have never been able to adequately solve this problem: When I create a Product class with DTML/ZPT methods, how can I make those methods members of the class rather than simply instance methods? These examples should illustrate what I mean. My first try was like this: class HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item): def __init__(self,resourcePath, imageFolder): self.my_method = Globals.HTMLFile("dtml/my_method",globals()) That works fine until you want to add a new method to your class like so: class HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item): def __init__(self,resourcePath, imageFolder): self.my_method = Globals.HTMLFile("dtml/my_method",globals()) self.my_new_method = Globals.HTMLFile("dtml/my_new_method",globals()) new instances of your product will get the my_new_method, but instances that have already been created will not. My second try was like this: class HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item): def my_method(self, REQUEST): """ template """ if (not hasattr(self,"my_method_member")): self.my_method_member = Globals.DTMLFile("dtml/my_method",globals()) return self.my_method_member(self,REQUEST) This allows me to add methods to the class at any time and all instances will get the method. However, for some reason, when I use this pattern, i get strange acquisition results (for example, some of my objects children's dtml methods will use the parent's namespace for lookups). Currently, I'm doing this: class HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item): def my_method(self, REQUEST): """ template """ self.my_method_member = Globals.DTMLFile("dtml/my_method",globals()) return self.my_method_member(self,REQUEST) which seems ugly, and like it might have performance problems, but i haven't had any acquisition problems yet. My real question here is: What is the standard way to handle this problem of being able to add dtml/zpt methods to classes such that all instances of those classes get the new methods? Thanks in advance for any insight. Sincerely, Keith Alperin
On Thu, Jan 27, 2005 at 09:59:17PM -0600, Keith Alperin wrote:
Greetings Zopistas!
I have been a Zope tinkerer for a few years and have never been able to adequately solve this problem: When I create a Product class with DTML/ZPT methods, how can I make those methods members of the class rather than simply instance methods? These examples should illustrate what I mean.
I feel like I must be misunderstanding the question. Your examples are so odd, you must be trying to do something unusual ;-) But if you're asking what I think you're asking, just do what nearly ever other zope Product on the planet does: class Foo(SimpleItem): my_method1 = DTMLFile('dtml/my_method', globals()) my_method2 = PageTemplateFile('zpt/my_other_method', globals()) In other words, set them as class attributes, not within another method. And to secure them, do this: class Foo2(SimpleItem): security = AccessControl.classSecurityInfo() security.declareProtected('Some Permission', 'method1') method1 = PageTemplateFile('zpt/method1', globals()) security.declarePrivate('method2') method2 = PageTemplateFile('zpt/method1', globals()) security.declarePublic('method3') method3 = PageTemplateFile('zpt/method3', globals()) -- Paul Winkler http://www.slinkp.com
You understood the question perfectly; this is exactly what i was looking for. I'm not sure where I got the idea of making those methods instance variables, but once I started, I never even looked for a better (even standard!) alternative. Thank you (and Pascal Peregrina who gave the same answer) for your help!! Sincerely, Keith Alperin On Jan 27, 2005, at 11:23 PM, Paul Winkler wrote:
On Thu, Jan 27, 2005 at 09:59:17PM -0600, Keith Alperin wrote:
Greetings Zopistas!
I have been a Zope tinkerer for a few years and have never been able to adequately solve this problem: When I create a Product class with DTML/ZPT methods, how can I make those methods members of the class rather than simply instance methods? These examples should illustrate what I mean.
I feel like I must be misunderstanding the question. Your examples are so odd, you must be trying to do something unusual ;-)
But if you're asking what I think you're asking, just do what nearly ever other zope Product on the planet does:
class Foo(SimpleItem):
my_method1 = DTMLFile('dtml/my_method', globals())
my_method2 = PageTemplateFile('zpt/my_other_method', globals())
In other words, set them as class attributes, not within another method.
And to secure them, do this:
class Foo2(SimpleItem):
security = AccessControl.classSecurityInfo()
security.declareProtected('Some Permission', 'method1') method1 = PageTemplateFile('zpt/method1', globals())
security.declarePrivate('method2') method2 = PageTemplateFile('zpt/method1', globals())
security.declarePublic('method3') method3 = PageTemplateFile('zpt/method3', globals())
--
Paul Winkler http://www.slinkp.com _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
participants (2)
-
Keith Alperin -
Paul Winkler