Implements a interface at run time

2 messages Options
Embed this post
Permalink
lucmult () Implements a interface at run time
Reply Threaded More More options
Print post
Permalink
Hi all,

I need to make a class implements a new interface at run time.

The use case:

I want that the user select what Content Type will be rateable, via site setup.

This works ok with contentratings and plone.contentratings, but I need index the rates of selected types.

I added a subscriber to IObjectRatedEvent:
    <subscriber
        for="*
             contentratings.interfaces.IObjectRatedEvent"
        handler=".subscribers.reindexRating"
        />

And did:
def reindexRating(obj, event):
    try:
        rated = IUserRating(obj)
    except (ComponentLookupError, TypeError, ValueError):
        # The catalog expects AttributeErrors when a value can't be found
        return

    obj.averageRating = rated.averageRating
    obj.numberOfRatings = rated.numberOfRatings
    obj.reindexObject(idxs=['averageRating', 'numberOfRatings'])
    # another approach, I don't know what is correct, yet. :-/
    #rated.reindexObject(idxs=['averageRating', 'numberOfRatings'])

But, all objects can't be adapted to IUserRating interface, so I want to do something like below, after user configure the Type in Ratings configlet in Site Setup.
classImplements(obj.__class__, IUserRating) # I know that is wrong :-)

How can I do that classImplements or how can I get the class of a Content Type ?

Thanks a lot,
--
Luciano Pacheco
Simples Consultoria
www.simplesconsultoria.com.br

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
Martin Aspeli () Re: Implements a interface at run time
Reply Threaded More More options
Print post
Permalink
Luciano Pacheco wrote:
> Hi all,
>
> I need to make a class implements a new interface at run time.

You can't do that, but you can make instances provide an interface via
an event subscriber for IObjectAddedEvent or whatever, using
zope.interface.alsoProvides().

Also, I think plone.contentratings supports the use case you're after,
so maybe you don't need to do anything custom.

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users