0
Event Driven Programming in
           Plone
                     Matt Hamilton


    ...or how to extend Plone the lazy w...
Who am I?


                    - Matt Hamilton
                    - Technical Director of
                         Netsi...
Who is this talk for?


           - Integrators
           - Those newish to Plone, they can
                assemble tog...
What is the problem?




                          ?
Matt Hamilton   European Plone Symposium 2010, Sorrento   4
What is the problem?




                I want to change the functionality of an
                existing product, but do...
What is the problem?




                Came from a real life problem.
                Developing Netsight's new website....
What is the problem?




Matt Hamilton   European Plone Symposium 2010, Sorrento   7
What is the problem?




                plone.app.discussion has captcha
                support already, but I wanted to...
Admittedly, I could have offered to
                refactor the whole p.a.discussion code ;)




Matt Hamilton           ...
Considered Approaches


           - I could subclass the product and
                override it
                ➡ A lot ...
Eureka!

Matt Hamilton   European Plone Symposium 2010, Sorrento   11
Eureka!




                Use Events!


Matt Hamilton    European Plone Symposium 2010, Sorrento        12
Eureka!




           - I realised I could leave p.a.discussion
                alone, and just listen for an event for
 ...
Advantages




           - Leave the existing code alone
           - Work around lack of suitable extension
            ...
Zope's Event System




           - Events
           - Subscribers


Matt Hamilton        European Plone Symposium 2010,...
A Simple Event
    in configure.zcml:

    <subscriber
      for=".interfaces.IMyObject
           .interfaces.IMyEvent"
  ...
Event Simpler Event


   in events.py:

   from five import grok
   from interfaces import IMyObject, IMyEvent

   @@grok....
Example - Spam Checking

     in events.py:

     from five import grok
     from plone.app.discussion.interfaces 
       ...
Next Example - GetPaid




            GetPaid is an eCommerce add-on to
            Plone

            ➡ Allows you to ma...
PloneConf 2010 Registration

                       User clicks ‘register’


         User fills in ‘add attendee’ form, an...
GetPaid Example

     from Products.CMFCore.utils import getToolByName
     from getpaid.core.interfaces import workflow_s...
Other Ideas



           - Whenever a Folder is created, add
                some default content to it

           - Whe...
Questions?
        Matt Hamilton


matth@netsight.co.uk
Upcoming SlideShare
Loading in...5
×

Plone: Event Driven Programming

3,135

Published on

A talk I gave at the European Plone Symposium 2010 in Sorrento.

Event Driven Programming in Plone - or how to extend Plone the lazy way

Ever wanted to extend Plone somehow but not wanted to change the existing code for a product? Want to learn the easy way to extend Plone's functionality?

Events!

I will show you some examples of how to use Plone's event subscriber and workflow systems to extend Plone without having to change any existing products. Using a number of common Plone products and the events system to combine them you can come up with unlimited possibilities. I'll use examples from two websites I've been working on recently to show you how you can make authors have to pay (using GetPaid) to publish content on a site, and how to check the comments are not spam on your discussion board.

