changeset 2335:abcb89a89ad2

Merge with changes I made when away.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Mon, 02 Jul 2007 13:16:54 +0200
parents 529b17ec4396 (current diff) 88c539512520 (diff)
children 10be78f1fa97
files
diffstat 9 files changed, 127 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/PageEditor.py	Mon Jul 02 13:15:50 2007 +0200
+++ b/MoinMoin/PageEditor.py	Mon Jul 02 13:16:54 2007 +0200
@@ -30,7 +30,7 @@
 from MoinMoin.logfile import editlog, eventlog
 from MoinMoin.util import filesys, timefuncs, web
 from MoinMoin.mail import sendmail
-from MoinMoin.events import PageDeletedEvent, send_event
+from MoinMoin.events import PageDeletedEvent, PageRenamedEvent, send_event
 import MoinMoin.events.notification as notification
 
 # used for merging
@@ -536,7 +536,7 @@
             if request.user.may.write(newpagename):
                 # Save page text with a comment about the old name and log entry
                 savetext = u"## page was copied from %s\n%s" % (self.page_name, savetext)
-                newpage.saveText(savetext, 0, comment=comment, index=0, extra=self.page_name, action='SAVE')
+                newpage.saveText(savetext, 0, comment=comment, index=0, extra=self.page_name, action='SAVE', notify=False)
             else:
                 # if user is  not able to write to the page itselfs we set a log entry only
                 from MoinMoin import packages
@@ -605,7 +605,7 @@
             self.error = None
             # Save page text with a comment about the old name
             savetext = u"## page was renamed from %s\n%s" % (self.page_name, savetext)
-            newpage.saveText(savetext, 0, comment=comment, index=0, extra=self.page_name, action='SAVE/RENAME')
+            newpage.saveText(savetext, 0, comment=comment, index=0, extra=self.page_name, action='SAVE/RENAME', notify=False)
             # delete pagelinks
             arena = newpage
             key = 'pagelinks'
@@ -625,6 +625,9 @@
                 if index.exists():
                     index.remove_item(self.page_name, now=0)
                     index.update_page(newpagename)
+                    
+            event = PageRenamedEvent(request, newpage, comment)
+            send_event(event)
 
             return True, None
         except OSError, err:
@@ -655,7 +658,7 @@
             event = PageDeletedEvent(request, self, comment)
             send_event(event)
             
-            msg = self.saveText(u"deleted\n", 0, comment=comment or u'', index=1, deleted=True)
+            msg = self.saveText(u"deleted\n", 0, comment=comment or u'', index=1, deleted=True, notify=False)
             msg = msg.replace(
                 _("Thank you for your changes. Your attention to detail is appreciated."),
                 _('Page "%s" was successfully deleted!') % (self.page_name,))
@@ -963,6 +966,7 @@
         @keyword action: action for editlog (default: SAVE)
         @keyword index: needs indexing, not already handled (default: 1)
         @keyword deleted: if True, then don't save page content (used by DeletePage, default: False)
+        @keyword notify: if False (default: True), don't send a PageChangedEvent
         @rtype: unicode
         @return: error msg
         """
@@ -971,6 +975,7 @@
         self._save_draft(newtext, rev, **kw)
         action = kw.get('action', 'SAVE')
         deleted = kw.get('deleted', False)
+        notify = kw.get('notify', True)
 
         #!!! need to check if we still retain the lock here
         #!!! rev check is not enough since internal operations use "0"
@@ -1046,19 +1051,20 @@
             self.clean_acl_cache()
             self._save_draft(None, None) # everything fine, kill the draft for this page
 
-            # send notifications
-            from MoinMoin import events
-            e = events.PageChangedEvent(self.request, self, comment, trivial)
-            results = events.send_event(e)
+            if notify:
+                # send notifications
+                from MoinMoin import events
+                e = events.PageChangedEvent(self.request, self, comment, trivial)
+                results = events.send_event(e)
 
-            recipients = set()
-            for result in results:
-                if isinstance(result, notification.Success):
-                    recipients.update(result.recipients)
+                recipients = set()
+                for result in results:
+                    if isinstance(result, notification.Success):
+                        recipients.update(result.recipients)
 
-            if recipients:
-                info = _("Notifications sent to:")
-                msg = msg + "<p>%s %s</p>" % (info, ",".join(recipients))
+                        if recipients:
+                            info = _("Notifications sent to:")
+                            msg = msg + "<p>%s %s</p>" % (info, ",".join(recipients))
 
             if kw.get('index', 1) and request.cfg.xapian_search:
                 from MoinMoin.search.Xapian import Index
--- a/MoinMoin/_tests/test_events.py	Mon Jul 02 13:15:50 2007 +0200
+++ b/MoinMoin/_tests/test_events.py	Mon Jul 02 13:16:54 2007 +0200
@@ -6,8 +6,13 @@
     @license: GNU GPL, see COPYING for details.
 """
 
+import py
+
 import MoinMoin.events as events
 import MoinMoin.events.notification as notification
