annotate MoinMoin/events/emailnotify.py @ 6133:a6283e189869 tip

fixup: remove nonexisting passlib.utils._blowfish this was removed by the passlib 1.7.1 upgrade.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 01 Jun 2017 18:10:19 +0200
parents 500f68d3e2fd
children
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
6027
8618232296b5 optimized notification of superusers on user account creation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5280
diff changeset
15 from MoinMoin.user import User, superusers
2638
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
16 from MoinMoin.action.AttachFile import getAttachUrl
6e84127a1860 Finally all known bugs fixed, OOB is used to send links with jabber. More...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2637
diff changeset
17
2469
663867be4afc Add a separate TrivialPageChangedEvent users can subscribe to.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2426
diff changeset
18 import MoinMoin.events as ev
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2168
diff changeset
19 import MoinMoin.events.notification as 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
20
2086
c8bc6df45964 Formatting fixes according to PEP 8.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
21
6036
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
22 def prep_page_changed_mail(request, page, comment, email_lang, revisions,
6037
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
23 trivial=False, change_type="page_changed",
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
24 old_page=None):
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
25 """ Prepare information required for email notification about page change
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
26
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
27 @param page: the modified page instance
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
28 @param comment: editor's comment given when saving the page
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
29 @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
30 @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
31 @param trivial: the change is marked as trivial
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
32 @return: dict with email title and body
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
33 @rtype: dict
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
34
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
35 """
6037
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
36 old_name = old_page and old_page.page_name or None # for rename
6036
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
37 change = notification.page_change_message(change_type, request, page, email_lang,
6037
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
38 comment=comment, revisions=revisions, trivial=trivial,
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
39 old_name=old_name)
3636
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
40 _ = lambda text: request.getText(text, lang=email_lang)
6035
be1e6321ba13 email notifications: move default texts to configuration so they can be overwritten, clean up notification module
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6034
diff changeset
41 cfg = request.cfg
6034
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
42 intro = change['text']
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
43 diff = change['diff']
6035
be1e6321ba13 email notifications: move default texts to configuration so they can be overwritten, clean up notification module
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6034
diff changeset
44 subject = change['subject']
6034
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
45
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
46 if change.has_key('comment'):
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
47 comment = _("Comment:") + "\n" + change['comment'] + "\n\n"
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
48 else:
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
49 comment = ''
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
50
6037
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
51 diff_revs = None
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
52 if change_type == "page_changed" and len(revisions) >= 2:
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
53 diff_revs = revisions[1], revisions[0]
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
54 elif change_type == "page_deleted" and len(revisions) >= 1:
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
55 diff_revs = revisions[0], revisions[0] + 1
6036
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
56
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
57 if diff_revs:
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
58 querystr = {'action': 'diff', 'rev1': str(diff_revs[0]), 'rev2': str(diff_revs[1])}
3484
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
59 else:
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
60 querystr = {}
6036
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
61
6034
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
62 # links to diff or to page (if only 1 rev):
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
63 difflink = request.getQualifiedURL(page.url(request, querystr))
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
64 # always links to page:
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
65 pagelink = request.getQualifiedURL(page.url(request))
3484
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
66
6034
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
67 sitename = page.cfg.sitename or "Wiki"
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
68 pagename = page.page_name
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
69 username = page.uid_override or user.getUserIdentification(request)
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
70
6040
784455464e93 notification template strings: better naming consistency for configuration values
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6038
diff changeset
71 text = _(cfg.mail_notify_page_text) % locals()
6034
f77842ede10f email notification: internally use python template strings to assemble the subject and texts from given components
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6027
diff changeset
72 return {'subject': subject, 'text': text}
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
73
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
74
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
75 def send_notification(request, from_address, emails, data):
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
76 """ Send notification email
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
77
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
78 @param emails: list of email addresses
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
79 @return: sendmail result
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
80 @rtype int
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
81
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
82 """
3592
ff60ed56002a s/body/text/g to get rid of a stupid bug
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 3518
diff changeset
83 return sendmail.sendmail(request, emails, data['subject'], data['text'], mail_from=from_address)
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
84
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
85
6038
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
86 def handle_page_changed(event):
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
87 """ 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
88
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
89 @param event: event to notify about
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
90 @rtype: string
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
91 @return: message, indicating success or errors.
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
92
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
93 """
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
94 comment = event.comment
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
95 page = event.page
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
96 request = event.request
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
97 trivial = isinstance(event, ev.TrivialPageChangedEvent)
6037
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
98 old_page = None
6036
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
99 if isinstance(event, (ev.PageChangedEvent, ev.TrivialPageChangedEvent)):
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
100 change_type = "page_changed"
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
101 elif isinstance(event, ev.PageDeletedEvent):
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
102 change_type = "page_deleted"
6037
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
103 elif isinstance(event, ev.PageRenamedEvent):
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
104 change_type = "page_renamed"
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
105 old_page = event.old_page
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
106 subscribers = page.getSubscribers(request, return_users=1)
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
107 mail_from = page.cfg.mail_from
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2168
diff changeset
108
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
109 if subscribers:
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2168
diff changeset
110 recipients = set()
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325 2286
diff changeset
111
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
112 # 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
113 revisions = page.getRevList()
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
114
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
115 # 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
116 for lang in subscribers:
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2384
diff changeset
117 users = [u for u in subscribers[lang]
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
118 if event.name in u.email_subscribed_events]
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2384
diff changeset
119 emails = [u.email for u in users]
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2384
diff changeset
120 names = [u.name for u in users]
6036
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
121 data = prep_page_changed_mail(request, page, comment, lang, revisions,
6037
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
122 trivial=trivial, change_type=change_type,
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
123 old_page=old_page)
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
124
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
125 if send_notification(request, mail_from, emails, data):
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2168
diff changeset
126 recipients.update(names)
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
127
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2168
diff changeset
128 if recipients:
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2168
diff changeset
129 return notification.Success(recipients)
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
130
2086
c8bc6df45964 Formatting fixes according to PEP 8.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
131
6038
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
132 def handle_file_changed(event):
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
133 """Sends an email to users that have subscribed to this event type"""
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
134
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
135 names = set()
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
136 from_address = event.request.cfg.mail_from
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
137 request = event.request
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
138 page = Page(request, event.pagename)
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
139
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
140 if isinstance(event, ev.FileAttachedEvent):
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
141 notification_fn = notification.attachment_added
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
142 elif isinstance(event, ev.FileRemovedEvent):
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
143 notification_fn = notification.attachment_removed
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
144
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
145 subscribers = page.getSubscribers(request, return_users=1)
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
146 notification.filter_subscriber_list(event, subscribers, False)
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
147 recipients = []
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
148
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
149 for lang in subscribers:
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
150 recipients.extend(subscribers[lang])
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
151
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
152 attachlink = request.getQualifiedURL(getAttachUrl(event.pagename, event.filename, request))
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
153 pagelink = request.getQualifiedURL(page.url(request, {}))
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
154
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
155 for lang in subscribers:
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
156 _ = lambda text: request.getText(text, lang=lang)
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
157
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
158 links = _("Attachment link: %(attach)s\n" \
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
159 "Page link: %(page)s\n") % {'attach': attachlink, 'page': pagelink}
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
160
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
161 data = notification_fn(request, _, event.pagename, event.filename, event.size)
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
162 data['text'] = data['text'] + links
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
163
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
164 emails = [usr.email for usr in subscribers[lang]]
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
165
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
166 if send_notification(request, from_address, emails, data):
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
167 names.update(recipients)
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
168
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
169 return notification.Success(names)
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
170
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
171
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
172 def handle_user_created(event):
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
173 """Sends an email to super users that have subscribed to this event type"""
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
174
3636
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
175 request = event.request
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
176 sitename = request.cfg.sitename
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
177 from_address = request.cfg.mail_from
2633
f42fb1ae0a38 Fixes to the Event.name commit.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2505
diff changeset
178 event_name = event.name
2354
ebc8fc2a482a Add user_created_message() to share some code. Some fixes to support subjects.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2350
diff changeset
179 email = event.user.email or u"NOT SET"
ebc8fc2a482a Add user_created_message() to share some code. Some fixes to support subjects.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2350
diff changeset
180 username = event.user.name
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
181
6027
8618232296b5 optimized notification of superusers on user account creation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5280
diff changeset
182 for usr in superusers(request):
8618232296b5 optimized notification of superusers on user account creation
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5280
diff changeset
183 if usr.email and event_name in usr.email_subscribed_events:
3638
715c0a9e4840 jabber/email new user notification: use english language if user did not specify language
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3636
diff changeset
184 _ = lambda text: request.getText(text, lang=usr.language or 'en')
3636
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
185 data = notification.user_created_message(request, _, sitename, username, email)
4a1813f1cb61 event notifications: send notifies in the language of the message recipient (not of the current wiki user)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3592
diff changeset
186 send_notification(request, from_address, [usr.email], data)
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
187
2356
1d60f9aa07d1 Email notification about new attachments. Some i18n issues left to be fixed.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2354
diff changeset
188
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
189 def handle(event):
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
190 """An event handler"""
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
191
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
192 if not event.request.cfg.mail_enabled:
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
193 return
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
194
6036
46bca3b2d3ba email notifications: handle PageDeletedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6035
diff changeset
195 if isinstance(event, (ev.PageChangedEvent, ev.TrivialPageChangedEvent,
6037
e99666460fb1 email notifications: handle PageRenamedEvent (bugfix, was not handled yet)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6036
diff changeset
196 ev.PageDeletedEvent, ev.PageRenamedEvent)):
6038
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
197 return handle_page_changed(event)
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
198 elif isinstance(event, (ev.FileAttachedEvent, ev.FileRemovedEvent)):
ce4a707bc0e7 email notifications: handle PageRenamedEvent (bugfix, was not handled yet), reduce code duplication
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6037
diff changeset
199 return handle_file_changed(event)
2469
663867be4afc Add a separate TrivialPageChangedEvent users can subscribe to.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2426
diff changeset
200 elif isinstance(event, ev.UserCreatedEvent):
2345
59d83c1c6ffc Refactor email notification so more code is shared. Initial user creation notification - not tested.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
201 return handle_user_created(event)