def test_available_actions_chgprop_only(self): ts = TicketSystem(self.env) perm = Mock(has_permission=lambda x: x == 'TICKET_CHGPROP') self.assertEqual(['leave', 'reassign', 'accept'], ts.get_available_actions({'status': 'new'}, perm)) self.assertEqual(['leave', 'reassign'], ts.get_available_actions({'status': 'assigned'}, perm)) self.assertEqual(['leave', 'reassign'], ts.get_available_actions({'status': 'reopened'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'closed'}, perm))
def test_available_actions_no_perms(self): ts = TicketSystem(self.env) perm = Mock(has_permission=lambda x: 0) self.assertEqual(['leave'], ts.get_available_actions({'status': 'new'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'assigned'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'reopened'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'closed'}, perm))
def test_available_actions_no_perms(self): ts = TicketSystem(self.env) perm = Mock(has_permission=lambda x: 0) self.assertEqual(['leave'], ts.get_available_actions({'status': 'new'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'assigned'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'reopened'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'closed'}, perm))
def test_available_actions_create_only(self): ts = TicketSystem(self.env) perm = Mock(has_permission=lambda x: x == 'TICKET_CREATE') self.assertEqual(['leave'], ts.get_available_actions({'status': 'new'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'assigned'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'reopened'}, perm)) self.assertEqual(['leave', 'reopen'], ts.get_available_actions({'status': 'closed'}, perm))
def test_available_actions_chgprop_only(self): # CHGPROP is not enough for changing a ticket's state (#3289) ts = TicketSystem(self.env) perm = Mock(has_permission=lambda x: x == 'TICKET_CHGPROP') self.assertEqual(['leave'], ts.get_available_actions({'status': 'new'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'assigned'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'reopened'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'closed'}, perm))
def test_available_actions_chgprop_only(self): # CHGPROP is not enough for changing a ticket's state (#3289) ts = TicketSystem(self.env) perm = Mock(has_permission=lambda x: x == 'TICKET_CHGPROP') self.assertEqual(['leave'], ts.get_available_actions({'status': 'new'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'assigned'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'reopened'}, perm)) self.assertEqual(['leave'], ts.get_available_actions({'status': 'closed'}, perm))
def _get_action_controls(self, req, tickets): action_controls = [] ts = TicketSystem(self.env) tickets_by_action = {} for t in tickets: ticket = Ticket(self.env, t['id']) available_actions = ts.get_available_actions(req, ticket) for action in available_actions: tickets_by_action.setdefault(action, []).append(ticket) # Sort the allowed actions by the 'default' key. allowed_actions = set(tickets_by_action) workflow = ConfigurableTicketWorkflow(self.env) all_actions = sorted(((action['default'], name) for name, action in workflow.get_all_actions().iteritems()), reverse=True) sorted_actions = [action[1] for action in all_actions if action[1] in allowed_actions] for action in sorted_actions: first_label = None hints = [] widgets = [] ticket = tickets_by_action[action][0] for controller in self._get_action_controllers(req, ticket, action): label, widget, hint = controller.render_ticket_action_control( req, ticket, action) if not first_label: first_label = label widgets.append(widget) hints.append(hint) action_controls.append((action, first_label, tag(widgets), hints)) return action_controls
def _implementation(db): tkt = Ticket(self.env, ticket_id) ts = TicketSystem(self.env) tm = TicketModule(self.env) if action not in ts.get_available_actions(req, tkt): raise ValueError(["This ticket cannot be moved to this status,\ perhaps the ticket has been updated by someone else."]) field_changes, problems = \ tm.get_ticket_changes(req, tkt, action) if problems: raise ValueError(problems) tm._apply_ticket_changes(tkt, field_changes) valid = tm._validate_ticket(req, tkt, force_collision_check=True) if not valid: raise ValueError(req.chrome['warnings']) else: tkt.save_changes(req.authname, "", when=datetime.now(utc))
def getActions(self, req, id): """Returns the actions that can be performed on the ticket as a list of `[action, label, hints, [input_fields]]` elements, where `input_fields` is a list of `[name, value, [options]]` for any required action inputs.""" ts = TicketSystem(self.env) t = model.Ticket(self.env, id) actions = [] for action in ts.get_available_actions(req, t): fragment = genshi.builder.Fragment() hints = [] first_label = None for controller in ts.action_controllers: if action in [c_action for c_weight, c_action \ in controller.get_ticket_actions(req, t)]: label, widget, hint = \ controller.render_ticket_action_control(req, t, action) fragment += widget hints.append(to_unicode(hint).rstrip('.') + '.') first_label = first_label == None and label or first_label controls = [] for elem in fragment.children: if not isinstance(elem, genshi.builder.Element): continue if elem.tag == 'input': controls.append((elem.attrib.get('name'), elem.attrib.get('value'), [])) elif elem.tag == 'select': value = '' options = [] for opt in elem.children: if not (opt.tag == 'option' and opt.children): continue option = opt.children[0] options.append(option) if opt.attrib.get('selected'): value = option controls.append((elem.attrib.get('name'), value, options)) actions.append((action, first_label, " ".join(hints), controls)) return actions
def getActions(self, req, id): """Returns the actions that can be performed on the ticket as a list of `[action, label, hints, [input_fields]]` elements, where `input_fields` is a list of `[name, value, [options]]` for any required action inputs.""" ts = TicketSystem(self.env) t = model.Ticket(self.env, id) actions = [] for action in ts.get_available_actions(req, t): fragment = genshi.builder.Fragment() hints = [] first_label = None for controller in ts.action_controllers: if action in [c_action for c_weight, c_action \ in controller.get_ticket_actions(req, t)]: label, widget, hint = \ controller.render_ticket_action_control(req, t, action) fragment += widget hints.append(hint.rstrip('.') + '.') first_label = first_label == None and label or first_label controls = [] for elem in fragment.children: if not isinstance(elem, genshi.builder.Element): continue if elem.tag == 'input': controls.append((elem.attrib.get('name'), elem.attrib.get('value'), [])) elif elem.tag == 'select': value = '' options = [] for opt in elem.children: if not (opt.tag == 'option' and opt.children): continue option = opt.children[0] options.append(option) if opt.attrib.get('selected'): value = option controls.append((elem.attrib.get('name'), value, options)) actions.append((action, first_label, " ".join(hints), controls)) return actions
def _get_actions(self, ticket_dict): ts = TicketSystem(self.env) ticket = Ticket(self.env) ticket.populate(ticket_dict) id = ticket.insert() return ts.get_available_actions(self.req, Ticket(self.env, id))
def _get_actions(self, ticket_dict): ts = TicketSystem(self.env) ticket = Ticket(self.env) ticket.populate(ticket_dict) id = ticket.insert() return ts.get_available_actions(self.req, Ticket(self.env, id))
def update(self, req, id, comment, attributes={}, notify=False, author='', when=None): """ Update a ticket, returning the new ticket in the same form as get(). 'New-style' call requires two additional items in attributes: (1) 'action' for workflow support (including any supporting fields as retrieved by getActions()), (2) '_ts' changetime token for detecting update collisions (as received from get() or update() calls). ''Calling update without 'action' and '_ts' changetime token is deprecated, and will raise errors in a future version.'' """ t = model.Ticket(self.env, id) # custom author? if author and not (req.authname == 'anonymous' \ or 'TICKET_ADMIN' in req.perm(t.resource)): # only allow custom author if anonymous is permitted or user is admin self.log.warn( "RPC ticket.update: %r not allowed to change author " "to %r for comment on #%d", req.authname, author, id) author = '' author = author or req.authname # custom change timestamp? if when and not 'TICKET_ADMIN' in req.perm(t.resource): self.log.warn( "RPC ticket.update: %r not allowed to update #%d with " "non-current timestamp (%r)", author, id, when) when = None when = when or to_datetime(None, utc) # and action... if not 'action' in attributes: # FIXME: Old, non-restricted update - remove soon! self.log.warning("Rpc ticket.update for ticket %d by user %s " \ "has no workflow 'action'." % (id, req.authname)) req.perm(t.resource).require('TICKET_MODIFY') time_changed = attributes.pop('_ts', None) if time_changed and \ str(time_changed) != str(to_utimestamp(t.time_changed)): raise TracError("Ticket has been updated since last get().") for k, v in attributes.iteritems(): t[k] = v t.save_changes(author, comment, when=when) else: ts = TicketSystem(self.env) tm = TicketModule(self.env) # TODO: Deprecate update without time_changed timestamp time_changed = attributes.pop('_ts', to_utimestamp(t.time_changed)) try: time_changed = int(time_changed) except ValueError: raise TracError("RPC ticket.update: Wrong '_ts' token " \ "in attributes (%r)." % time_changed) action = attributes.get('action') avail_actions = ts.get_available_actions(req, t) if not action in avail_actions: raise TracError("Rpc: Ticket %d by %s " \ "invalid action '%s'" % (id, req.authname, action)) controllers = list(tm._get_action_controllers(req, t, action)) all_fields = [field['name'] for field in ts.get_ticket_fields()] for k, v in attributes.iteritems(): if k in all_fields and k != 'status': t[k] = v # TicketModule reads req.args - need to move things there... req.args.update(attributes) req.args['comment'] = comment # Collision detection: 0.11+0.12 timestamp req.args['ts'] = str(from_utimestamp(time_changed)) # Collision detection: 0.13/1.0+ timestamp req.args['view_time'] = str(time_changed) changes, problems = tm.get_ticket_changes(req, t, action) for warning in problems: add_warning(req, "Rpc ticket.update: %s" % warning) valid = problems and False or tm._validate_ticket(req, t) if not valid: raise TracError(" ".join( [warning for warning in req.chrome['warnings']])) else: tm._apply_ticket_changes(t, changes) self.log.debug("Rpc ticket.update save: %s" % repr(t.values)) t.save_changes(author, comment, when=when) # Apply workflow side-effects for controller in controllers: controller.apply_action_side_effects(req, t, action) if notify: try: tn = TicketNotifyEmail(self.env) tn.notify(t, newticket=False, modtime=when) except Exception, e: self.log.exception("Failure sending notification on change of " "ticket #%s: %s" % (t.id, e))
def update(self, req, id, comment, attributes={}, notify=False, author='', when=None): """ Update a ticket, returning the new ticket in the same form as get(). 'New-style' call requires two additional items in attributes: (1) 'action' for workflow support (including any supporting fields as retrieved by getActions()), (2) '_ts' changetime token for detecting update collisions (as received from get() or update() calls). ''Calling update without 'action' and '_ts' changetime token is deprecated, and will raise errors in a future version.'' """ t = model.Ticket(self.env, id) # custom author? if author and not (req.authname == 'anonymous' \ or 'TICKET_ADMIN' in req.perm(t.resource)): # only allow custom author if anonymous is permitted or user is admin self.log.warn("RPC ticket.update: %r not allowed to change author " "to %r for comment on #%d", req.authname, author, id) author = '' author = author or req.authname # custom change timestamp? if when and not 'TICKET_ADMIN' in req.perm(t.resource): self.log.warn("RPC ticket.update: %r not allowed to update #%d with " "non-current timestamp (%r)", author, id, when) when = None when = when or to_datetime(None, utc) # and action... if not 'action' in attributes: # FIXME: Old, non-restricted update - remove soon! self.log.warning("Rpc ticket.update for ticket %d by user %s " \ "has no workflow 'action'." % (id, req.authname)) req.perm(t.resource).require('TICKET_MODIFY') time_changed = attributes.pop('_ts', None) if time_changed and \ str(time_changed) != str(to_utimestamp(t.time_changed)): raise TracError("Ticket has been updated since last get().") for k, v in attributes.iteritems(): t[k] = v t.save_changes(author, comment, when=when) else: ts = TicketSystem(self.env) tm = TicketModule(self.env) # TODO: Deprecate update without time_changed timestamp time_changed = attributes.pop('_ts', to_utimestamp(t.time_changed)) try: time_changed = int(time_changed) except ValueError: raise TracError("RPC ticket.update: Wrong '_ts' token " \ "in attributes (%r)." % time_changed) action = attributes.get('action') avail_actions = ts.get_available_actions(req, t) if not action in avail_actions: raise TracError("Rpc: Ticket %d by %s " \ "invalid action '%s'" % (id, req.authname, action)) controllers = list(tm._get_action_controllers(req, t, action)) all_fields = [field['name'] for field in ts.get_ticket_fields()] for k, v in attributes.iteritems(): if k in all_fields and k != 'status': t[k] = v # TicketModule reads req.args - need to move things there... req.args.update(attributes) req.args['comment'] = comment # Collision detection: 0.11+0.12 timestamp req.args['ts'] = str(from_utimestamp(time_changed)) # Collision detection: 0.13/1.0+ timestamp req.args['view_time'] = str(time_changed) changes, problems = tm.get_ticket_changes(req, t, action) for warning in problems: add_warning(req, "Rpc ticket.update: %s" % warning) valid = problems and False or tm._validate_ticket(req, t) if not valid: raise TracError( " ".join([warning for warning in req.chrome['warnings']])) else: tm._apply_ticket_changes(t, changes) self.log.debug("Rpc ticket.update save: %s" % repr(t.values)) t.save_changes(author, comment, when=when) # Apply workflow side-effects for controller in controllers: controller.apply_action_side_effects(req, t, action) if notify: try: tn = TicketNotifyEmail(self.env) tn.notify(t, newticket=False, modtime=when) except Exception, e: self.log.exception("Failure sending notification on change of " "ticket #%s: %s" % (t.id, e))
def invoke(self, message, warnings): """reply to a ticket""" ticket = self.ticket reporter = self._reporter(message) # get the mailBody and attachments mailBody, attachments = get_body_and_attachments(message) if not mailBody: warnings.append("Seems to be a reply to %s but I couldn't find a comment") return message #go throught work ts = TicketSystem(self.env) tm = TicketModule(self.env) perm = PermissionSystem(self.env) # TODO: Deprecate update without time_changed timestamp mockReq = self._MockReq(perm.get_user_permissions(reporter), reporter) avail_actions = ts.get_available_actions(mockReq, ticket) mailBody, inBodyFields, actions = self._get_in_body_fields(mailBody, avail_actions, reporter) if inBodyFields or actions : # check permissions perm = PermissionSystem(self.env) #we have properties movement, cheking user permission to do so if not perm.check_permission('MAIL2TICKET_PROPERTIES', reporter) : # None -> 'anoymous' raise ("%s does not have MAIL2TICKET_PROPERTIES permissions" % (user or 'anonymous')) action = None if actions : action = actions.keys()[0] controllers = list(tm._get_action_controllers(mockReq, ticket, action)) all_fields = [field['name'] for field in ts.get_ticket_fields()] #impact changes find in inBodyFields for field in inBodyFields : ticket._old[field] = ticket[field] ticket.values[field] = inBodyFields[field] mockReq.args[field] = inBodyFields[field] if action : mockReq.args['action_%s_reassign_owner' % action] = ticket['owner'] mockReq.args['comment'] = mailBody mockReq.args['ts'] = datetime.now()#to_datetime(None, utc) mockReq.args['ts'] = str(ticket.time_changed) changes, problems = tm.get_ticket_changes(mockReq, ticket, action) valid = problems and False or tm._validate_ticket(mockReq, ticket) tm._apply_ticket_changes(ticket, changes) # add attachments to the ticket add_attachments(self.env, ticket, attachments) ticket.save_changes(reporter, mailBody) for controller in controllers: controller.apply_action_side_effects(mockReq, ticket, action) # Call ticket change listeners for listener in ts.change_listeners: listener.ticket_changed(ticket, mailBody, reporter, ticket._old) tn = TicketNotifyEmail(self.env) tn.notify(ticket, newticket=0, modtime=ticket.time_changed)
def _get_actions(self, ticket_dict): ts = TicketSystem(self.env) ticket = insert_ticket(self.env, **ticket_dict) return ts.get_available_actions(self.req, Ticket(self.env, ticket.id))
def getAvailableActions(self, req, id): """Returns the actions that can be performed on the ticket.""" ticketSystem = TicketSystem(self.env) t = model.Ticket(self.env, id) return ticketSystem.get_available_actions(t, req.perm)