changeset 2356:1d60f9aa07d1

Email notification about new attachments. Some i18n issues left to be fixed.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Wed, 11 Jul 2007 02:50:00 +0200
parents ebc8fc2a482a
children 148862d36d4a 6a24e3a77aea
files MoinMoin/action/AttachFile.py MoinMoin/events/__init__.py MoinMoin/events/emailnotify.py MoinMoin/events/jabbernotify.py MoinMoin/events/notification.py
diffstat 5 files changed, 134 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Tue Jul 10 20:56:18 2007 +0200
+++ b/MoinMoin/action/AttachFile.py	Wed Jul 11 02:50:00 2007 +0200
@@ -31,6 +31,7 @@
 from MoinMoin.Page import Page
 from MoinMoin.util import filesys, timefuncs
 from MoinMoin.events import FileAttachedEvent, send_event
+import MoinMoin.events.notification as notification
 
 action_name = __name__.split('.')[-1]
 
@@ -222,7 +223,7 @@
         _addLogEntry(request, 'ATTNEW', pagename, target)
 
         event = FileAttachedEvent(request, pagename, target, len(filecontent))
-        results = events.send_event(event)
+        results = send_event(event)
 
         recipients = []
         for result in results:
--- a/MoinMoin/events/__init__.py	Tue Jul 10 20:56:18 2007 +0200
+++ b/MoinMoin/events/__init__.py	Wed Jul 11 02:50:00 2007 +0200
@@ -77,11 +77,11 @@
     description = u"""A new attachment has been added"""
     req_superuser = False
 
-    def __init__(self, request, pagename, attachment_name, size):
+    def __init__(self, request, pagename, name, size):
         PageEvent.__init__(self, request)
         self.request = request
         self.pagename = pagename
-        self.attachment_name = attachment_name
+        self.name = name
         self.size = size
 
 
--- a/MoinMoin/events/emailnotify.py	Tue Jul 10 20:56:18 2007 +0200
+++ b/MoinMoin/events/emailnotify.py	Wed Jul 11 02:50:00 2007 +0200
@@ -17,7 +17,7 @@
 from MoinMoin import user
 from MoinMoin.Page import Page
 from MoinMoin.mail import sendmail
-from MoinMoin.events import PageChangedEvent, UserCreatedEvent
+from MoinMoin.events import PageChangedEvent, UserCreatedEvent, FileAttachedEvent
 from MoinMoin.user import User, getUserList
 import MoinMoin.events.notification as notification
 
@@ -103,7 +103,7 @@
     sitename = event.request.cfg.sitename
     username = event.user.name
 
-    data = notification.user_created_message(sitename, username, email)
+    data = notification.user_created_message(event.request, sitename, username, email)
 
     for id in user_ids:
         usr = User(event.request, id=id)
@@ -111,11 +111,41 @@
             continue
 
         # Currently send this only to super users
-        if usr.isSuperUser() and event_name in usr.subscribed_events:
+        if usr.isSuperUser() and event_name in usr.subscribed_events and usr.notify_by_email:
             emails.append(usr.email)
 
     send_notification(event.request, from_address, emails, data)
 
+
+def handle_file_attached(event):
+    """Sends an email to super users that have subscribed to this event type"""
+
+    names = set()
+    _ = event.request.getText
+    event_name = event.__class__.__name__
+    from_address = event.request.cfg.mail_from
+    request = event.request
+    page = Page(request, event.pagename)
+
+    subscribers = page.getSubscribers(request, return_users=1)
+    notification.filter_subscriber_list(event, subscribers, False)
+    data = notification.attachment_added(request, event.pagename, event.name, event.size)
+
+    for lang in subscribers:
+        emails = []
+
+        for usr in subscribers[lang]:
+            if usr.notify_by_email and event_name in usr.subscribed_events:
+                emails.append(usr.email)
+            else:
+                continue
+
+            if send_notification(request, from_address, emails, data):
+                names.update(usr.name)
+
+    return notification.Success(names)
+
+
 def handle(event):
     """An event handler"""
 
@@ -126,3 +156,5 @@
         return notify_subscribers(event.request, event.page, event.comment, event.trivial)
     elif isinstance(event, UserCreatedEvent):
         return handle_user_created(event)
