changeset 2346:bf3ec2aadfb6

Merge with devel.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Tue, 10 Jul 2007 01:20:14 +0200
parents b745ea150288 (current diff) 59d83c1c6ffc (diff)
children 8b4c8ebdd8bc
files
diffstat 2 files changed, 71 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/events/emailnotify.py	Mon Jul 09 00:30:22 2007 +0200
+++ b/MoinMoin/events/emailnotify.py	Tue Jul 10 01:20:14 2007 +0200
@@ -17,32 +17,45 @@
 from MoinMoin import user
 from MoinMoin.Page import Page
 from MoinMoin.mail import sendmail
-from MoinMoin.events import *
+from MoinMoin.events import PageChangedEvent, UserCreatedEvent
+from MoinMoin.user import User, getUserList
 import MoinMoin.events.notification as notification
 
 
-def send_notification(request, page, comment, emails, email_lang, revisions, trivial):
-    """ Send notification email for a single language.
+def prep_page_changed_mail(request, page, comment, email_lang, revisions, trivial):
+    """ Prepare information required for email notification about page change
 
+    @param page: the modified page instance
     @param comment: editor's comment given when saving the page
-    @param emails: list of email addresses
     @param email_lang: language of email
     @param revisions: revisions of this page (newest first!)
     @param trivial: the change is marked as trivial
-    @rtype: int
-    @return: sendmail result
+    @return: dict with email title and body
+    @rtype: dict
+
     """
     _ = request.getText
     mailBody = notification.page_change_message("page_changed", request, page, email_lang, comment=comment, revisions=revisions)
 
-    return sendmail.sendmail(request, emails,
-        _('[%(sitename)s] %(trivial)sUpdate of "%(pagename)s" by %(username)s', formatted=False) % {
+    title = _('[%(sitename)s] %(trivial)sUpdate of "%(pagename)s" by %(username)s', formatted=False) % {
             'trivial': (trivial and _("Trivial ", formatted=False)) or "",
             'sitename': page.cfg.sitename or "Wiki",
             'pagename': page.page_name,
             'username': page.uid_override or user.getUserIdentification(request),
-        },
-        mailBody, mail_from=page.cfg.mail_from)
+        }
+
+    return {'title': title, 'body': mailBody}
+
+
+def send_notification(request, from_address, emails, data):
+    """ Send notification email
+
+    @param emails: list of email addresses
+    @return: sendmail result
+    @rtype int
+
+    """
+    return sendmail.sendmail(request, emails, data['title'], data['body'], mail_from=from_address)
 
 
 def notify_subscribers(request, page, comment, trivial):
@@ -52,9 +65,11 @@
     @param trivial: editor's suggestion that the change is trivial (Subscribers may ignore this)
     @rtype: string
     @return: message, indicating success or errors.
+
     """
     _ = request.getText
     subscribers = page.getSubscribers(request, return_users=1, trivial=trivial)
+    mail_from = page.cfg.mail_from
 
     if subscribers:
         recipients = set()
@@ -66,15 +81,51 @@
         for lang in subscribers:
             emails = [u.email for u in subscribers[lang] if u.notify_by_email]
             names = [u.name for u in subscribers[lang] if u.notify_by_email]
-            result = send_notification(request, page, comment, emails, lang, revisions, trivial)
-            if result:
+            data = prep_page_changed_mail(request, page, comment, lang, revisions, trivial)
+
+            if send_notification(request, mail_from, emails, data):
                 recipients.update(names)
 
         if recipients:
             return notification.Success(recipients)
 
 
+def handle_user_created(event):
+    """Sends an email to super users that have subscribed to this event type"""
+
+    user_ids = getUserList(event.request)
+    emails = []
+    event_name = event.__class__.__name__
+    email = event.user.email or u"NOT SET"
+    _ = event.request.getText
+
+    title = _("New user account created on %(sitename)s")
+    body = _("""Dear Superuser, a new user has just been created. Details follow:
+    User name: %s
+    Email address: %s)""")
+
+    data = {'from': event.request.cfg.mail_from, 'title': title, 'body': body}
+    emails = []
+
+    for id in user_ids:
+        usr = User(event.request, id=id)
+        if not usr.notify_by_email:
+            continue
+
+        # Currently send this only to super users
+        if usr.isSuperUser() and event_name in usr.subscribed_events:
+            emails.append(usr.email)
+
+    send_notification(event.request, emails, data)
+
 def handle(event):
-    if isinstance(event, PageChangedEvent) and event.request.cfg.mail_enabled:
+    """An event handler"""
+
+    if not event.request.cfg.mail_enabled:
+        return
+
+    if isinstance(event, PageChangedEvent):
         return notify_subscribers(event.request, event.page, event.comment, event.trivial)
+    elif isinstance(event, UserCreatedEvent):
+        return handle_user_created(event)
 
--- a/MoinMoin/events/jabbernotify.py	Mon Jul 09 00:30:22 2007 +0200
+++ b/MoinMoin/events/jabbernotify.py	Tue Jul 10 01:20:14 2007 +0200
@@ -23,6 +23,8 @@
 
 
 def handle(event):
+    """An event handler"""
+
     cfg = event.request.cfg
 
     # Check for desired event type and if notification bot is configured
@@ -131,9 +133,10 @@
 def handle_user_created(event):
     """Handles an event sent when a new user is being created"""
 
+    jids = []
     user_ids = getUserList(event.request)
-    jids = []
     event_name = event.__class__.__name__
+    email = event.user.email or u"NOT SET"
     msg = u"""Dear Superuser, a new user has just been created. Details follow:
     User name: %s
     Email address: %s
@@ -147,9 +150,10 @@
             continue
 
         # Currently send this only to super users
-        # TODO: make it possible to disable this notification
         if usr.isSuperUser() and usr.jid and event_name in usr.subscribed_events:
-            send_notification(event.request, [usr.jid], msg % (event.user.name, email))
+            jids.append(usr.jid)
+
+    send_notification(event.request, jids, msg % (event.user.name, email))
 
 
 def page_change(type, request, page, subscribers, **kwargs):