Back to index

moin  1.9.0~rc2
notification.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - common functions for notification framework
00004 
00005     Code for building messages informing about events (changes)
00006     happening in the wiki.
00007 
00008     @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
00009     @license: GNU GPL, see COPYING for details.
00010 """
00011 
00012 from MoinMoin import user, wikiutil
00013 from MoinMoin.events import EventResult
00014 
00015 
00016 class Result(EventResult):
00017     """ A base class for results of notification handlers"""
00018     pass
00019 
00020 
00021 class Failure(Result):
00022     """ Used to report a failure in sending notifications """
00023     def __init__(self, reason, recipients = None):
00024         """
00025         @param recipients: a set of recipients
00026         @type recipients: set
00027         """
00028         self.reason = reason
00029         self.recipient = None
00030 
00031     def __str__(self):
00032         return self.reason or u""
00033 
00034 
00035 class Success(Result):
00036     """ Used to indicate successfull notifications """
00037 
00038     def __init__(self, recipients):
00039         """
00040         @param recipients: a set of recipients
00041         @type recipients: set
00042         """
00043         self.recipients = recipients
00044 
00045 
00046 class UnknownChangeType(Exception):
00047     """ Used to signal an invalid page change event """
00048     pass
00049 
00050 
00051 def page_link(request, page, querystr):
00052     """Create an absolute url to a given page with optional action
00053 
00054     @param page: a page to link to
00055     @type page: MoinMoin.Page.Page
00056     @param querystr: a dict passed to wikiutil.makeQueryString
00057 
00058     """
00059     return request.getQualifiedURL(page.url(request, querystr))
00060 
00061 
00062 def page_change_message(msgtype, request, page, lang, **kwargs):
00063     """Prepare a notification text for a page change of given type
00064 
00065     @param msgtype: a type of message to send (page_changed, page_renamed, ...)
00066     @type msgtype: str or unicode
00067     @param **kwargs: a dictionary of additional parameters, which depend on msgtype
00068 
00069     @return: dictionary containing data about the changed page
00070     @rtype: dict
00071 
00072     """
00073     _ = lambda text: request.getText(text, lang=lang)
00074     changes = {'page_name': page.page_name, 'revision': str(page.getRevList()[0])}
00075 
00076     if msgtype == "page_changed":
00077         revisions = kwargs['revisions']
00078 
00079     if msgtype == "page_changed":
00080         changes['text'] = _("Dear Wiki user,\n\n"
00081         'You have subscribed to a wiki page or wiki category on "%(sitename)s" for change notification.\n\n'
00082         'The "%(pagename)s" page has been changed by %(editor)s:\n') % {
00083             'pagename': page.page_name,
00084             'editor': page.uid_override or user.getUserIdentification(request),
00085             'sitename': page.cfg.sitename or request.url_root,
00086         }
00087 
00088         # append a diff (or append full page text if there is no diff)
00089         if len(revisions) < 2:
00090             changes['diff'] = _("New page:\n") + page.get_raw_body()
00091         else:
00092             lines = wikiutil.pagediff(request, page.page_name, revisions[1],
00093                                       page.page_name, revisions[0])
00094             if lines:
00095                 changes['diff'] = '\n'.join(lines)
00096             else:
00097                 changes['diff'] = _("No differences found!\n")
00098 
00099     elif msgtype == "page_deleted":
00100         changes['text'] = _("Dear wiki user,\n\n"
00101             'You have subscribed to a wiki page "%(sitename)s" for change notification.\n\n'
00102             'The page "%(pagename)s" has been deleted by %(editor)s:\n\n') % {
00103                 'pagename': page.page_name,
00104                 'editor': page.uid_override or user.getUserIdentification(request),
00105                 'sitename': page.cfg.sitename or request.url_root,
00106         }
00107 
00108     elif msgtype == "page_renamed":
00109         changes['text'] = _("Dear wiki user,\n\n"
00110             'You have subscribed to a wiki page "%(sitename)s" for change notification.\n\n'
00111             'The page "%(pagename)s" has been renamed from "%(oldname)s" by %(editor)s:\n') % {
00112                 'editor': page.uid_override or user.getUserIdentification(request),
00113                 'pagename': page.page_name,
00114                 'sitename': page.cfg.sitename or request.url_root,
00115                 'oldname': kwargs['old_name']
00116         }
00117 
00118         changes['old_name'] = kwargs['old_name']
00119 
00120     else:
00121         raise UnknownChangeType()
00122 
00123     changes['editor'] = page.uid_override or user.getUserIdentification(request)
00124     if 'comment' in kwargs and kwargs['comment']:
00125         changes['comment'] = kwargs['comment']
00126 
00127     return changes
00128 
00129 
00130 def user_created_message(request, _, sitename, username, email):
00131     """Formats a message used to notify about accounts being created
00132 
00133     @return: a dict containing message body and subject
00134     """
00135     subject = _("New user account created on %(sitename)s") % {'sitename': sitename or "Wiki"}
00136     text = _("""Dear Superuser, a new user has just been created on %(sitename)s". Details follow:
00137 
00138     User name: %(username)s
00139     Email address: %(useremail)s""") % {
00140          'username': username,
00141          'useremail': email,
00142          'sitename': sitename or "Wiki",
00143          }
00144 
00145     return {'subject': subject, 'text': text}
00146 
00147 
00148 def attachment_added(request, _, page_name, attach_name, attach_size):
00149     """Formats a message used to notify about new attachments
00150 
00151     @param _: a gettext function
00152     @return: a dict with notification data
00153 
00154     """
00155     data = {}
00156 
00157     data['subject'] = _("New attachment added to page %(pagename)s on %(sitename)s") % {
00158                 'pagename': page_name,
00159                 'sitename': request.cfg.sitename or request.url_root,
00160                 }
00161 
00162     data['text'] = _("Dear Wiki user,\n\n"
00163     'You have subscribed to a wiki page "%(page_name)s" for change notification. '
00164     "An attachment has been added to that page by %(editor)s. "
00165     "Following detailed information is available:\n\n"
00166     "Attachment name: %(attach_name)s\n"
00167     "Attachment size: %(attach_size)s\n") % {
00168         'editor': user.getUserIdentification(request),
00169         'page_name': page_name,
00170         'attach_name': attach_name,
00171         'attach_size': attach_size,
00172     }
00173 
00174     data['editor'] = user.getUserIdentification(request)
00175     data['page_name'] = page_name
00176     data['attach_size'] = attach_size
00177     data['attach_name'] = attach_name
00178 
00179     return data
00180 
00181 
00182 def attachment_removed(request, _, page_name, attach_name, attach_size):
00183     """Formats a message used to notify about removed attachments
00184 
00185     @param _: a gettext function
00186     @return: a dict with notification data
00187 
00188     """
00189     data = {}
00190 
00191     data['subject'] = _("Removed attachment from page %(pagename)s on %(sitename)s") % {
00192                 'pagename': page_name,
00193                 'sitename': request.cfg.sitename or request.url_root,
00194                 }
00195 
00196     data['text'] = _("Dear Wiki user,\n\n"
00197     'You have subscribed to a wiki page "%(page_name)s" for change notification. '
00198     "An attachment has been removed from that page by %(editor)s. "
00199     "Following detailed information is available:\n\n"
00200     "Attachment name: %(attach_name)s\n"
00201     "Attachment size: %(attach_size)s\n") % {
00202         'editor': user.getUserIdentification(request),
00203         'page_name': page_name,
00204         'attach_name': attach_name,
00205         'attach_size': attach_size,
00206     }
00207 
00208     data['editor'] = user.getUserIdentification(request)
00209     data['page_name'] = page_name
00210     data['attach_size'] = attach_size
00211     data['attach_name'] = attach_name
00212 
00213     return data
00214 
00215 
00216 # XXX: clean up this method to take a notification type instead of bool for_jabber
00217 def filter_subscriber_list(event, subscribers, for_jabber):
00218     """Filter a list of page subscribers to honor event subscriptions
00219 
00220     @param subscribers: list of subscribers (dict of lists, language is the key)
00221     @param for_jabber: require jid
00222     @type subscribers: dict
00223 
00224     """
00225     event_name = event.name
00226 
00227     # Filter the list by removing users who don't want to receive
00228     # notifications about this type of event
00229     for lang in subscribers.keys():
00230         userlist = []
00231 
00232         if for_jabber:
00233             for usr in subscribers[lang]:
00234                 if usr.jid and event_name in usr.jabber_subscribed_events:
00235                     userlist.append(usr)
00236         else:
00237             for usr in subscribers[lang]:
00238                 if usr.email and event_name in usr.email_subscribed_events:
00239                     userlist.append(usr)
00240 
00241         subscribers[lang] = userlist
00242