+    elif isinstance(event, FileAttachedEvent):
+        return handle_file_attached(event)
--- a/MoinMoin/events/jabbernotify.py	Tue Jul 10 20:56:18 2007 +0200
+++ b/MoinMoin/events/jabbernotify.py	Wed Jul 11 02:50:00 2007 +0200
@@ -64,37 +64,31 @@
         ev.logger.error(_("Low-level communication error: $s"), str(err))
 
 
-def _filter_subscriber_list(event, subscribers):
-    """Filter a list of page subscribers to honor event subscriptions
-
-    @param subscribers: list of subscribers (dict of lists, language is the key)
-    @type subscribers: dict
-
-    """
-    event_name = event.__class__.__name__
-
-    # Filter the list by removing users who don't want to receive
-    # notifications about this type of event
-    for lang in subscribers.keys():
-        userlist = []
-
-        for usr in subscribers[lang]:
-            if event_name in usr.subscribed_events and usr.notify_by_jabber:
-                userlist.append(usr)
-
-        subscribers[lang] = userlist
-
 def handle_file_attached(event):
     """Handles event sent when a file is attached to a page"""
 
+    names = set()
     request = event.request
     page = Page(request, event.pagename)
+    event_name = event.__class__.__name__
 
     subscribers = page.getSubscribers(request, return_users=1)
-    _filter_subscriber_list(event, subscribers)
+    notification.filter_subscriber_list(event, subscribers, True)
 
-    return page_change("attachment_added", request, page, subscribers, \
-                       attach_name=event.attachment_name, attach_size=event.size)
+    for lang in subscribers.keys():
+        jids = []
+        data = notification.attachment_added(request, event.pagename, event.name, event.size)
+
+        for usr in subscribers[lang]:
+            if usr.notify_by_jabber and usr.jid and event_name in usr.subscribed_events:
+                jids.append(usr.jid)
+            else:
+                continue
+
+            if send_notification(request, jids, data['body'], data['subject']):
+                names.update(usr.name)
+
+    return notification.Success(names)
 
 
 def handle_page_changed(event):
@@ -103,7 +97,7 @@
     page = event.page
 
     subscribers = page.getSubscribers(request, return_users=1, trivial=event.trivial)
-    _filter_subscriber_list(event, subscribers)
+    notification.filter_subscriber_list(event, subscribers, True)
     return page_change("page_changed", request, page, subscribers, \
                        revisions=page.getRevList(), comment=event.comment)
 
@@ -115,7 +109,7 @@
     page = event.page
 
     subscribers = page.getSubscribers(request, return_users=1)
-    _filter_subscriber_list(event, subscribers)
+    notification.filter_subscriber_list(event, subscribers, True)
     return page_change("page_deleted", request, page, subscribers)
 
 def handle_page_renamed(event):
@@ -126,7 +120,7 @@
     old_name = event.old_page.page_name
 
     subscribers = page.getSubscribers(request, return_users=1)
-    _filter_subscriber_list(event, subscribers)
+    notification.filter_subscriber_list(event, subscribers, True)
     return page_change("page_renamed", request, page, subscribers, oldname=old.page_name)
 
 
@@ -136,13 +130,13 @@
     jids = []
     user_ids = getUserList(event.request)
     event_name = event.__class__.__name__
-    
+
     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)
-    
+
+    data = notification.user_created_message(event.request, sitename, username, email)
+
     for id in user_ids:
         usr = User(event.request, id=id)
         if not usr.notify_by_jabber:
