Code

a1a1536f62996a48c7c95ed2881eccd85520bf91
[roundup.git] / share / roundup / templates / devel / detectors / nosyreaction.py
1 import sets
3 from roundup import roundupdb, hyperdb
5 def nosyreaction(db, cl, nodeid, oldvalues):
6     ''' A standard detector is provided that watches for additions to the
7         "messages" property.
8         
9         When a new message is added, the detector sends it to all the users on
10         the "nosy" list for the issue that are not already on the "recipients"
11         list of the message.
12         
13         Those users are then appended to the "recipients" property on the
14         message, so multiple copies of a message are never sent to the same
15         user.
16         
17         The journal recorded by the hyperdatabase on the "recipients" property
18         then provides a log of when the message was sent to whom. 
19     '''
20     # send a copy of all new messages to the nosy list
21     for msgid in determineNewMessages(cl, nodeid, oldvalues):
22         try:
23             cl.nosymessage(nodeid, msgid, oldvalues)
24         except roundupdb.MessageSendError, message:
25             raise roundupdb.DetectorError, message
27 def determineNewMessages(cl, nodeid, oldvalues):
28     ''' Figure a list of the messages that are being added to the given
29         node in this transaction.
30     '''
31     messages = []
32     if oldvalues is None:
33         # the action was a create, so use all the messages in the create
34         messages = cl.get(nodeid, 'messages')
35     elif oldvalues.has_key('messages'):
36         # the action was a set (so adding new messages to an existing issue)
37         m = {}
38         for msgid in oldvalues['messages']:
39             m[msgid] = 1
40         messages = []
41         # figure which of the messages now on the issue weren't there before
42         for msgid in cl.get(nodeid, 'messages'):
43             if not m.has_key(msgid):
44                 messages.append(msgid)
45     return messages
47 def updatenosy(db, cl, nodeid, newvalues):
48     '''Update the nosy list for changes to the assignedto
49     '''
50     # nodeid will be None if this is a new node
51     current_nosy = sets.Set()
52     if nodeid is None:
53         ok = ('new', 'yes')
54     else:
55         ok = ('yes',)
56         # old node, get the current values from the node if they haven't
57         # changed
58         if not newvalues.has_key('nosy'):
59             nosy = cl.get(nodeid, 'nosy')
60             for value in nosy:
61                 current_nosy.add(value)
63     # if the nosy list changed in this transaction, init from the new value
64     if newvalues.has_key('nosy'):
65         nosy = newvalues.get('nosy', [])
66         for value in nosy:
67             if not db.hasnode('user', value):
68                 continue
69             current_nosy.add(value)
71     new_nosy = sets.Set(current_nosy)
73     # add assignedto(s) to the nosy list
74     if newvalues.has_key('assignedto') and newvalues['assignedto'] is not None:
75         propdef = cl.getprops()
76         if isinstance(propdef['assignedto'], hyperdb.Link):
77             assignedto_ids = [newvalues['assignedto']]
78         elif isinstance(propdef['assignedto'], hyperdb.Multilink):
79             assignedto_ids = newvalues['assignedto']
80         for assignedto_id in assignedto_ids:
81             new_nosy.add(assignedto_id)
83     # see if there's any new messages - if so, possibly add the author and
84     # recipient to the nosy
85     if newvalues.has_key('messages'):
86         if nodeid is None:
87             ok = ('new', 'yes')
88             messages = newvalues['messages']
89         else:
90             ok = ('yes',)
91             # figure which of the messages now on the issue weren't
92             oldmessages = cl.get(nodeid, 'messages')
93             messages = []
94             for msgid in newvalues['messages']:
95                 if msgid not in oldmessages:
96                     messages.append(msgid)
98         # configs for nosy modifications
99         add_author = getattr(db.config, 'ADD_AUTHOR_TO_NOSY', 'new')
100         add_recips = getattr(db.config, 'ADD_RECIPIENTS_TO_NOSY', 'new')
102         # now for each new message:
103         msg = db.msg
104         for msgid in messages:
105             if add_author in ok:
106                 authid = msg.get(msgid, 'author')
107                 new_nosy.add(authid)
109             # add on the recipients of the message
110             if add_recips in ok:
111                 for recipient in msg.get(msgid, 'recipients'):
112                     new_nosy.add(recipient)
114     if current_nosy != new_nosy:
115         # that's it, save off the new nosy list
116         newvalues['nosy'] = list(new_nosy)
118 def init(db):
119     db.bug.react('create', nosyreaction)
120     db.bug.react('set', nosyreaction)
121     db.bug.audit('create', updatenosy)
122     db.bug.audit('set', updatenosy)
124     db.task.react('create', nosyreaction)
125     db.task.react('set', nosyreaction)
126     db.task.audit('create', updatenosy)
127     db.task.audit('set', updatenosy)
129     db.milestone.react('create', nosyreaction)
130     db.milestone.react('set', nosyreaction)
131     db.milestone.audit('create', updatenosy)
132     db.milestone.audit('set', updatenosy)