+import MoinMoin.events.jabbernotify as jabbernotify
+from MoinMoin.Page import Page
+from MoinMoin.user import User
 
 def test_get_handlers(request):
     """Test if there are any event handlers. There should be some internal ones"""
@@ -34,5 +39,48 @@
     
     print "There should be at least a few subscribable events!"
     assert events.get_subscribable_events()
+
+def test_page_change_message(request):
+    page = Page(request, "FrontPage")
+    
+    print "Provided with a dumb change type argument, this should raise an exception!"
+    py.test.raises(notification.UnknownChangeType, notification.page_change_message, 
+                   "StupidType", request, page, "en", revisions=page.getRevList())
+    
+def test_filter_subscriber_list(request):
+    user = User(request)
+    event = events.Event(request)
+    
+    print "User is subscribed to this event and wants to get notified by jabber."
+    print "This means, that he should stay on the list."
+    user.notify_by_jabber = True
+    user.subscribed_events = [events.Event.__name__]
+    subscribers = {"en": [user]}
+    jabbernotify._filter_subscriber_list(event, subscribers)
+    assert subscribers["en"]
+    
+    print "User is subscribed to this event, but doesn't want to get notified by jabber."
+    print "The list should be empty."
+    user.notify_by_jabber = False
+    user.subscribed_events = [events.Event.__name__]
+    subscribers = {"en": [user]}
+    jabbernotify._filter_subscriber_list(event, subscribers)
+    assert not subscribers["en"]
+    
+    print "User is not subscribed to this event, but wants to get notfied by jabber."
+    print "The list should be empty."
+    user.notify_by_jabber = True
+    user.subscribed_events = []
+    subscribers = {"en": [user]}
+    jabbernotify._filter_subscriber_list(event, subscribers)
+    assert not subscribers["en"]
+    
+    print "User is neither subscribed to this event, nor wants jabber notifications."
+    print "The list should be empty."
+    user.notify_by_jabber = False
+    user.subscribed_events = []
+    subscribers = {"en": [user]}
+    jabbernotify._filter_subscriber_list(event, subscribers)
+    assert not subscribers["en"]
     
 coverage_modules = ["MoinMoin.events"]
--- a/MoinMoin/action/revert.py	Mon Jul 02 13:15:50 2007 +0200
+++ b/MoinMoin/action/revert.py	Mon Jul 02 13:16:54 2007 +0200
@@ -27,7 +27,7 @@
 
         revstr = '%08d' % rev
         try:
-            msg = newpg.saveText(pg.get_raw_body(), 0, extra=revstr, action="SAVE/REVERT")
+            msg = newpg.saveText(pg.get_raw_body(), 0, extra=revstr, action="SAVE/REVERT", notify=False)
             pg = newpg
         except newpg.SaveError, msg:
             msg = unicode(msg)
--- a/MoinMoin/config/multiconfig.py	Mon Jul 02 13:15:50 2007 +0200
+++ b/MoinMoin/config/multiconfig.py	Mon Jul 02 13:16:54 2007 +0200
@@ -683,6 +683,14 @@
         
         # if we are to use the jabber bot, instantiate a server object for future use
         if self.jabber_enabled:
+            
+            errmsg = "You must set a (long) secret string to send notifications!"
+            try:
+                if not self.secret:
+                    raise error.ConfigurationError(errmsg)
+            except AttributeError, err:
+                    raise error.ConfigurationError(errmsg)
+            
             from xmlrpclib import Server
             self.notification_server = Server(self.notification_bot_uri, )
             
--- a/MoinMoin/events/__init__.py	Mon Jul 02 13:15:50 2007 +0200
+++ b/MoinMoin/events/__init__.py	Mon Jul 02 13:16:54 2007 +0200
@@ -49,7 +49,15 @@
 
         
 class PageRenamedEvent(PageEvent):
-    pass
+    
+    description = u"""Page has been renamed"""
+    req_superuser = False
+    
+    def __init__(self, request, page, old_page, comment):
+        PageEvent.__init__(self, request)
+        self.page = page
+        self.old_page = old_page
+        self.comment = comment
 
 
 class PageDeletedEvent(PageEvent):
--- a/MoinMoin/events/jabbernotify.py	Mon Jul 02 13:15:50 2007 +0200
+++ b/MoinMoin/events/jabbernotify.py	Mon Jul 02 13:16:54 2007 +0200
@@ -28,10 +28,6 @@
     # Check for desired event type and if notification bot is configured
     if not cfg.jabber_enabled:
         return
-    
-    if not cfg.secret:
-        errmsg = "You must set a (long) secret string to send notifications!"
-        raise error.ConfigurationError(errmsg)
 
     if isinstance(event, ev.PageChangedEvent):
         return handle_page_changed(event)
@@ -41,6 +37,8 @@
         return handle_file_attached(event)
     elif isinstance(event, ev.PageDeletedEvent):
         return handle_page_deleted(event)
+    elif isinstance(event, ev.PageRenamedEvent):
+        return handle_page_renamed(event)
     elif isinstance(event, ev.UserCreatedEvent):
         return handle_user_created(event)
     