--- a/MoinMoin/events/notification.py	Tue Jul 10 20:56:18 2007 +0200
+++ b/MoinMoin/events/notification.py	Wed Jul 11 02:50:00 2007 +0200
@@ -9,6 +9,7 @@
     @license: GNU GPL, see COPYING for details.
 """
 
+import MoinMoin.user
 from MoinMoin import user, wikiutil
 from MoinMoin.Page import Page
 from MoinMoin.events import EventResult
@@ -69,10 +70,6 @@
                     'rev2': str(revisions[0]),
                     'rev1': str(revisions[1])}
 
-    if msgtype == "attachment_added":
-        attachlink = request.getBaseURL() + \
-                        getAttachUrl(page.page_name, kwargs['attach_name'], request)
-
     pagelink = request.getQualifiedURL(page.url(request, querystr, relative=False))
 
     if msgtype == "page_changed":
@@ -98,22 +95,6 @@
             else:
                 messageBody = messageBody + _("No differences found!\n", formatted=False)
 
-    elif msgtype == "attachment_added":
-        messageBody = _("Dear Wiki user,\n\n"
-        'You have subscribed to a wiki page "%(sitename)s" for change notification.\n\n'
-        "An attachment has been added to the following page by %(editor)s:\n"
-        "Following detailed information is available:\n"
-        "Attachment name: %(attach_name)s\n"
-        "Attachment size: %(attach_size)s\n"
-        "Download link: %(attach_get)s", formatted=False) % {
-            'editor': user.getUserIdentification(request),
-            'pagelink': pagelink,
-            'sitename': page.cfg.sitename or request.getBaseURL(),
-            'attach_name': kwargs['attach_name'],
-            'attach_size': kwargs['attach_size'],
-            'attach_get': attachlink,
-        }
-
     elif msgtype == "page_deleted":
         messageBody = _("Dear wiki user,\n\n"
             'You have subscribed to a wiki page "%(sitename)s" for change notification.\n\n'
@@ -137,20 +118,86 @@
     else:
         raise UnknownChangeType()
 
-    if 'comment' in kwargs and kwargs['comment'] is not None:
+    if 'comment' in kwargs and kwargs['comment']:
         messageBody = messageBody + \
             _("The comment on the change is:\n%(comment)s", formatted=False) % {'comment': kwargs['comment']}
 
     return messageBody
 
-def user_created_message(sitename, username, email):
+def user_created_message(request, sitename, username, email):
+    """Formats a message used to notify about accounts being created
+
+    @return: a dict containing message body and subject
+    """
+    _ = request.getText
     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}
+
+def attachment_added(request, page_name, attach_name, attach_size):
+    """Formats a message used to notify about new attachments
+
+    @return: a dict containing message body and subject
+    """
+    from MoinMoin.action.AttachFile import getAttachUrl
+
+    _ = request.getText
+    page = Page(request, page_name)
+    attachlink = request.getBaseURL() + getAttachUrl(page_name, attach_name, request)
+    pagelink = request.getQualifiedURL(page.url(request, {}, relative=False))
+
+    subject = _("New attachment added to page %(pagename)s on %(sitename)s") % {
+                'pagename': page_name,
+                'sitename': request.cfg.sitename or request.getBaseURL(),
+                }
+
+    body = _("Dear Wiki user,\n\n"
+    'You have subscribed to a wiki page "%(page_name)s" for change notification. '
+    "An attachment has been added to that page by %(editor)s. "
+    "Following detailed information is available:\n\n"
+    "Attachment name: %(attach_name)s\n"
+    "Attachment size: %(attach_size)s\n"
+    "Download link: %(attach_get)s", formatted=False) % {
+        'editor': user.getUserIdentification(request),
+        'pagelink': pagelink,
+        'page_name': page_name,
+        'attach_name': attach_name,
+        'attach_size': attach_size,
+        'attach_get': attachlink,
+    }
+
+    return {'body': body, 'subject': subject}
+
+
+def filter_subscriber_list(event, subscribers, for_jabber):
+    """Filter a list of page subscribers to honor event subscriptions
+
+    @param subscribers: list of subscribers (dict of lists, language is the key)
+    @param for_jabber: require jid and notify_by_jabber
+    @type subscribers: dict
+
+    """
+    event_name = event.__class__.__name__
+
+    # Filter the list by removing users who don't want to receive
+    # notifications about this type of event
+    for lang in subscribers.keys():
+        userlist = []
+
+        if for_jabber:
+            for usr in subscribers[lang]:
+                if usr.jid and usr.notify_by_jabber and event_name in usr.subscribed_events:
+                    userlist.append(usr)
+        else:
+            for usr in subscribers[lang]:
+                if usr.notify_by_email and event_name in usr.subscribed_events:
+                    userlist.append(usr)
+
+        subscribers[lang] = userlist