changeset 2396:cf85b6916d17

Add a PagePreSave event; abort page save if handlers return Abort.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Sun, 15 Jul 2007 23:40:40 +0200
parents 0f9b7b20fb9c
children a10ae31a6133
files MoinMoin/PageEditor.py MoinMoin/events/__init__.py
diffstat 2 files changed, 34 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/PageEditor.py	Sun Jul 15 05:40:56 2007 +0200
+++ b/MoinMoin/PageEditor.py	Sun Jul 15 23:40:40 2007 +0200
@@ -30,7 +30,8 @@
 from MoinMoin.logfile import editlog, eventlog
 from MoinMoin.util import filesys, timefuncs, web
 from MoinMoin.mail import sendmail
-from MoinMoin.events import PageDeletedEvent, PageRenamedEvent, PageCopiedEvent, send_event
+from MoinMoin.events import PageDeletedEvent, PageRenamedEvent, PageCopiedEvent
+from MoinMoin.events import PagePreSaveEvent, Abort, send_event
 import MoinMoin.events.notification as notification
 
 # used for merging
@@ -1028,7 +1029,16 @@
                 msg = _("You can't change ACLs on this page since you have no admin rights on it!")
                 raise self.NoAdmin, msg
 
-        # save only if no error occurred (msg is empty)
+        presave = PagePreSaveEvent(request, self, newtext)
+        results = send_event(presave)
+        
+        for result in results:
+            if isinstance(result, Abort):
+                # XXX: this should return a list of messages to the sorrounding context
+                # XXX: rather than dumbly concatenate them. Fix in the future.
+                msg = msg + result.reason
+
+        # save only if no error occurred (msg is empty) and no abort has been requested
         if not msg:
             # set success msg
             msg = _("Thank you for your changes. Your attention to detail is appreciated.")
--- a/MoinMoin/events/__init__.py	Sun Jul 15 05:40:56 2007 +0200
+++ b/MoinMoin/events/__init__.py	Sun Jul 15 23:40:40 2007 +0200
@@ -147,10 +147,32 @@
         Event.__init__(self, request)
         self.user = user
 
+class PagePreSaveEvent(Event):
+    """ Event sent when a page is about to be saved
+
+    This can be used to abort a save, for instance,
+    if handler returns
+
+    """
+    def __init__(self, request, page_editor, new_text):
+        Event.__init__(self, request)
+        self.page_editor = page_editor
+        self.new_text = new_text
+
+
 class EventResult:
     """ This is a base class for messages passed from event handlers """
     pass
 
+class Abort(EventResult):
+    """ Result returned if handler wants to abort operation that sent the event """
+    def __init__(self, reason):
+        """
+        @param reason: human-readable reason of failure
+        """
+        self.reason = reason
+        
+
 def get_handlers(cfg):
     """Create a list of available event handlers.