[Zope3-dev] Re: View permissions
    Philipp von Weitershausen 
    philipp at weitershausen.de
       
    Tue Jan 30 08:46:42 EST 2007
    
    
  
Martijn Faassen wrote:
> Marius Gedminas wrote:
> [snip]
>> Also, I'd like a way to specify the adapter name in the adapter class.
>> Something like
>>
>>     from zope.interface import implements
>>     from zope.component import adapts, named
>>
>>     class MyNamedAdapter(object):
>>         adapts(IFoo)
>>         implements(IBar)
>>         named('myname')
>>
>>         def __init__(self, context):
>>             self.context
>>
>> Also I'd like to have a zope.component.Adapter class that defines
>>
>>         def __init__(self, context):
>>             self.context
>>
>> and does nothing else.  It seems to me that 95% of all adapters have an
>> __init__ like this.  It is tiring to keep repeating it.
>>
>> Also, I want a pony.  Ok, not really.
> 
> You can have your pony. In grok, there's a grok.Adapter class, which
> does the thing in init, where you can specify the name using grok.name, 
> and which causes registration as an adapter without the need for ZCML.
E.g.:
import grok
from zope.index.text.interfaces import ISearchableText
class Page(grok.Model):
     def __init__(self, text):
         self.text = text
class SearchableTextForPage(grok.Adapter):
     grok.implements(ISearchableText)
     grok.context(Page) # automatically assumed = the default
     grok.name('')      # the default
     def getSearchableText(self):
         # self.context is set by grok.Adapter.__init__
         return self.context.text
-- 
http://worldcookery.com -- Professional Zope documentation and training
Next Zope 3 training at Camp5: http://trizpug.org/boot-camp/camp5
    
    
More information about the Zope3-dev
mailing list