changeset 2354:ebc8fc2a482a

Add user_created_message() to share some code. Some fixes to support subjects.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Tue, 10 Jul 2007 20:56:18 +0200
parents d9da7dbce6cd
children 91b9d9e40e07 1d60f9aa07d1
files MoinMoin/events/emailnotify.py MoinMoin/events/jabbernotify.py MoinMoin/events/notification.py jabberbot/commands.py jabberbot/xmlrpcbot.py jabberbot/xmppbot.py
diffstat 6 files changed, 36 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/events/emailnotify.py	Tue Jul 10 18:44:21 2007 +0200
+++ b/MoinMoin/events/emailnotify.py	Tue Jul 10 20:56:18 2007 +0200
@@ -37,14 +37,14 @@
     _ = request.getText
     mailBody = notification.page_change_message("page_changed", request, page, email_lang, comment=comment, revisions=revisions)
 
-    title = _('[%(sitename)s] %(trivial)sUpdate of "%(pagename)s" by %(username)s', formatted=False) % {
+    subject = _('[%(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),
         }
 
-    return {'title': title, 'body': mailBody}
+    return {'subject': subject, 'body': mailBody}
 
 
 def send_notification(request, from_address, emails, data):
@@ -55,7 +55,7 @@
     @rtype int
 
     """
-    return sendmail.sendmail(request, emails, data['title'], data['body'], mail_from=from_address)
+    return sendmail.sendmail(request, emails, data['subject'], data['body'], mail_from=from_address)
 
 
 def notify_subscribers(request, page, comment, trivial):
@@ -93,25 +93,17 @@
 def handle_user_created(event):
     """Sends an email to super users that have subscribed to this event type"""
 
+    emails = []
+    _ = event.request.getText
     user_ids = getUserList(event.request)
-    emails = []
     event_name = event.__class__.__name__
-    email = event.user.email or u"NOT SET"
-    _ = event.request.getText
-    cfg = event.request.cfg
 
-    title = _("New user account created on %(sitename)s") % {'sitename': cfg.sitename or "Wiki"}
-    body = _("""Dear Superuser, a new user has just been created. Details follow:
-    
-    User name: %(username)s
-    Email address: %(useremail)s""", formatted=False) % {
-         'username': event.user.name,
-         'useremail': email,
-         }
+    from_address = event.request.cfg.mail_from
+    email = event.user.email or u"NOT SET"
+    sitename = event.request.cfg.sitename
+    username = event.user.name
 
-    from_address = cfg.mail_from
-    data = {'title': title, 'body': body}
-    emails = []
+    data = notification.user_created_message(sitename, username, email)
 
     for id in user_ids:
         usr = User(event.request, id=id)
--- a/MoinMoin/events/jabbernotify.py	Tue Jul 10 18:44:21 2007 +0200
+++ b/MoinMoin/events/jabbernotify.py	Tue Jul 10 20:56:18 2007 +0200
@@ -136,14 +136,13 @@
     jids = []
     user_ids = getUserList(event.request)
     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
-    """
-
-    email = event.user.email or u"NOT SET"
-
+    sitename = event.request.cfg.sitename
+    username = event.user.name
+    
+    data = notification.user_created_message(sitename, username, email)
+    
     for id in user_ids:
         usr = User(event.request, id=id)
         if not usr.notify_by_jabber:
@@ -153,7 +152,7 @@
         if usr.isSuperUser() and usr.jid and event_name in usr.subscribed_events:
             jids.append(usr.jid)
 
-    send_notification(event.request, jids, msg % (event.user.name, email))
+    send_notification(event.request, jids, msg % (event.user.name, email), data['subject'])
 
 
 def page_change(type, request, page, subscribers, **kwargs):
@@ -176,7 +175,7 @@
         if recipients:
             return notification.Success(recipients)
 
-def send_notification(request, jids, message):
+def send_notification(request, jids, message, subject=""):
     """ Send notifications for a single language.
 
     @param comment: editor's comment given when saving the page
@@ -186,7 +185,7 @@
     server = request.cfg.notification_server
 
     try:
-        server.send_notification(request.cfg.secret, jids, message)
+        server.send_notification(request.cfg.secret, jids, message, subject)
         return True
     except xmlrpclib.Error, err:
         ev.logger.error(_("XML RPC error: %s"), str(err))
--- a/MoinMoin/events/notification.py	Tue Jul 10 18:44:21 2007 +0200
+++ b/MoinMoin/events/notification.py	Tue Jul 10 20:56:18 2007 +0200
@@ -143,3 +143,14 @@
 
     return messageBody
 
+def user_created_message(sitename, username, email):
+    subject = _("New user account created on %(sitename)s") % {'sitename': sitename or "Wiki"}
+    body = _("""Dear Superuser, a new user has just been created. Details follow:
+    
+    User name: %(username)s
+    Email address: %(useremail)s""", formatted=False) % {
+         'username': username,
+         'useremail': email,
+         }
+    
+    return {'subject': subject, 'body': body}
--- a/jabberbot/commands.py	Tue Jul 10 18:44:21 2007 +0200
+++ b/jabberbot/commands.py	Tue Jul 10 20:56:18 2007 +0200
@@ -12,12 +12,13 @@
 # First, XML RPC -> XMPP commands
 class NotificationCommand:
     """Class representing a notification request"""
-    def __init__(self, jids, text):
+    def __init__(self, jids, text, subject=""):
         if type(jids) != list:
             raise Exception("jids argument must be a list!")
 
         self.jids = jids
         self.text = text
+        self.subject = subject
 
 class AddJIDToRosterCommand:
     """Class representing a request to add a new jid to roster"""
--- a/jabberbot/xmlrpcbot.py	Tue Jul 10 18:44:21 2007 +0200
+++ b/jabberbot/xmlrpcbot.py	Tue Jul 10 20:56:18 2007 +0200
@@ -262,7 +262,7 @@
         return protected_func
 
 
-    def send_notification(self, jids, text):
+    def send_notification(self, jids, text, subject):
         """Instructs the XMPP component to send a notification
 
         @param jids: a list of JIDs to send a message to (bare JIDs)
@@ -271,7 +271,7 @@
         @type text: unicode
 
         """
-        command = cmd.NotificationCommand(jids, text)
+        command = cmd.NotificationCommand(jids, text, subject)
         self.commands.put_nowait(command)
         return True
     send_notification.export = True
--- a/jabberbot/xmppbot.py	Tue Jul 10 18:44:21 2007 +0200
+++ b/jabberbot/xmppbot.py	Tue Jul 10 20:56:18 2007 +0200
@@ -235,7 +235,7 @@
         stanza = Presence(to_jid=jid, stanza_type="unsubscribed")
         self.get_stream().send(stanza)
 
-    def send_message(self, jid, text, msg_type=u"chat"):
+    def send_message(self, jid, text, subject="", msg_type=u"chat"):
         """Sends a message
 
         @param jid: JID to send the message to
@@ -244,7 +244,7 @@
         @type jid: pyxmpp.jid.JID
 
         """
-        message = Message(to_jid=jid, body=text, stanza_type=msg_type)
+        message = Message(to_jid=jid, body=text, stanza_type=msg_type, subject=subject)
         self.get_stream().send(message)
 
     def handle_message(self, message):