1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/themes/mercurialroundup/detectors/nosyreaction.py Mon Jun 07 01:14:02 2010 +0200
1.3 @@ -0,0 +1,144 @@
1.4 +#
1.5 +# Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
1.6 +# This module is free software, and you may redistribute it and/or modify
1.7 +# under the same terms as Python, so long as this copyright message and
1.8 +# disclaimer are retained in their original form.
1.9 +#
1.10 +# IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
1.11 +# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
1.12 +# OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
1.13 +# POSSIBILITY OF SUCH DAMAGE.
1.14 +#
1.15 +# BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
1.16 +# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1.17 +# FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
1.18 +# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1.19 +# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1.20 +#
1.21 +#$Id: nosyreaction.py,v 1.4 2005-04-04 08:47:14 richard Exp $
1.22 +
1.23 +# Python 2.3 ... 2.6 compatibility:
1.24 +from roundup.anypy.sets_ import set
1.25 +
1.26 +from roundup import roundupdb, hyperdb
1.27 +
1.28 +def nosyreaction(db, cl, nodeid, oldvalues):
1.29 + ''' A standard detector is provided that watches for additions to the
1.30 + "messages" property.
1.31 +
1.32 + When a new message is added, the detector sends it to all the users on
1.33 + the "nosy" list for the issue that are not already on the "recipients"
1.34 + list of the message.
1.35 +
1.36 + Those users are then appended to the "recipients" property on the
1.37 + message, so multiple copies of a message are never sent to the same
1.38 + user.
1.39 +
1.40 + The journal recorded by the hyperdatabase on the "recipients" property
1.41 + then provides a log of when the message was sent to whom.
1.42 + '''
1.43 + # send a copy of all new messages to the nosy list
1.44 + for msgid in determineNewMessages(cl, nodeid, oldvalues):
1.45 + try:
1.46 + cl.nosymessage(nodeid, msgid, oldvalues)
1.47 + except roundupdb.MessageSendError, message:
1.48 + raise roundupdb.DetectorError, message
1.49 +
1.50 +def determineNewMessages(cl, nodeid, oldvalues):
1.51 + ''' Figure a list of the messages that are being added to the given
1.52 + node in this transaction.
1.53 + '''
1.54 + messages = []
1.55 + if oldvalues is None:
1.56 + # the action was a create, so use all the messages in the create
1.57 + messages = cl.get(nodeid, 'messages')
1.58 + elif oldvalues.has_key('messages'):
1.59 + # the action was a set (so adding new messages to an existing issue)
1.60 + m = {}
1.61 + for msgid in oldvalues['messages']:
1.62 + m[msgid] = 1
1.63 + messages = []
1.64 + # figure which of the messages now on the issue weren't there before
1.65 + for msgid in cl.get(nodeid, 'messages'):
1.66 + if not m.has_key(msgid):
1.67 + messages.append(msgid)
1.68 + return messages
1.69 +
1.70 +def updatenosy(db, cl, nodeid, newvalues):
1.71 + '''Update the nosy list for changes to the assignedto
1.72 + '''
1.73 + # nodeid will be None if this is a new node
1.74 + current_nosy = set()
1.75 + if nodeid is None:
1.76 + ok = ('new', 'yes')
1.77 + else:
1.78 + ok = ('yes',)
1.79 + # old node, get the current values from the node if they haven't
1.80 + # changed
1.81 + if not newvalues.has_key('nosy'):
1.82 + nosy = cl.get(nodeid, 'nosy')
1.83 + for value in nosy:
1.84 + current_nosy.add(value)
1.85 +
1.86 + # if the nosy list changed in this transaction, init from the new value
1.87 + if newvalues.has_key('nosy'):
1.88 + nosy = newvalues.get('nosy', [])
1.89 + for value in nosy:
1.90 + if not db.hasnode('user', value):
1.91 + continue
1.92 + current_nosy.add(value)
1.93 +
1.94 + new_nosy = set(current_nosy)
1.95 +
1.96 + # add assignedto(s) to the nosy list
1.97 + if newvalues.has_key('assignedto') and newvalues['assignedto'] is not None:
1.98 + propdef = cl.getprops()
1.99 + if isinstance(propdef['assignedto'], hyperdb.Link):
1.100 + assignedto_ids = [newvalues['assignedto']]
1.101 + elif isinstance(propdef['assignedto'], hyperdb.Multilink):
1.102 + assignedto_ids = newvalues['assignedto']
1.103 + for assignedto_id in assignedto_ids:
1.104 + new_nosy.add(assignedto_id)
1.105 +
1.106 + # see if there's any new messages - if so, possibly add the author and
1.107 + # recipient to the nosy
1.108 + if newvalues.has_key('messages'):
1.109 + if nodeid is None:
1.110 + ok = ('new', 'yes')
1.111 + messages = newvalues['messages']
1.112 + else:
1.113 + ok = ('yes',)
1.114 + # figure which of the messages now on the issue weren't
1.115 + oldmessages = cl.get(nodeid, 'messages')
1.116 + messages = []
1.117 + for msgid in newvalues['messages']:
1.118 + if msgid not in oldmessages:
1.119 + messages.append(msgid)
1.120 +
1.121 + # configs for nosy modifications
1.122 + add_author = getattr(db.config, 'ADD_AUTHOR_TO_NOSY', 'new')
1.123 + add_recips = getattr(db.config, 'ADD_RECIPIENTS_TO_NOSY', 'new')
1.124 +
1.125 + # now for each new message:
1.126 + msg = db.msg
1.127 + for msgid in messages:
1.128 + if add_author in ok:
1.129 + authid = msg.get(msgid, 'author')
1.130 + new_nosy.add(authid)
1.131 +
1.132 + # add on the recipients of the message
1.133 + if add_recips in ok:
1.134 + for recipient in msg.get(msgid, 'recipients'):
1.135 + new_nosy.add(recipient)
1.136 +
1.137 + if current_nosy != new_nosy:
1.138 + # that's it, save off the new nosy list
1.139 + newvalues['nosy'] = list(new_nosy)
1.140 +
1.141 +def init(db):
1.142 + db.issue.react('create', nosyreaction)
1.143 + db.issue.react('set', nosyreaction)
1.144 + db.issue.audit('create', updatenosy)
1.145 + db.issue.audit('set', updatenosy)
1.146 +
1.147 +# vim: set filetype=python ts=4 sw=4 et si