changeset 2091:7af58454aafa

Send an event when user changes her jabber id, force jid to be unique.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Sun, 03 Jun 2007 21:56:02 +0200
parents 9fec7760489e
children 937da10cb5b3
files MoinMoin/config/multiconfig.py MoinMoin/events/JabberNotification.py MoinMoin/events/__init__.py MoinMoin/user.py MoinMoin/userform.py
diffstat 5 files changed, 62 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/config/multiconfig.py	Sun Jun 03 20:30:08 2007 +0200
+++ b/MoinMoin/config/multiconfig.py	Sun Jun 03 21:56:02 2007 +0200
@@ -462,6 +462,7 @@
 
     user_autocreate = False # do we auto-create user profiles
     user_email_unique = True # do we check whether a user's email is unique?
+    user_jid_unique = True # do we check whether a user's email is unique?
 
     # a regex of HTTP_USER_AGENTS that should be excluded from logging
     # and receive a FORBIDDEN for anything except viewing a page
--- a/MoinMoin/events/JabberNotification.py	Sun Jun 03 20:30:08 2007 +0200
+++ b/MoinMoin/events/JabberNotification.py	Sun Jun 03 21:56:02 2007 +0200
@@ -9,7 +9,7 @@
 """
 
 import xmlrpclib
-from MoinMoin.events import PageEvent, PageChangedEvent
+from MoinMoin.events import PageEvent, PageChangedEvent, JabberIDSetEvent
 from MoinMoin.events.notification_common import page_changed_notification
 
 
@@ -20,6 +20,27 @@
 def handle(event):
     global server
 
+    cfg = event.request.cfg
+
+    # Check for desired event type and if notification bot is configured
+    if cfg.jabber_enabled is None:
+        return
+    
+    # Create an XML RPC server object only if it doesn't exist
+    if server is None:
+        server = xmlrpclib.Server("http://" + cfg.bot_host)
+    
+    if isinstance(event, PageEvent):
+        handle_page_changed(event)
+    elif isinstance(event, JabberIDSetEvent):
+        handle_jid_changed(event)
+
+def handle_jid_changed(event):
+    print "JID changed"
+
+def handle_page_changed(event):
+    """ Handles events related to page changes """
+    
     request = event.request
     trivial = event.trivial
     comment = event.comment
@@ -28,16 +49,7 @@
     
     _ = request.getText
     
-    # Check for desired event type and if notification bot is configured
-    if not isinstance(event, PageEvent) or cfg.jabber_enabled is None:
-        return
-    
-    # Create an XML RPC server object only if it doesn't exist
-    if server is None:
-        server = xmlrpclib.Server("http://" + cfg.bot_host)
-    
     subscribers = page.getSubscribers(request, return_users=1, trivial=event.trivial)
-    
     if subscribers:
         # get a list of old revisions, and append a diff
         revisions = page.getRevList()
@@ -47,7 +59,7 @@
         for lang in subscribers:
             jids = [u.jid for u in subscribers[lang]]
             names = [u.name for u in subscribers[lang]]
-            jabberok, status = sendNotification(request, page, comment, jids, lang, revisions, trivial)
+            jabberok, status = send_notification(request, page, comment, jids, lang, revisions, trivial)
             recipients = ", ".join(names)
             results.append(_('[%(lang)s] %(recipients)s: %(status)s') % {
                 'lang': lang, 'recipients': recipients, 'status': status})
@@ -59,8 +71,7 @@
     # No notifications sent, no message.
     return ''
 
-
-def sendNotification(request, page, comment, jids, message_lang, revisions, trivial):
+def send_notification(request, page, comment, jids, message_lang, revisions, trivial):
     """ Send notifications for a single language.
 
     @param comment: editor's comment given when saving the page
--- a/MoinMoin/events/__init__.py	Sun Jun 03 20:30:08 2007 +0200
+++ b/MoinMoin/events/__init__.py	Sun Jun 03 21:56:02 2007 +0200
@@ -21,20 +21,19 @@
 modules = pysupport.getPackageModules(__file__)
 
 
