annotate MoinMoin/events/emailnotify.py @ 5126:02234c0ac784

include comment in email notifies
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 05 Sep 2009 22:12:27 +0200
parents 68628fe3b091
children 12d27670e274
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
2595
5e7b45489430 moved python2.3/2.4 compatibility code to MoinMoin.support.python_compatibility (thanks to Heinrich Wendel)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2505
diff changeset
15 from MoinMoin.support.python_compatibility import set
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
16 from MoinMoin.user import User, getUserList
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
17 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
18
2469
663867be4afc Add a separate TrivialPageChangedEvent users can subscribe to.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2426
diff changeset
19 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
20 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
21
2086
c8bc6df45964 Formatting fixes according to PEP 8.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
22
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
23 def prep_page_changed_mail(request, page, comment, email_lang, revisions, trivial):
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
24 """ 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
25
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
26 @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
27 @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
28 @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
29 @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
30 @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
31 @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
32 @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
33
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
34 """
2917
093ffb70b26f fix email notification bug
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2638
diff changeset
35 change = notification.page_change_message("page_changed", request, page, email_lang, comment=comment, revisions=revisions)
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
36 _ = lambda text: request.getText(text, lang=email_lang)
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
37
3484
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
38 if len(revisions) >= 2:
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
39 querystr = {'action': 'diff',
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
40 'rev2': str(revisions[0]),
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
41 'rev1': str(revisions[1])}
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
42 else:
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
43 querystr = {}
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
44
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
45 pagelink = "%(link)s\n\n" % {'link': notification.page_link(request, page, querystr)}
be7c5a205cc2 Regression fix: send page diff url in notification emails
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 3234
diff changeset
46
3123
5cb80bd604b4 refactored _() getText calls to match new api
Radomir Dopieralski <moindev@sheep.art.pl>
parents: 2942
diff changeset
47 subject = _('[%(sitename)s] %(trivial)sUpdate of "%(pagename)s" by %(username)s') % {
5cb80bd604b4 refactored _() getText calls to match new api
Radomir Dopieralski <moindev@sheep.art.pl>
parents: 2942
diff changeset
48 'trivial': (trivial and _("Trivial ")) or "",
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
49 '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
50 '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
51 '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
52 }
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
53
5126
02234c0ac784 include comment in email notifies
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4663
diff changeset
54 if change.has_key('comment'):
02234c0ac784 include comment in email notifies
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4663
diff changeset
55 comment = _("Comment:") + "\n" + change['comment'] + "\n\n"
02234c0ac784 include comment in email notifies
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4663
diff changeset
56 else:
02234c0ac784 include comment in email notifies
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4663
diff changeset
57 comment = ''
02234c0ac784 include comment in email notifies
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4663
diff changeset
58
02234c0ac784 include comment in email notifies
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4663
diff changeset
59 return {'subject': subject, 'text': change['text'] + pagelink + comment + change['diff']}
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
60
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
61
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
62 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
63 """ 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
64
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
65 @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
66 @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
67 @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
68
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
69 """
3592
ff60ed56002a s/body/text/g to get rid of a stupid bug
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 3518
diff changeset
70 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
71
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
72
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
73 def handle_page_change(event):
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
74 """ 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
75
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
76 @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
77 @rtype: string
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
78 @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
79
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
80 """
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
81 comment = event.comment
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
82 page = event.page
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
83 request = event.request
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
84 trivial = isinstance(event, ev.TrivialPageChangedEvent)
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
85 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
86 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
87
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
88 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
89 recipients = set()
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325 2286
diff changeset
90
2062
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
91 # 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
92 revisions = page.getRevList()
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
93
c60fcb00dfa6 Extend EmailNotification plugin with code from PageEditor. Works.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2059
diff changeset
94 # 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
95 for lang in subscribers:
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2384
diff changeset
96 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
97 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
98 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
99 names = [u.name for u in users]
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
100 data = prep_page_changed_mail(request, page, comment, lang, revisions, trivial)
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
101
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
102 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
103 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
104
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2168
diff changeset
105 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
106 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
107
2086
c8bc6df45964 Formatting fixes according to PEP 8.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2076
diff changeset
108
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
109 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
110 """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
111
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
112 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
113 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
114 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
115 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
116 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
117 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
118
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
119 user_ids = getUserList(request)
2505
0a90c1bad60f Again, some fixes suggested by pylint.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2469
diff changeset
120 for usr_id in user_ids:
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
121 usr = User(request, id=usr_id)
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
122 # Currently send this only to super users
2426
5d1c31b0c6b5 make jabber/email notification types configurable orthogonally
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2384
diff changeset
123 if usr.isSuperUser() 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
124 _ = 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
125 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
126 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
127
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
128
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
129 def handle_file_attached(event):
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
130 """Sends an email to super users that have subscribed to this event type"""
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
131
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
132 names = set()
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
133 from_address = event.request.cfg.mail_from
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
134 request = event.request
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
135 page = Page(request, event.pagename)
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
136
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
137 subscribers = page.getSubscribers(request, return_users=1)
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
138 notification.filter_subscriber_list(event, subscribers, False)
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
139 recipients = []
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
140
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
141 for lang in subscribers:
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
142 recipients.extend(subscribers[lang])
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
143
4663
68628fe3b091 fix wrong links in attachment notifications
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3638
diff changeset
144 attachlink = request.getQualifiedURL(getAttachUrl(event.pagename, event.filename, request))
3234
a739558ca3dc Page.url() default changed to relative=False
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3211
diff changeset
145 pagelink = request.getQualifiedURL(page.url(request, {}))
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
146
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
147 for lang in subscribers:
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
148 emails = []
3143
16ae95df840a i18n.getText: fixed some callers that had still 'formatted=' kw args
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3123
diff changeset
149 _ = lambda text: request.getText(text, lang=lang)
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
150
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
151 links = _("Attachment link: %(attach)s\n" \
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
152 "Page link: %(page)s\n") % {'attach': attachlink, 'page': pagelink}
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
153
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
154 data = notification.attachment_added(request, _, event.pagename, event.filename, event.size)
3592
ff60ed56002a s/body/text/g to get rid of a stupid bug
Karol 'grzywacz' Nowak <grywacz@gmail.com>
parents: 3518
diff changeset
155 data['text'] = data['text'] + links
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
156
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
157 emails = [usr.email for usr in subscribers[lang]]
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
158
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
159 if send_notification(request, from_address, emails, data):
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
160 names.update(recipients)
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
161
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
162 return notification.Success(names)
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
163
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
164
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
165 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
166 """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
167
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
168 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
169 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
170
3518
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
171 if isinstance(event, (ev.PageChangedEvent, ev.TrivialPageChangedEvent)):
ef612c82bdfd remove want_trivial completely, fix trivial notifications
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3484
diff changeset
172 return handle_page_change(event)
2469
663867be4afc Add a separate TrivialPageChangedEvent users can subscribe to.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2426
diff changeset
173 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
174 return handle_user_created(event)
2469
663867be4afc Add a separate TrivialPageChangedEvent users can subscribe to.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2426
diff changeset
175 elif isinstance(event, ev.FileAttachedEvent):
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
176 return handle_file_attached(event)