[Zope3-Users] Re: Extending the Buddy demo -- adding BuddyFolder.hello() method how?

Dominik Huber dominik.huber at projekt01.ch
Wed Dec 29 16:25:14 EST 2004


There is still a little flaw...

>    class BuddyFolder(BTreeContainer):
>    zope.interface.implements(IBuddyFolder)
>
>    __used_for__ = IBuddyFolder
>
>    def hello(self):
>        return "Hello World"

   class BuddyFolder(BTreeContainer):
       zope.interface.implements(IBuddyFolder)

       def hello(self):
           return "Hello World"

This change has no impact to the error: You don't need the __used_for__ 
declaration. This is an optional declaration for adapters only (An 
instance of such adapter can be used for IBuddyFolder for example, but 
your buddy folder cannot be used for itself).

Jim implemented a replacement for that __used_for__ declaration a week 
ago. It can be used like the implements declaration -> 
zope.component.adapts(IBuddyFolder).

> And my configuration is:
>      <content class=".buddy.BuddyFolder">
>        <require permission="zope.View"
>            interface="zope.app.container.interfaces.IReadContainer" />
>        <require permission="zope.ManageContent"
>            interface="zope.app.container.interfaces.IWriteContainer" />
>    </content>

<content class=".buddy.BuddyFolder">
  <allow attributes="hello" />
  <require permission="zope.View"
           interface="zope.app.container.interfaces.IReadContainer"
           />
  <require permission="zope.ManageContent"
           interface="zope.app.container.interfaces.IWriteContainer"
           />
</content>

I does not recognized that one the last time. You need to declare your 
new 'hello' method within the content directive. The error is raised 
because you do not set the permission to your hole interface, but only 
to its derived sub interfaces IReadContainer and IWriteContainer. 
Therefore your newly implemented hello method is not registered 
automatically and you have to 'enable' it first. Afterward it will be 
traversable too.


Other ways of registration and implementation:
Require the hole interface at once, but then you loose the fine grained 
permission declartions:

<content class=".buddy.BuddyFolder">
  <require permission="zope.View"
           interface=".interfaces.IBuddyFolder"
           />
</content>

Maybe a good implementation would be to write independent interface, too:

class IHelloWorld(Interface):
    def hello():
         """bla"""

Add that interface to the implements declaration of your buddy folder:

   class BuddyFolder(BTreeContainer):
       zope.interface.implements(IBuddyFolder, IHelloWorld)

       def hello(self):
           return "Hello World"

And register those changes within the zope application:

<content class=".buddy.BuddyFolder">
  <require permission="zope.View"
           interface=".interfaces.IHelloWorld"
           />
  <require permission="zope.View"
           interface="zope.app.container.interfaces.IReadContainer"
           />
  <require permission="zope.ManageContent"
           interface="zope.app.container.interfaces.IWriteContainer"
           />
</content>

Now, everything should work - at least in my brain ;)

Regards,
Dominik



More information about the Zope3-users mailing list