annotate MoinMoin/events/jabbernotify.py @ 2385:53fbdf5899ff

Use the event handling framework to (re)generate xapian indexes.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Sat, 14 Jul 2007 18:42:12 +0200
parents 1d60f9aa07d1
children 5d1c31b0c6b5
rev   line source
2066
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
2 """
2074
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2069
diff changeset
3 MoinMoin - jabber notification plugin for event system
2066
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
4
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
5 This code sends notifications using a separate daemon.
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
6
2110
1ede132a9d38 Suggested fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2109
diff changeset
7 @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
1ede132a9d38 Suggested fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2109
diff changeset
8 @license: GNU GPL, see COPYING for details.
2066
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
9 """
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
10
2154
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
11 import xmlrpclib
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
12
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
13 try:
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
14 set
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
15 except:
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
16 from sets import Set as set
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2331 2286
diff changeset
17
2315
69ed234c3241 Bail out if the secret string is empty.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2307
diff changeset
18 from MoinMoin import error
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
19 from MoinMoin.Page import Page
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
20 from MoinMoin.user import User, getUserList
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
21 import MoinMoin.events.notification as notification
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
22 import MoinMoin.events as ev
2086
c8bc6df45964 Formatting fixes according to PEP 8.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2074
diff changeset
23
c8bc6df45964 Formatting fixes according to PEP 8.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2074
diff changeset
24
2066
b73b2b885b02 Add a stub for jabber notification plugin.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
25 def handle(event):
2344
d680aee762a7 Fix notification about user creation - command needs a list of jids.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
26 """An event handler"""
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: 2344
diff changeset
27
2091
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
28 cfg = event.request.cfg
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
29
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
30 # Check for desired event type and if notification bot is configured
2093
917bb59fdbb8 Catch Exception as well, because xmlrpclib lets other exceptions propagate.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2091
diff changeset
31 if not cfg.jabber_enabled:
2091
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
32 return
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
33
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
34 if isinstance(event, ev.PageChangedEvent):
2097
9cebfffe27af Don't forget to pass the value returned by event handlers back to the caller.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2095
diff changeset
35 return handle_page_changed(event)
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
36 elif isinstance(event, ev.JabberIDSetEvent) or isinstance(event, ev.JabberIDUnsetEvent):
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
37 return handle_jid_changed(event)
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
38 elif isinstance(event, ev.FileAttachedEvent):
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
39 return handle_file_attached(event)
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
40 elif isinstance(event, ev.PageDeletedEvent):
2109
b39379ab47d3 Send notifications when page is deleted + some small fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2106
diff changeset
41 return handle_page_deleted(event)
2329
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
42 elif isinstance(event, ev.PageRenamedEvent):
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
43 return handle_page_renamed(event)
2145
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
44 elif isinstance(event, ev.UserCreatedEvent):
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
45 return handle_user_created(event)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
46
2091
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
47
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
48 def handle_jid_changed(event):
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
49 """ Handles events sent when user's JID changes """
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
50
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
51 request = event.request
2160
7c8ee12f8c1d Fix an error with missing bot_host attribute. Use more descriptive names.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2154
diff changeset
52 server = request.cfg.notification_server
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
53 _ = request.getText
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
54
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
55 try:
2145
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
56 if isinstance(event, ev.JabberIDSetEvent):
2105
cc5067ea1235 Fix brain-damaged behaviour I introduced yestarday.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2098
diff changeset
57 server.addJIDToRoster(request.cfg.secret, event.jid)
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
58 else:
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
59 server.removeJIDFromRoster(request.cfg.secret, event.jid)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
60
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
61 except xmlrpclib.Error, err:
2323
a43db0817020 Use stdlib's logging instead of prints.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
62 ev.logger.error(_("XML RPC error: %s"), str(err))
2098
da36e7cc80a3 Handle JID set/unset events on the wiki side.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2097
diff changeset
63 except Exception, err:
2323
a43db0817020 Use stdlib's logging instead of prints.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
64 ev.logger.error(_("Low-level communication error: $s"), str(err))
2095
258d6e648eca Add a JabberIDUnsetEvent; make some private symbols internal in MoinMoin.events
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2093
diff changeset
65
2091
7af58454aafa Send an event when user changes her jabber id, force jid to be unique.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2087
diff changeset
66
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
67 def handle_file_attached(event):
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
68 """Handles event sent when a file is attached to a page"""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
69
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
70 names = set()
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
71 request = event.request
2154
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
72 page = Page(request, event.pagename)
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
73 event_name = event.__class__.__name__
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
74
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
75 subscribers = page.getSubscribers(request, return_users=1)
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
76 notification.filter_subscriber_list(event, subscribers, True)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
77
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
78 for lang in subscribers.keys():
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
79 jids = []
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
80 data = notification.attachment_added(request, event.pagename, event.name, event.size)
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
81
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
82 for usr in subscribers[lang]:
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
83 if usr.notify_by_jabber and usr.jid and event_name in usr.subscribed_events:
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
84 jids.append(usr.jid)
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
85 else:
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
86 continue
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
87
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
88 if send_notification(request, jids, data['body'], data['subject']):
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
89 names.update(usr.name)
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
90
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
91 return notification.Success(names)
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
92
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
93
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
94 def handle_page_changed(event):
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
95 """ Handles events related to page changes """
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
96 request = event.request
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
97 page = event.page
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
98
2337
1d85514968a2 Fix remaining excessive white space characters.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2336
diff changeset
99 subscribers = page.getSubscribers(request, return_users=1, trivial=event.trivial)
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
100 notification.filter_subscriber_list(event, subscribers, True)
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
101 return page_change("page_changed", request, page, subscribers, \
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
102 revisions=page.getRevList(), comment=event.comment)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
103
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
104
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
105 def handle_page_deleted(event):
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
106 """Handles event sent when a page is deleted"""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
107
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
108 request = event.request
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
109 page = event.page
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
110
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
111 subscribers = page.getSubscribers(request, return_users=1)
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
112 notification.filter_subscriber_list(event, subscribers, True)
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
113 return page_change("page_deleted", request, page, subscribers)
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
114
2329
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
115 def handle_page_renamed(event):
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
116 """Handles event sent when a page is renamed"""
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2331 2286
diff changeset
117
2329
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
118 request = event.request
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
119 page = event.page
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
120 old_name = event.old_page.page_name
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2331 2286
diff changeset
121
2329
9a71ea0c941b Add support for PageRenamed event. Not tested yet.
Karol Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2325
diff changeset
122 subscribers = page.getSubscribers(request, return_users=1)
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
123 notification.filter_subscriber_list(event, subscribers, True)
2385
53fbdf5899ff Use the event handling framework to (re)generate xapian indexes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2356
diff changeset
124 return page_change("page_renamed", request, page, subscribers, oldname=old_name)
2145
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
125
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
126
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
127 def handle_user_created(event):
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
128 """Handles an event sent when a new user is being created"""
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
129
2344
d680aee762a7 Fix notification about user creation - command needs a list of jids.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
130 jids = []
2145
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
131 user_ids = getUserList(event.request)
2154
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
132 event_name = event.__class__.__name__
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
133
2344
d680aee762a7 Fix notification about user creation - command needs a list of jids.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
134 email = event.user.email or u"NOT SET"
2354
ebc8fc2a482a Add user_created_message() to share some code. Some fixes to support subjects.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2345
diff changeset
135 sitename = event.request.cfg.sitename
ebc8fc2a482a Add user_created_message() to share some code. Some fixes to support subjects.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2345
diff changeset
136 username = event.user.name
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
137
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 data = notification.user_created_message(event.request, sitename, username, email)
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
139
2145
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
140 for id in user_ids:
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
141 usr = User(event.request, id=id)
2325
741ab277616a Honor the user setting of notification methods.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2323
diff changeset
142 if not usr.notify_by_jabber:
741ab277616a Honor the user setting of notification methods.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2323
diff changeset
143 continue
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
144
2145
f153b3155240 Send notifications about creation of users to Superusers with JID set.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2114
diff changeset
145 # Currently send this only to super users
2154
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
146 if usr.isSuperUser() and usr.jid and event_name in usr.subscribed_events:
2344
d680aee762a7 Fix notification about user creation - command needs a list of jids.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2337
diff changeset
147 jids.append(usr.jid)
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: 2344
diff changeset
148
2354
ebc8fc2a482a Add user_created_message() to share some code. Some fixes to support subjects.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2345
diff changeset
149 send_notification(event.request, jids, msg % (event.user.name, email), data['subject'])
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
150
2111
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
151
a3377b242e21 First try to reduce code duplication. More tomorrow after some sleep...
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2110
diff changeset
152 def page_change(type, request, page, subscribers, **kwargs):
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
153 """Sends notification about page being changed in some way"""
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
154 _ = request.getText
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
155
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
156 # send notifications to all subscribers
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
157 if subscribers:
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
158 recipients = set()
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2331 2286
diff changeset
159
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
160 for lang in subscribers:
2154
f2ae2f08958d Honor event subscriptions when sending jabber notifications. Fix missing import.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2147
diff changeset
161 jids = [u.jid for u in subscribers[lang] if u.jid]
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
162 names = [u.name for u in subscribers[lang] if u.jid]
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
163 msg = notification.page_change_message(type, request, page, lang, **kwargs)
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
164 result = send_notification(request, jids, msg)
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
165
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
166 if result:
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
167 recipients.update(names)
2106
1577663f6354 Notify when a new attachment is added to a page. s/JabberBot/jabberbot/.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2105
diff changeset
168
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
169 if recipients:
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
170 return notification.Success(recipients)
2109
b39379ab47d3 Send notifications when page is deleted + some small fixes.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2106
diff changeset
171
2354
ebc8fc2a482a Add user_created_message() to share some code. Some fixes to support subjects.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2345
diff changeset
172 def send_notification(request, jids, message, subject=""):
2074
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2069
diff changeset
173 """ Send notifications for a single language.
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2069
diff changeset
174
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2069
diff changeset
175 @param comment: editor's comment given when saving the page
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
176 @param jids: an iterable of Jabber IDs to send the message to
2074
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2069
diff changeset
177 """
088edb461832 Use the same logic to generate email and jabber notifications. Both are easily
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2069
diff changeset
178 _ = request.getText
2160
7c8ee12f8c1d Fix an error with missing bot_host attribute. Use more descriptive names.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2154
diff changeset
179 server = request.cfg.notification_server
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
180
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
181 try:
2354
ebc8fc2a482a Add user_created_message() to share some code. Some fixes to support subjects.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2345
diff changeset
182 server.send_notification(request.cfg.secret, jids, message, subject)
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
183 return True
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
184 except xmlrpclib.Error, err:
2323
a43db0817020 Use stdlib's logging instead of prints.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2318
diff changeset
185 ev.logger.error(_("XML RPC error: %s"), str(err))
2318
062b76cf8d48 Introduce an base EventResult class for event handler return values.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2315
diff changeset
186 except Exception, err:
2336
10be78f1fa97 Merge main.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2331 2286
diff changeset
187 ev.logger.error(_("Low-level communication error: %s"), str(err), )
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2160
diff changeset
188