Published in: Technology
1 Comment
1 Like
Statistics
Notes
  • Well presented and created, interesting presentation
    http://www.trainingreiki.com/
    http://www.trainingreiki.com/reiki-training.php
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
3,135
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Plone: Event Driven Programming"

  1. 1. Event Driven Programming in Plone Matt Hamilton ...or how to extend Plone the lazy way Matt Hamilton Technical Director, Netsight
  2. 2. Who am I? - Matt Hamilton - Technical Director of Netsight Internet Solutions - A Plone ‘integrator’ matth@netsight.co.uk @hammertoe Matt Hamilton European Plone Symposium 2010, Sorrento 2
  3. 3. Who is this talk for? - Integrators - Those newish to Plone, they can assemble together a site from a number of products, but don't really want to alter them - I consider myself 'experienced' but this was a bit of an epiphany for me Matt Hamilton European Plone Symposium 2010, Sorrento 3
  4. 4. What is the problem? ? Matt Hamilton European Plone Symposium 2010, Sorrento 4
  5. 5. What is the problem? I want to change the functionality of an existing product, but don't want to change the guts of it. Matt Hamilton European Plone Symposium 2010, Sorrento 5
  6. 6. What is the problem? Came from a real life problem. Developing Netsight's new website. Wanted to use plone.app.discussion, but needed to add spam checking on comments Matt Hamilton European Plone Symposium 2010, Sorrento 6
  7. 7. What is the problem? Matt Hamilton European Plone Symposium 2010, Sorrento 7
  8. 8. What is the problem? plone.app.discussion has captcha support already, but I wanted to add Akismet support, but no easy extension point Matt Hamilton European Plone Symposium 2010, Sorrento 8
  9. 9. Admittedly, I could have offered to refactor the whole p.a.discussion code ;) Matt Hamilton European Plone Symposium 2010, Sorrento 9
  10. 10. Considered Approaches - I could subclass the product and override it ➡ A lot of boiler-plate for small change - I could use an adapter and adapt the behaviour ➡ No adapter lookup where I needed it - I could 'just hack it in the original code' ➡ Yuck! Maintainability nightmare Matt Hamilton European Plone Symposium 2010, Sorrento 10
  11. 11. Eureka! Matt Hamilton European Plone Symposium 2010, Sorrento 11
  12. 12. Eureka! Use Events! Matt Hamilton European Plone Symposium 2010, Sorrento 12
  13. 13. Eureka! - I realised I could leave p.a.discussion alone, and just listen for an event for when a comment is added and then check it for spam - I can listen for an event from Plone and then do the behaviour afterwards Matt Hamilton European Plone Symposium 2010, Sorrento 13
  14. 14. Advantages - Leave the existing code alone - Work around lack of suitable extension point - Very little boilerplate code - All happens in same transaction still Matt Hamilton European Plone Symposium 2010, Sorrento 14
  15. 15. Zope's Event System - Events - Subscribers Matt Hamilton European Plone Symposium 2010, Sorrento 15
  16. 16. A Simple Event in configure.zcml: <subscriber for=".interfaces.IMyObject .interfaces.IMyEvent" handler=".events.myEventHandler" /> in events.py: def myEventHandler( object, event): object.doSomeThing() Matt Hamilton European Plone Symposium 2010, Sorrento 16
  17. 17. Event Simpler Event in events.py: from five import grok from interfaces import IMyObject, IMyEvent @@grok.subscribe(IMyObject, IMyEvent) def myEventHandler( object, event): object.doSomeThing() Matt Hamilton European Plone Symposium 2010, Sorrento 17
  18. 18. Example - Spam Checking in events.py: from five import grok from plone.app.discussion.interfaces import IComment from zope.lifecycleevent.interfaces import IObjectAddedEvent @@grok.subscribe(IComment, IObjectAddedEvent) def checkForSpam( comment, event): wf = getToolByName(comment, 'portal_workflow') if is_spam(comment.text): wf.doActionFor(comment, ‘spam’) Matt Hamilton European Plone Symposium 2010, Sorrento 18
  19. 19. Next Example - GetPaid GetPaid is an eCommerce add-on to Plone ➡ Allows you to mark any piece of content as ‘buyable’ Matt Hamilton European Plone Symposium 2010, Sorrento 19
  20. 20. PloneConf 2010 Registration User clicks ‘register’ User fills in ‘add attendee’ form, and hits submit Event fired indicating object added to container Event subscriber marks item as buyable, adds to shopping cart, and then redirects to cart view Matt Hamilton European Plone Symposium 2010, Sorrento 20
  21. 21. GetPaid Example from Products.CMFCore.utils import getToolByName from getpaid.core.interfaces import workflow_states import interfaces def handlePaymentReceived( order, event ):     if event.destination !=workflow_states.order.finance.CHARGED:         return             for item in order.shopping_cart.values():         ob = item.resolve()         workflow = getToolByName( ob, 'portal_workflow')         state = workflow.getInfoFor( ob, 'review_state' )                   if state == 'published':             return         workflow.doActionFor( ob, 'publish') Matt Hamilton European Plone Symposium 2010, Sorrento 21
  22. 22. Other Ideas - Whenever a Folder is created, add some default content to it - When a Page is added, set some metadata fields - When an Event is added, check that an expiry date has been set no more than 12 months in the future Matt Hamilton European Plone Symposium 2010, Sorrento 22
  23. 23. Questions? Matt Hamilton matth@netsight.co.uk
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×