annotate MoinMoin/events/EmailNotification.py @ 2076:0128bbaf0172

Add newlines at the end of files, fix some overzealous deletion.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Fri, 01 Jun 2007 21:18:01 +0200
parents 088edb461832
children c8bc6df45964
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2059
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
2 """
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
3 MoinMoin - email notification plugin from event system
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
4
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
5 This code sends email notifications about page changes.
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
6 TODO: refactor it to handle separate events for page changes, creations, etc
2059
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
7
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
8 @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
9 @license: GNU GPL, see COPYING for details.
2059
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
10 """
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
11
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
12 from MoinMoin import user
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
13 from MoinMoin.Page import Page
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
14 from MoinMoin.mail import sendmail
2059
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
15 from MoinMoin.events import *
2074
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2062
diff changeset
16 from MoinMoin.events.notification_common import page_changed_notification
2059
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
17
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
18 def sendNotification(request, page, comment, emails, email_lang, revisions, trivial):
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
19 """ Send notification email for a single language.
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
20
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
21 @param comment: editor's comment given when saving the page
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
22 @param emails: list of email addresses
2074
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2062
diff changeset
23 @param email_lang: language of email
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
24 @param revisions: revisions of this page (newest first!)
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
25 @param trivial: the change is marked as trivial
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
26 @rtype: int
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
27 @return: sendmail result
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
28 """
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
29 _ = request.getText
2074
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2062
diff changeset
30 mailBody = page_changed_notification(request, page, comment, email_lang, revisions, trivial)
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
31
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
32 return sendmail.sendmail(request, emails,
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
33 _('[%(sitename)s] %(trivial)sUpdate of "%(pagename)s" by %(username)s', formatted=False) % {
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
34 'trivial': (trivial and _("Trivial ", formatted=False)) or "",
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
35 'sitename': page.cfg.sitename or "Wiki",
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
36 'pagename': page.page_name,
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
37 'username': page.uid_override or user.getUserIdentification(request),
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
38 },
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
39 mailBody, mail_from=page.cfg.mail_from)
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
40
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
41
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
42 def notifySubscribers(request, page, comment, trivial):
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
43 """ Send email to all subscribers of given page.
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
44
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
45 @param comment: editor's comment given when saving the page
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
46 @param trivial: editor's suggestion that the change is trivial (Subscribers may ignore this)
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
47 @rtype: string
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
48 @return: message, indicating success or errors.
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
49 """
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
50 _ = request.getText
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
51 subscribers = page.getSubscribers(request, return_users=1, trivial=trivial)
2074
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2062
diff changeset
52
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
53 if subscribers:
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
54 # get a list of old revisions, and append a diff
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
55 revisions = page.getRevList()
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
56
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
57 # send email to all subscribers
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
58 results = [_('Status of sending notification mails:')]
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
59 for lang in subscribers:
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
60 emails = [u.email for u in subscribers[lang]]
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
61 names = [u.name for u in subscribers[lang]]
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
62 mailok, status = sendNotification(request, page, comment, emails, lang, revisions, trivial)
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
63 recipients = ", ".join(names)
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
64 results.append(_('[%(lang)s] %(recipients)s: %(status)s') % {
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
65 'lang': lang, 'recipients': recipients, 'status': status})
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
66
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
67 # Return mail sent results. Ignore trivial - we don't have
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
68 # to lie. If mail was sent, just tell about it.
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
69 return '<p>\n%s\n</p> ' % '<br>'.join(results)
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
70
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
71 # No mail sent, no message.
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
72 return ''
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
73
2059
855d73886dae Add a pseudo-plugin for testing. Email notification from PageEditor will be
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
74 def handle(event):
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
75 if not isinstance(event, PageChangedEvent):
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
76 return
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
77
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
78 if not event.request.cfg.mail_enabled:
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
79 return
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
80
2076
0128bbaf0172 Add newlines at the end of files, fix some overzealous deletion.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2074
diff changeset
81 return notifySubscribers(event.request, event.page, event.comment, event.trivial)