changeset 2398:f9d087b59fd4

Merge devel.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Sun, 15 Jul 2007 23:41:39 +0200
parents 75817589c437 (current diff) a10ae31a6133 (diff)
children 245181ec9fe2
files
diffstat 3 files changed, 56 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/PageEditor.py	Sun Jul 15 05:45:57 2007 +0200
+++ b/MoinMoin/PageEditor.py	Sun Jul 15 23:41:39 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/_tests/test_PageEditor.py	Sun Jul 15 05:45:57 2007 +0200
+++ b/MoinMoin/_tests/test_PageEditor.py	Sun Jul 15 23:41:39 2007 +0200
@@ -169,3 +169,25 @@
         page = Page(self.request, self.dictPage)
         return page.getPagePath(use_underlay=0, check_create=0)
 
+
+def testSave(request):
+    """Test if saveText() is interrupted if PagePreSave event handler returns Abort"""
+
+    def handler(event):
+        from MoinMoin.events import Abort
+        return Abort("This is just a test")
+
+    def dummy_write(self, *args, **kwargs):
+        print "PageEditor can't save a page if Abort is returned from PreSave event handlers"
+        assert False
+
+    pagename = u'AutoCreatedMoinMoinTemporaryTestPage'
+    testtext = u'ThisIsSomeStupidTestPageText!!'
+
+    cfg = request.cfg
+    cfg.event_handlers = [handler]
+    
+    editor = PageEditor(request, pagename)
+    editor._write_file = dummy_write
+    editor.saveText(testtext, 0)
+
--- a/MoinMoin/events/__init__.py	Sun Jul 15 05:45:57 2007 +0200
+++ b/MoinMoin/events/__init__.py	Sun Jul 15 23:41:39 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.