@@ -88,7 +86,6 @@
     """Handles event sent when a file is attached to a page"""
     
     request = event.request
-    server = request.cfg.notification_server
     page = Page(request, event.pagename)
     
     subscribers = page.getSubscribers(request, return_users=1)
@@ -101,7 +98,6 @@
 def handle_page_changed(event):
     """ Handles events related to page changes """
     request = event.request
-    server = request.cfg.notification_server
     page = event.page
     
     subscribers = page.getSubscribers(request, return_users=1, trivial=event.trivial)    
@@ -114,13 +110,23 @@
     """Handles event sent when a page is deleted"""
     
     request = event.request
-    server = request.cfg.notification_server
     page = event.page
     
     subscribers = page.getSubscribers(request, return_users=1)
     _filter_subscriber_list(event, subscribers)
     return page_change("page_deleted", request, page, subscribers)
 
+def handle_page_renamed(event):
+    """Handles event sent when a page is renamed"""
+    
+    request = event.request
+    page = event.page
+    old_name = event.old_page.page_name
+    
+    subscribers = page.getSubscribers(request, return_users=1)
+    _filter_subscriber_list(event, subscribers)
+    return page_change("page_renamed", request, page, subscribers, oldname=old.page_name)
+
 
 def handle_user_created(event):
     """Handles an event sent when a new user is being created"""
--- a/MoinMoin/events/notification.py	Mon Jul 02 13:15:50 2007 +0200
+++ b/MoinMoin/events/notification.py	Mon Jul 02 13:16:54 2007 +0200
@@ -123,6 +123,17 @@
                 'pagelink': pagelink,
                 'sitename': page.cfg.sitename or request.getBaseURL(),
         }
+            
+    elif msgtype == "page_renamed":
+        messageBody = _("Dear wiki user,\n\n"
+            'You have subscribed to a wiki page "%(sitename)s" for change notification.\n\n'
+            "The following page has been renamed from %(oldname)s by %(editor)s:\n"
+            "%(pagelink)s\n\n", formatted=False) % {
+                'editor': page.uid_override or user.getUserIdentification(request),
+                'pagelink': pagelink,
+                'sitename': page.cfg.sitename or request.getBaseURL(),
+                'oldname': kwargs['old_name']
+        }
     else:
         raise UnknownChangeType()
     
--- a/MoinMoin/user.py	Mon Jul 02 13:15:50 2007 +0200
+++ b/MoinMoin/user.py	Mon Jul 02 13:16:54 2007 +0200
@@ -285,8 +285,8 @@
         self.auth_method = kw.get('auth_method', 'internal')
         self.auth_attribs = kw.get('auth_attribs', ())
         self.bookmarks = {} # interwikiname: bookmark
-        self.notify_be_email = True
-        self.notify_be_jabber = False
+        self.notify_by_email = True
+        self.notify_by_jabber = False
 
         # create some vars automatically
         self.__dict__.update(self._cfg.user_form_defaults)
--- a/jabberbot/xmlrpcbot.py	Mon Jul 02 13:15:50 2007 +0200
+++ b/jabberbot/xmlrpcbot.py	Mon Jul 02 13:16:54 2007 +0200
@@ -75,7 +75,7 @@
         token = self.connection.getAuthToken(jid, self.config.secret)
         if token:
             self.token = token
-    
+                
     def _xmlrpc_decorator(function):
         """A decorator function, which adds some maintenance code
         
@@ -86,19 +86,25 @@
         def wrapped_func(self, command):
             self.token = None
             self.multicall = MultiCall(self.connection)
-            self.get_auth_token(command.jid)
+            jid = command.jid
             
-            if self.token:
-                self.multicall.applyAuthToken(self.token)
-   
             try:
                 try:
+                    self.get_auth_token(command.jid)
+                    if self.token:
+                        self.multicall.applyAuthToken(self.token)
+                        
                     function(self, command)
                     self.commands_out.put_nowait(command)
                 except xmlrpclib.Fault, fault:
                     msg = u"""Your request has failed. The reason is:\n%s"""
-                    notification = cmd.NotificationCommand(command.jid, msg % (fault.faultString, ))
+                    notification = cmd.NotificationCommand([jid], msg % (fault.faultString, ))
                     self.commands_out.put_nowait(notification)
+                except xmlrpclib.Error, err:
+                    msg = u"""A serious error occured while processing your request:\n%s"""
+                    notification = cmd.NotificationCommand([jid], msg % (str(err), ))
+                    self.commands_out.put_nowait(notification)
+                    
             finally:
                 del self.token
                 del self.multicall
@@ -255,8 +261,8 @@
     def send_notification(self, jids, text):
         """Instructs the XMPP component to send a notification
         
-        @param jid: a jid to send a message to (bare jid)
-        @type jid: str or unicode
+        @param jids: a list of JIDs to send a message to (bare JIDs)
+        @type jids: a list of str or unicode
         @param text: a message body
         @type text: unicode