Commits

Christian Boos committed f8edcca

ticket links: further simplify link updates

Comments (0)

Files changed (1)

trac/ticket/model.py

         The `db` argument is deprecated in favor of `with_transaction()`.
         """
         assert self.exists, 'Cannot update a new ticket'
+        api = TicketSystem(self.env)
 
         if not self._old and not comment:
             return False # Not modified
 
             # Update links
             for end in (e for e in link_fields if e in self._old):
-                new_ids = TicketSystem(self.env).parse_links(self[end])
-                old_ids = TicketSystem(self.env).parse_links(self._old[end])
+                new_ids = set(api.parse_links(self[end]))
+                old_ids = set(api.parse_links(self._old[end]))
                 # New links added
-                added = [id for id in new_ids if id not in old_ids]
-                for id in added:
+                for id in new_ids - old_ids:
                     cursor.execute("""
                         INSERT INTO ticket_links (source,destination,type)
                         VALUES (%s,%s,%s)
                         """, [self.id, id, end])
-                    other_end = TicketSystem(self.env).link_ends_map[end]
+                    other_end = api.link_ends_map[end]
                     if other_end:
                         cursor.execute("""
                             INSERT INTO ticket_links (source,destination,type)
                             VALUES (%s,%s,%s)
                             """, [id, self.id, other_end])
                 # Old links removed
-                removed = [id for id in old_ids if id not in new_ids]
-                for id in removed:
+                for id in old_ids - new_ids:
                     cursor.execute("""
                         DELETE FROM ticket_links 
                         WHERE source=%s AND destination=%s AND type=%s
                         """, [self.id, id, end])
-                    other_end = TicketSystem(self.env).link_ends_map[end]
+                    other_end = api.link_ends_map[end]
                     if other_end:
                         cursor.execute("""
                             DELETE FROM ticket_links
                             WHERE source=%s AND destination=%s AND type=%s
                             """, [id, self.id, other_end])
-                if added or removed:
+                if old_ids ^ new_ids:
                     cursor.execute("""
                         INSERT INTO ticket_change
                             (ticket,time,author,field,oldvalue,newvalue)
         self._old = {}
         self.values['changetime'] = when
         
-        for listener in TicketSystem(self.env).change_listeners:
+        for listener in api.change_listeners:
             listener.ticket_changed(self, comment, author, old_values)
         return True