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