[Grok-dev] Annotations adapters and interface inheritance

Douglas Cerna douglascerna at yahoo.com
Wed May 7 16:24:59 EDT 2008


Hi everybody!

I just spent two hours trying to understand this:

--- interfaces ---

IResource(Interface):
    data = schema.Bytes(...)

IDateable(Interface):
    date = schema.Date(...)

IMedia(IDateable):
    duration = schema.Int(...)

IPublication(IDateable):
    type = schema.Choice(...)

IResourceContainer(Interface):
    ...

IMediaContainer(Interface):
    ...

IPublicationContainer(Interface):
    ...

--- models ---

Resource(grok.Model):
    grok.implements(IResource)
    def __init__(self, **data):
        self.data = data.get("data")

--- annotations ---

Media(grok.Annotation):
    grok.context(IResource)
    grok.provides(IMedia)

Publication(grok.Annotation):
    grok.context(IResource)
    grok.provides(IPublication)

--- form ---

class AddForm(grok.Form):
    grok.context(IResourceContainer)
    
    def __init__(self, *args, **kw):
        super(AddForm, self).__init__(*args, **kw)
        self.form_fields = grok.Fields(IResource)
        # No container provides IMediaContainer and IPublicationContainer
        # at the same time
        if IMediaContainer.providedBy(self.context):
            self.form_fields += grok.Fields(IMedia)
        if IPublicationContainer.providedBy(self.context):
            self.form_fields += grok.Fields(IPublication)

    @grok.action(u"Add")
    def add(self, **data):
        resource = Resource(**data)
        changes = self.applyData(resource, **data)
        import pdb; pbd.set_trace()

If I use the form in a IPublicationContainer, everything works as expected. However, if I use it in a IMediaContainer I get this:

(Pdb) changes
{<InterfaceClass roco.interfaces.IDateable>: ['date'], <InterfaceClass roco..interfaces.IMedia>: ['duration'], <InterfaceClass roco.interfaces.IResource>: ['data']}
(Pdb) IMedia(resource)
<roco.Media object at 0xb5f8e62c>
(Pdb) IMedia(resource).date
*** AttributeError: 'Media' object has no attribute 'date'

And when I adapt the object to IDateable I get this:

(Pdb) IDateable(resource)
<roco.Publication object at 0xb5f8e66c>

I know applyData adapts the context to the requested form interfaces. But, why is it adapting IResource to IDateable using the Publication adapter instead of the Media adapter? It works if I provide a IResource -> IDateable adapter, though.

Could anybody comment on this please. Thanks. Regards, Douglas.

"... allí­ es cuando te das cuenta que las cosas malas pueden resultar bastante buenas..." - Lionel Messi

Por favor, evite enviarme adjuntos de Word, Excel o PowerPoint.
Vea http://www.gnu.org/philosophy/no-word-attachments.es.html


      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ



More information about the Grok-dev mailing list