-class Observable:
+class Event:
     """A class handling information common to all events."""
-    
     def __init__(self, request):
         self.request = request
 
         
-class PageEvent(Observable):
+class PageEvent(Event):
     """An event related to a page change"""
 
         
 class PageChangedEvent(PageEvent):
     def __init__(self, request, page, comment, trivial):
-        Observable.__init__(self, request)
+        Event.__init__(self, request)
         self.page = page
         self.comment = comment
         self.trivial = trivial
@@ -50,7 +49,7 @@
 
 class PageRevertedEvent(PageEvent):
     def __init__(self, request, pagename, previous, current):
-        Observable.__init__(self, request)
+        Event.__init__(self, request)
         self.pagename = pagename
         self.previous = previous
         self.current = current    
@@ -58,11 +57,18 @@
 
 class SubscribedToPageEvent(PageEvent):
     def __init__(self, request, pagename, username):
-        Observable.__init__(self, request)    
+        Event.__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
+        
+
 def register_handlers(cfg):
     """Create a list of available event handlers.
     
--- a/MoinMoin/user.py	Sun Jun 03 20:30:08 2007 +0200
+++ b/MoinMoin/user.py	Sun Jun 03 21:56:02 2007 +0200
@@ -43,7 +43,7 @@
 
 def get_by_filter(request, filter):
     """ Searches for an user with a given filter """
-    for uind in getUserList(request):
+    for uid in getUserList(request):
         theuser = User(request, uid)
         if filter(theuser):
             return theuser
--- a/MoinMoin/userform.py	Sun Jun 03 20:30:08 2007 +0200
+++ b/MoinMoin/userform.py	Sun Jun 03 21:56:02 2007 +0200
@@ -9,6 +9,7 @@
 
 import time
 from MoinMoin import user, util, wikiutil
+from MoinMoin.events import send_event, JabberIDSetEvent
 from MoinMoin.widget import html
 
 _debug = 0
@@ -226,13 +227,28 @@
 
             # Email should be unique - see also MoinMoin/script/accounts/moin_usercheck.py
             if theuser.email and self.request.cfg.user_email_unique:
-                users = user.getUserList(self.request)
-                for uid in users:
-                    if uid == theuser.id:
-                        continue
-                    thisuser = user.User(self.request, uid, auth_method='userform:283')
-                    if thisuser.email == theuser.email:
-                        return _("This email already belongs to somebody else.")
+                other = user.get_by_email_address(self.request, theuser.email)
+                if other is not None and other.id != theuser.id:
+                    return _("This email already belongs to somebody else.")
+                    
+        if not 'jid' in theuser.auth_attribs:
+            # try to get the jid
+            jid = wikiutil.clean_input(form.get('jid', [theuser.jid])[0]).strip()
+            
+            jid_changed = False
+            if theuser.jid != jid:
+                jid_changed = True
+                
+            theuser.jid = jid
+            
+            if theuser.jid and self.request.cfg.user_jid_unique:
+                other = user.get_by_jabber_id(self.request, theuser.jid)
+                if other is not None and other.id != theuser.id:
+                    return _("This jabber id already belongs to somebody else.")
+            
+            if jid_changed:
+                event = JabberIDSetEvent(self.request, theuser.jid)
+                send_event(event)
 
         if not 'aliasname' in theuser.auth_attribs:
             # aliasname
@@ -284,7 +300,7 @@
         already_handled = ['name', 'password', 'password2', 'email',
                            'aliasname', 'edit_rows', 'editor_default',
                            'editor_ui', 'tz_offset', 'datetime_fmt',
-                           'theme_name', 'language']
+                           'theme_name', 'language', 'jid']
         for field in self.cfg.user_form_fields:
             key = field[0]
             if ((key in self.cfg.user_form_disable)