File: notification.py

package info (click to toggle)
moin 1.9.9-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 76,024 kB
  • sloc: python: 143,896; java: 10,704; php: 2,385; perl: 1,574; xml: 371; makefile: 214; sh: 81; sed: 5
file content (190 lines) | stat: -rw-r--r-- 6,370 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - common functions for notification framework

    Code for building messages informing about events (changes)
    happening in the wiki.

    @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
    @license: GNU GPL, see COPYING for details.
"""

from MoinMoin import user, wikiutil
from MoinMoin.events import EventResult


class Result(EventResult):
    """ A base class for results of notification handlers"""
    pass


class Failure(Result):
    """ Used to report a failure in sending notifications """
    def __init__(self, reason, recipients = None):
        """
        @param recipients: a set of recipients
        @type recipients: set
        """
        self.reason = reason
        self.recipient = None

    def __str__(self):
        return self.reason or u""


class Success(Result):
    """ Used to indicate successfull notifications """

    def __init__(self, recipients):
        """
        @param recipients: a set of recipients
        @type recipients: set
        """
        self.recipients = recipients


class UnknownChangeType(Exception):
    """ Used to signal an invalid change event """
    pass


def page_change_message(msgtype, request, page, lang, **kwargs):
    """Prepare a notification text for a page change of given type

    @param msgtype: a type of message to send (page_changed, page_renamed, ...)
    @type msgtype: str or unicode
    @param **kwargs: a dictionary of additional parameters, which depend on msgtype

    @return: dictionary containing data about the changed page
    @rtype: dict

    """
    _ = lambda text: request.getText(text, lang=lang)
    cfg = request.cfg
    data = {}
    data['revision'] = str(page.getRevList()[0])
    data['page_name'] = pagename = page.page_name
    sitename = page.cfg.sitename or request.url_root
    data['editor'] = editor = username = page.uid_override or user.getUserIdentification(request)

    trivial = (kwargs.get('trivial') and _("Trivial ")) or ""

    if msgtype == "page_changed":
        data['subject'] = _(cfg.mail_notify_page_changed_subject) % locals()
        data['text'] = _(cfg.mail_notify_page_changed_intro) % locals()

        revisions = kwargs['revisions']
        # append a diff (or append full page text if there is no diff)
        if len(revisions) < 2:
            data['diff'] = _("New page:\n") + page.get_raw_body()
        else:
            lines = wikiutil.pagediff(request, page.page_name, revisions[1],
                                      page.page_name, revisions[0])
            if lines:
                data['diff'] = '\n'.join(lines)
            else:
                data['diff'] = _("No differences found!\n")

    elif msgtype == "page_deleted":
        data['subject'] = _(cfg.mail_notify_page_deleted_subject) % locals()
        data['text'] = _(cfg.mail_notify_page_deleted_intro) % locals()

        revisions = kwargs['revisions']
        latest_existing = revisions[0]
        lines = wikiutil.pagediff(request, page.page_name, latest_existing,
                                  page.page_name, latest_existing + 1)
        if lines:
            data['diff'] = '\n'.join(lines)
        else:
            data['diff'] = _("No differences found!\n")

    elif msgtype == "page_renamed":
        data['old_name'] = oldname = kwargs['old_name']
        data['subject'] = _(cfg.mail_notify_page_renamed_subject) % locals()
        data['text'] = _(cfg.mail_notify_page_renamed_intro) % locals()
        data['diff'] = ''

    else:
        raise UnknownChangeType()

    if 'comment' in kwargs and kwargs['comment']:
        data['comment'] = kwargs['comment']

    return data


def user_created_message(request, _, sitename, username, email):
    """Formats a message used to notify about accounts being created

    @return: a dict containing message body and subject
    """
    cfg = request.cfg
    sitename = sitename or "Wiki"
    useremail = email

    data = {}
    data['subject'] = _(cfg.mail_notify_user_created_subject) % locals()
    data['text'] = _(cfg.mail_notify_user_created_intro) % locals()
    return data


def attachment_added(request, _, page_name, attach_name, attach_size):
    return _attachment_changed(request, _, page_name, attach_name, attach_size, change="added")


def attachment_removed(request, _, page_name, attach_name, attach_size):
    return _attachment_changed(request, _, page_name, attach_name, attach_size, change="removed")


def _attachment_changed(request, _, page_name, attach_name, attach_size, change):
    """Formats a message used to notify about new / removed attachments

    @param _: a gettext function
    @return: a dict with notification data
    """
    cfg = request.cfg
    pagename = page_name
    sitename = cfg.sitename or request.url_root
    editor = user.getUserIdentification(request)
    data = {}
    data['editor'] = editor
    data['page_name'] = page_name
    data['attach_size'] = attach_size
    data['attach_name'] = attach_name
    if change == "added":
        data['subject'] = _(cfg.mail_notify_att_added_subject) % locals()
        data['text'] = _(cfg.mail_notify_att_added_intro) % locals()
    elif change == "removed":
        data['subject'] = _(cfg.mail_notify_att_removed_subject) % locals()
        data['text'] = _(cfg.mail_notify_att_removed_intro) % locals()
    else:
        raise UnknownChangeType()
    return data


# XXX: clean up this method to take a notification type instead of bool for_jabber
def filter_subscriber_list(event, subscribers, for_jabber):
    """Filter a list of page subscribers to honor event subscriptions

    @param subscribers: list of subscribers (dict of lists, language is the key)
    @param for_jabber: require jid
    @type subscribers: dict

    """
    event_name = event.name

    # Filter the list by removing users who don't want to receive
    # notifications about this type of event
    for lang in subscribers.keys():
        userlist = []

        if for_jabber:
            for usr in subscribers[lang]:
                if usr.jid and event_name in usr.jabber_subscribed_events:
                    userlist.append(usr)
        else:
            for usr in subscribers[lang]:
                if usr.email and event_name in usr.email_subscribed_events:
                    userlist.append(usr)

        subscribers[lang] = userlist