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.
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.
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.
18 #$Id: nosyreaction.py,v 1.4 2005-04-04 08:47:14 richard Exp $
20 # Python 2.3 ... 2.6 compatibility:
21 from roundup
.anypy
.sets_
import set
23 from roundup
import roundupdb
, hyperdb
25 def nosyreaction(db
, cl
, nodeid
, oldvalues
):
26 ''' A standard detector is provided that watches for additions to the
29 When a new message is added, the detector sends it to all the users on
30 the "nosy" list for the issue that are not already on the "recipients"
33 Those users are then appended to the "recipients" property on the
34 message, so multiple copies of a message are never sent to the same
37 The journal recorded by the hyperdatabase on the "recipients" property
38 then provides a log of when the message was sent to whom.
40 # force creation send message
41 if oldvalues
is None and cl
.classname
== 'issue':
42 authid
= cl
.db
.getuid()
43 authaddr
= cl
.db
.user
.get(authid
, 'address', '')
44 title
= cl
.get(nodeid
, 'title') or '%s message copy' % cl
.classname
45 msg
= "Votre demande d'assistance \"" + title \
46 + "\" nous a bien été transmise et sera traitée dès que " \
48 cl
.send_message(nodeid
, None, msg
, [authaddr
,])
50 # send a copy of all new messages to the nosy list
51 for msgid
in determineNewMessages(cl
, nodeid
, oldvalues
):
53 cl
.nosymessage(nodeid
, msgid
, oldvalues
)
54 except roundupdb
.MessageSendError
, message
:
55 raise roundupdb
.DetectorError
, message
57 def determineNewMessages(cl
, nodeid
, oldvalues
):
58 ''' Figure a list of the messages that are being added to the given
59 node in this transaction.
63 # the action was a create, so use all the messages in the create
64 messages
= cl
.get(nodeid
, 'messages')
65 elif oldvalues
.has_key('messages'):
66 # the action was a set (so adding new messages to an existing issue)
68 for msgid
in oldvalues
['messages']:
71 # figure which of the messages now on the issue weren't there before
72 for msgid
in cl
.get(nodeid
, 'messages'):
73 if not m
.has_key(msgid
):
74 messages
.append(msgid
)
77 def updatenosy(db
, cl
, nodeid
, newvalues
):
78 '''Update the nosy list for changes to the assignedto
80 # nodeid will be None if this is a new node
86 # old node, get the current values from the node if they haven't
88 if not newvalues
.has_key('nosy'):
89 nosy
= cl
.get(nodeid
, 'nosy')
91 current_nosy
.add(value
)
93 # if the nosy list changed in this transaction, init from the new value
94 if newvalues
.has_key('nosy'):
95 nosy
= newvalues
.get('nosy', [])
97 if not db
.hasnode('user', value
):
99 current_nosy
.add(value
)
101 new_nosy
= set(current_nosy
)
103 # add assignedto(s) to the nosy list
104 if newvalues
.has_key('assignedto') and newvalues
['assignedto'] is not None:
105 propdef
= cl
.getprops()
106 if isinstance(propdef
['assignedto'], hyperdb
.Link
):
107 assignedto_ids
= [newvalues
['assignedto']]
108 elif isinstance(propdef
['assignedto'], hyperdb
.Multilink
):
109 assignedto_ids
= newvalues
['assignedto']
110 for assignedto_id
in assignedto_ids
:
111 new_nosy
.add(assignedto_id
)
113 # see if there's any new messages - if so, possibly add the author and
114 # recipient to the nosy
115 if newvalues
.has_key('messages'):
118 messages
= newvalues
['messages']
121 # figure which of the messages now on the issue weren't
122 oldmessages
= cl
.get(nodeid
, 'messages')
124 for msgid
in newvalues
['messages']:
125 if msgid
not in oldmessages
:
126 messages
.append(msgid
)
128 # configs for nosy modifications
129 add_author
= getattr(db
.config
, 'ADD_AUTHOR_TO_NOSY', 'new')
130 add_recips
= getattr(db
.config
, 'ADD_RECIPIENTS_TO_NOSY', 'new')
132 # now for each new message:
134 for msgid
in messages
:
136 authid
= msg
.get(msgid
, 'author')
139 # add on the recipients of the message
141 for recipient
in msg
.get(msgid
, 'recipients'):
142 new_nosy
.add(recipient
)
144 if current_nosy
!= new_nosy
:
145 # that's it, save off the new nosy list
146 newvalues
['nosy'] = list(new_nosy
)
149 db
.issue
.react('create', nosyreaction
)
150 db
.issue
.react('set', nosyreaction
)
151 db
.issue
.audit('create', updatenosy
)
152 db
.issue
.audit('set', updatenosy
)
154 # vim: set filetype=python ts=4 sw=4 et si