view MoinMoin/events/ @ 2329:9a71ea0c941b

Add support for PageRenamed event. Not tested yet.
author Karol Nowak <>
date Tue, 26 Jun 2007 12:43:11 +0200
parents a43db0817020
children 10be78f1fa97
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
    MoinMoin - event (notification) framework

    This code abstracts event handling in MoinMoin,
    currently for notifications. It implements the observer pattern.

    @copyright: 2007 by Karol Nowak <>
    @license: GNU GPL, see COPYING for details.

import logging

from MoinMoin import wikiutil
from MoinMoin.util import pysupport
from MoinMoin.wikiutil import PluginAttributeError

# Create a list of extension actions from the package directory
modules = pysupport.getPackageModules(__file__)

# A module-wide Logger, don't use it from outside
logger = logging.getLogger("events")

class Event(object):
    """A class handling information common to all events."""
    def __init__(self, request):
        self.request = request

class PageEvent(Event):
    """An event related to a page change"""
    def __init__(self, request):
        Event.__init__(self, request)

class PageChangedEvent(PageEvent):
    description = u"""Page has been modified (edit, creation, deletion)"""
    req_superuser = False
    def __init__(self, request, page, comment, trivial):
        PageEvent.__init__(self, request) = page
        self.comment = comment
        self.trivial = trivial

class PageRenamedEvent(PageEvent):
    description = u"""Page has been renamed"""
    req_superuser = False
    def __init__(self, request, page, old_page, comment):
        PageEvent.__init__(self, request) = page
        self.old_page = old_page
        self.comment = comment

class PageDeletedEvent(PageEvent):
    description = u"""Page has been deleted"""
    req_superuser = False
    def __init__(self, request, page, comment):
        PageEvent.__init__(self, request)
        self.request = request = page
        self.comment = comment

class FileAttachedEvent(PageEvent):
    description = u"""A new attachment has been added"""
    req_superuser = False
    def __init__(self, request, pagename, attachment_name, size):
        PageEvent.__init__(self, request)
        self.request = request
        self.pagename = pagename
        self.attachment_name = attachment_name
        self.size = size

class PageRevertedEvent(PageEvent):
    description = u"""A page has been reverted to a previous state"""
    req_superuser = False
    def __init__(self, request, pagename, previous, current):
        PageEvent.__init__(self, request)
        self.pagename = pagename
        self.previous = previous
        self.current = current    

class SubscribedToPageEvent(PageEvent):
    description = u"""An user has subscribed to a page"""
    req_superuser = True
    def __init__(self, request, pagename, username):
        PageEvent.__init__(self, request)    
        self.pagename = pagename
        self.username = username

class JabberIDSetEvent(Event):
    """ Sent when user changes her Jabber ID """
    def __init__(self, request, jid):
        Event.__init__(self, request)
        self.jid = jid
class JabberIDUnsetEvent(Event):
    """ Sent when Jabber ID is no longer used
    Obviously this will be usually sent along with JabberIDSetEvent,
    because we require user's jabber id to be unique by default.
    def __init__(self, request, jid):
        Event.__init__(self, request)
        self.jid = jid
class UserCreatedEvent(Event):
    """ Sent when a new user has been created """
    description = u"""A new account has been created"""
    req_superuser = True
    def __init__(self, request, user):
        Event.__init__(self, request)
        self.user = user
class EventResult:
    """ This is a base class for messages passed from event handlers """
def get_handlers(cfg):
    """Create a list of available event handlers.
    Each handler is a handle() function defined in an plugin,
    pretty much like in case of actions.
    TODO: maybe make it less dumb? ;-)
    event_handlers = []
    names = wikiutil.getPlugins("events", cfg)

    for name in names:
            handler = wikiutil.importPlugin(cfg, "events", name, "handle")
        except PluginAttributeError:
            handler = None
        if handler is not None:
    return event_handlers

def send_event(event):
    """Function called from outside to process an event
    @return: a list of messages returned by handlers
    @rtype: list
    # A list of messages generated by event handlers, passed back to caller
    msg = []
    cfg = event.request.cfg
    # Try to handle the event with each available handler (for now)
    for handle in cfg.event_handlers:
        retval = handle(event)
        if retval:
    return msg

def get_subscribable_events():
    """Create and return a list of user-visible events
    @return: A list of user-visible events described by dictionaries
    @rtype: dict
    defs = globals()
    subscribable_events = {}    
    for ev in defs.values():
        if type(ev) is type and issubclass(ev, Event) and ev.__dict__.has_key("description"):
            subscribable_events[ev.__name__] = {'desc': ev.description,
                                                 'superuser': ev.req_superuser}
    return subscribable_events