# # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/) # This module is free software, and you may redistribute it and/or modify # under the same terms as Python, so long as this copyright message and # disclaimer are retained in their original form. # # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # from roundup import roundupdb, hyperdb def nosyreaction(db, cl, nodeid, oldvalues): ''' A standard detector is provided that watches for additions to the "messages" property. When a new message is added, the detector sends it to all the users on the "nosy" list for the issue that are not already on the "recipients" list of the message. Those users are then appended to the "recipients" property on the message, so multiple copies of a message are never sent to the same user. The journal recorded by the hyperdatabase on the "recipients" property then provides a log of when the message was sent to whom. ''' # send a copy of all new messages to the nosy list for msgid in determineNewMessages(cl, nodeid, oldvalues): try: cl.nosymessage(nodeid, msgid, oldvalues, note_filter=format_lmit_message) except roundupdb.MessageSendError as message: raise roundupdb.DetectorError(message) def format_lmit_message(original_note, issue_id, db, newvalues, oldvalues): """ Format the message like the lmit_oncall tracker did with nice field names. Determine if the description changed or if there are new Update/Resolution, and return detail that should go into the body of the email message. """ email_details = [] new_description = newvalues.get(issue_id, 'description') # If this is a new issue and it has a description, or if the issue # was edited and the description changed. have_new_description = ((oldvalues is None and new_description) or (oldvalues is not None and 'description' in oldvalues and (oldvalues.get('description') != new_description))) new_messages = determineNewMessages(newvalues, issue_id, oldvalues) # Have new detail. Add the common detail first. if have_new_description or new_messages: if newvalues.get(issue_id, 'pagedate'): email_details.append('Page Date: %s' % (newvalues.get(issue_id, 'pagedate').pretty(format='%Y-%m-%d'),)) if newvalues.get(issue_id, 'pagetime'): email_details.append('Page Time: %s' % (newvalues.get(issue_id, 'pagetime'),)) email_details.append('Name: %s' % (newvalues.get(issue_id, 'username'),)) source_id = newvalues.get(issue_id, 'source') if source_id: email_details.append('Source: %s' % (db.source.get(source_id, 'name'),)) location_id = newvalues.get(issue_id, 'location') if location_id: email_details.append('Location: %s' % (db.location.get(location_id, 'name'),)) if have_new_description: email_details.append('Problem Description: %s' % (new_description,)) # Add messages (Update/Resolution) if there are any. messages = newvalues.db.msg for msgid in new_messages: content = messages.get(msgid, 'content', '') if content: email_details.append('Update/Resolution: %s' % (content,)) return '\n\n'.join(email_details) def determineNewMessages(cl, nodeid, oldvalues): ''' Figure a list of the messages that are being added to the given node in this transaction. ''' messages = [] if oldvalues is None: # the action was a create, so use all the messages in the create messages = cl.get(nodeid, 'messages') elif 'messages' in oldvalues: # the action was a set (so adding new messages to an existing issue) m = {} for msgid in oldvalues['messages']: m[msgid] = 1 messages = [] # figure which of the messages now on the issue weren't there before for msgid in cl.get(nodeid, 'messages'): if msgid not in m: messages.append(msgid) return messages def updatenosy(db, cl, nodeid, newvalues): '''Update the nosy list for changes to the assignedto ''' # nodeid will be None if this is a new node current_nosy = set() if nodeid is None: ok = ('new', 'yes') else: ok = ('yes',) # old node, get the current values from the node if they haven't # changed if 'nosy' not in newvalues: nosy = cl.get(nodeid, 'nosy') for value in nosy: current_nosy.add(value) # if the nosy list changed in this transaction, init from the new value if 'nosy' in newvalues: nosy = newvalues.get('nosy', []) for value in nosy: if not db.hasnode('user', value): continue current_nosy.add(value) new_nosy = set(current_nosy) # add assignedto(s) to the nosy list if 'assignedto' in newvalues and newvalues['assignedto'] is not None: propdef = cl.getprops() if isinstance(propdef['assignedto'], hyperdb.Link): assignedto_ids = [newvalues['assignedto']] elif isinstance(propdef['assignedto'], hyperdb.Multilink): assignedto_ids = newvalues['assignedto'] for assignedto_id in assignedto_ids: new_nosy.add(assignedto_id) # see if there's any new messages - if so, possibly add the author and # recipient to the nosy if 'messages' in newvalues: if nodeid is None: ok = ('new', 'yes') messages = newvalues['messages'] else: ok = ('yes',) # figure which of the messages now on the issue weren't oldmessages = cl.get(nodeid, 'messages') messages = [] for msgid in newvalues['messages']: if msgid not in oldmessages: messages.append(msgid) # configs for nosy modifications add_author = getattr(db.config, 'ADD_AUTHOR_TO_NOSY', 'new') add_recips = getattr(db.config, 'ADD_RECIPIENTS_TO_NOSY', 'new') # now for each new message: msg = db.msg for msgid in messages: if add_author in ok: authid = msg.get(msgid, 'author') new_nosy.add(authid) # add on the recipients of the message if add_recips in ok: for recipient in msg.get(msgid, 'recipients'): new_nosy.add(recipient) if current_nosy != new_nosy: # that's it, save off the new nosy list newvalues['nosy'] = list(new_nosy) def init(db): db.issue.react('create', nosyreaction) db.issue.react('set', nosyreaction) db.issue.audit('create', updatenosy) db.issue.audit('set', updatenosy) # vim: set filetype=python ts=4 sw=4 et si