Code

Move templates/ to share/roundup/templates/
[roundup.git] / share / roundup / templates / classic / detectors / nosyreaction.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 #$Id: nosyreaction.py,v 1.4 2005-04-04 08:47:14 richard Exp $
20 import sets
22 from roundup import roundupdb, hyperdb
24 def nosyreaction(db, cl, nodeid, oldvalues):
25     ''' A standard detector is provided that watches for additions to the
26         "messages" property.
27         
28         When a new message is added, the detector sends it to all the users on
29         the "nosy" list for the issue that are not already on the "recipients"
30         list of the message.
31         
32         Those users are then appended to the "recipients" property on the
33         message, so multiple copies of a message are never sent to the same
34         user.
35         
36         The journal recorded by the hyperdatabase on the "recipients" property
37         then provides a log of when the message was sent to whom. 
38     '''
39     # send a copy of all new messages to the nosy list
40     for msgid in determineNewMessages(cl, nodeid, oldvalues):
41         try:
42             cl.nosymessage(nodeid, msgid, oldvalues)
43         except roundupdb.MessageSendError, message:
44             raise roundupdb.DetectorError, message
46 def determineNewMessages(cl, nodeid, oldvalues):
47     ''' Figure a list of the messages that are being added to the given
48         node in this transaction.
49     '''
50     messages = []
51     if oldvalues is None:
52         # the action was a create, so use all the messages in the create
53         messages = cl.get(nodeid, 'messages')
54     elif oldvalues.has_key('messages'):
55         # the action was a set (so adding new messages to an existing issue)
56         m = {}
57         for msgid in oldvalues['messages']:
58             m[msgid] = 1
59         messages = []
60         # figure which of the messages now on the issue weren't there before
61         for msgid in cl.get(nodeid, 'messages'):
62             if not m.has_key(msgid):
63                 messages.append(msgid)
64     return messages
66 def updatenosy(db, cl, nodeid, newvalues):
67     '''Update the nosy list for changes to the assignedto
68     '''
69     # nodeid will be None if this is a new node
70     current_nosy = sets.Set()
71     if nodeid is None:
72         ok = ('new', 'yes')
73     else:
74         ok = ('yes',)
75         # old node, get the current values from the node if they haven't
76         # changed
77         if not newvalues.has_key('nosy'):
78             nosy = cl.get(nodeid, 'nosy')
79             for value in nosy:
80                 current_nosy.add(value)
82     # if the nosy list changed in this transaction, init from the new value
83     if newvalues.has_key('nosy'):
84         nosy = newvalues.get('nosy', [])
85         for value in nosy:
86             if not db.hasnode('user', value):
87                 continue
88             current_nosy.add(value)
90     new_nosy = sets.Set(current_nosy)
92     # add assignedto(s) to the nosy list
93     if newvalues.has_key('assignedto') and newvalues['assignedto'] is not None:
94         propdef = cl.getprops()
95         if isinstance(propdef['assignedto'], hyperdb.Link):
96             assignedto_ids = [newvalues['assignedto']]
97         elif isinstance(propdef['assignedto'], hyperdb.Multilink):
98             assignedto_ids = newvalues['assignedto']
99         for assignedto_id in assignedto_ids:
100             new_nosy.add(assignedto_id)
102     # see if there's any new messages - if so, possibly add the author and
103     # recipient to the nosy
104     if newvalues.has_key('messages'):
105         if nodeid is None:
106             ok = ('new', 'yes')
107             messages = newvalues['messages']
108         else:
109             ok = ('yes',)
110             # figure which of the messages now on the issue weren't
111             oldmessages = cl.get(nodeid, 'messages')
112             messages = []
113             for msgid in newvalues['messages']:
114                 if msgid not in oldmessages:
115                     messages.append(msgid)
117         # configs for nosy modifications
118         add_author = getattr(db.config, 'ADD_AUTHOR_TO_NOSY', 'new')
119         add_recips = getattr(db.config, 'ADD_RECIPIENTS_TO_NOSY', 'new')
121         # now for each new message:
122         msg = db.msg
123         for msgid in messages:
124             if add_author in ok:
125                 authid = msg.get(msgid, 'author')
126                 new_nosy.add(authid)
128             # add on the recipients of the message
129             if add_recips in ok:
130                 for recipient in msg.get(msgid, 'recipients'):
131                     new_nosy.add(recipient)
133     if current_nosy != new_nosy:
134         # that's it, save off the new nosy list
135         newvalues['nosy'] = list(new_nosy)
137 def init(db):
138     db.issue.react('create', nosyreaction)
139     db.issue.react('set', nosyreaction)
140     db.issue.audit('create', updatenosy)
141     db.issue.audit('set', updatenosy)
143 # vim: set filetype=python ts=4 sw=4 et si