Ajout script mise a jour de tracker
[auf_roundup.git] / share / roundup / templates / classic / detectors / nosyreaction.py
CommitLineData
c638d827
CR
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 $
19
20# Python 2.3 ... 2.6 compatibility:
21from roundup.anypy.sets_ import set
22
23from roundup import roundupdb, hyperdb
24
25def nosyreaction(db, cl, nodeid, oldvalues):
26 ''' A standard detector is provided that watches for additions to the
27 "messages" property.
28
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"
31 list of the message.
32
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
35 user.
36
37 The journal recorded by the hyperdatabase on the "recipients" property
38 then provides a log of when the message was sent to whom.
39 '''
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 " \
47 + "possible."
48 cl.send_message(nodeid, None, msg, [authaddr,])
49
50 # send a copy of all new messages to the nosy list
51 for msgid in determineNewMessages(cl, nodeid, oldvalues):
52 try:
53 cl.nosymessage(nodeid, msgid, oldvalues)
54 except roundupdb.MessageSendError, message:
55 raise roundupdb.DetectorError, message
56
57def determineNewMessages(cl, nodeid, oldvalues):
58 ''' Figure a list of the messages that are being added to the given
59 node in this transaction.
60 '''
61 messages = []
62 if oldvalues is None:
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)
67 m = {}
68 for msgid in oldvalues['messages']:
69 m[msgid] = 1
70 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)
75 return messages
76
77def updatenosy(db, cl, nodeid, newvalues):
78 '''Update the nosy list for changes to the assignedto
79 '''
80 # nodeid will be None if this is a new node
81 current_nosy = set()
82 if nodeid is None:
83 ok = ('new', 'yes')
84 else:
85 ok = ('yes',)
86 # old node, get the current values from the node if they haven't
87 # changed
88 if not newvalues.has_key('nosy'):
89 nosy = cl.get(nodeid, 'nosy')
90 for value in nosy:
91 current_nosy.add(value)
92
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', [])
96 for value in nosy:
97 if not db.hasnode('user', value):
98 continue
99 current_nosy.add(value)
100
101 new_nosy = set(current_nosy)
102
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)
112
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'):
116 if nodeid is None:
117 ok = ('new', 'yes')
118 messages = newvalues['messages']
119 else:
120 ok = ('yes',)
121 # figure which of the messages now on the issue weren't
122 oldmessages = cl.get(nodeid, 'messages')
123 messages = []
124 for msgid in newvalues['messages']:
125 if msgid not in oldmessages:
126 messages.append(msgid)
127
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')
131
132 # now for each new message:
133 msg = db.msg
134 for msgid in messages:
135 if add_author in ok:
136 authid = msg.get(msgid, 'author')
137 new_nosy.add(authid)
138
139 # add on the recipients of the message
140 if add_recips in ok:
141 for recipient in msg.get(msgid, 'recipients'):
142 new_nosy.add(recipient)
143
144 if current_nosy != new_nosy:
145 # that's it, save off the new nosy list
146 newvalues['nosy'] = list(new_nosy)
147
148def init(db):
149 db.issue.react('create', nosyreaction)
150 db.issue.react('set', nosyreaction)
151 db.issue.audit('create', updatenosy)
152 db.issue.audit('set', updatenosy)
153
154# vim: set filetype=python ts=4 sw=4 et si