Permalink
Browse files

Refs #27: Modified `validate_ticket` so that individual messages are …

…reported for each valid ticket and the parent list is fixed-up to remove all invalid ticket. Modified `post_process_request` to avoid an exception during auto-preview.
  • Loading branch information...
1 parent 0b7c581 commit f7e4683bf5de4355a24d8ced383d101afeaf2f39 @rjollos rjollos committed Oct 22, 2012
Showing with 35 additions and 25 deletions.
  1. +23 −14 tracsubtickets/api.py
  2. +12 −11 tracsubtickets/web_ui.py
View
@@ -34,6 +34,7 @@
from trac.core import *
from trac.env import IEnvironmentSetupParticipant
from trac.db import DatabaseManager
+from trac.resource import ResourceNotFound
from trac.ticket.model import Ticket
from trac.ticket.api import ITicketChangeListener, ITicketManipulator
from trac.ticket.notification import TicketNotifyEmail
@@ -185,19 +186,20 @@ def validate_ticket(self, req, ticket):
cursor = db.cursor()
try:
- ids = []
+ invalid_ids = set()
_ids = set(NUMBERS_RE.findall(ticket['parents'] or ''))
myid = str(ticket.id)
for id in _ids:
if id == myid:
+ invalid_ids.add(id)
yield 'parents', _('A ticket cannot be a parent to itself')
else:
# check if the id exists
cursor.execute("SELECT id FROM ticket WHERE id=%s", (id, ))
row = cursor.fetchone()
if row is None:
+ invalid_ids.add(id)
yield 'parents', _('Ticket #%s does not exist') % id
- ids.append(id)
# circularity check function
def _check_parents(id, all_parents):
@@ -206,26 +208,33 @@ def _check_parents(id, all_parents):
cursor.execute("SELECT parent FROM subtickets WHERE child=%s", (id, ))
for x in [int(x[0]) for x in cursor]:
if x in all_parents:
+ invalid_ids.add(x)
error = ' > '.join(['#%s' % n for n in all_parents + [x]])
errors.append(('parents', _('Circularity error: %s') % error))
else:
errors += _check_parents(x, all_parents)
return errors
- for x in ids:
+ for x in _ids:
# check parent ticket state
- parent = Ticket(self.env, x)
- if parent and parent['status'] == 'closed' and ticket['status'] != 'closed':
- yield 'parents', _('Parent ticket #%s is closed') % x
- else:
- # check circularity
- all_parents = ticket.id and [ticket.id] or []
- for error in _check_parents(int(x), all_parents):
- yield error
+ try:
+ parent = Ticket(self.env, x)
+ if parent and parent['status'] == 'closed' and ticket['status'] != 'closed':
+ invalid_ids.add(x)
+ yield 'parents', _('Parent ticket #%s is closed.') % x
+ else:
+ # check circularity
+ all_parents = ticket.id and [ticket.id] or []
+ for error in _check_parents(int(x), all_parents):
+ yield error
+ except ResourceNotFound, e:
+ invalid_ids.add(x)
- ticket['parents'] = ', '.join(sorted(ids, key=lambda x: int(x)))
+ valid_ids = _ids.difference(invalid_ids)
+ ticket['parents'] = valid_ids and ', '.join(sorted(valid_ids, key=lambda x: int(x))) or ''
except Exception, e:
- self.log.error(e)
- yield 'parents', _('Not a valid list of ticket IDs')
+ import traceback
+ self.log.error(traceback.format_exc())
+ yield 'parents', _('Not a valid list of ticket IDs.')
View
@@ -60,17 +60,18 @@ def pre_process_request(self, req, handler):
def post_process_request(self, req, template, data, content_type):
path = req.path_info
if path.startswith('/ticket/') or path.startswith('/newticket'):
- # get parents data
- ticket = data['ticket']
- parents = ticket['parents'] or ''
- ids = set(NUMBERS_RE.findall(parents))
-
- if len(parents) > 0:
- self._append_parent_links(req, data, ids)
-
- children = self.get_children(ticket.id)
- if children:
- data['subtickets'] = children
+ # get parent ticket's data
+ if data and 'ticket' in data:
+ ticket = data['ticket']
+ parents = ticket['parents'] or ''
+ ids = set(NUMBERS_RE.findall(parents))
+
+ if len(parents) > 0:
+ self._append_parent_links(req, data, ids)
+
+ children = self.get_children(ticket.id)
+ if children:
+ data['subtickets'] = children
return template, data, content_type

0 comments on commit f7e4683

Please sign in to comment.