Commits

Alex Willmer  committed 266812b Merge

Merge from ticket-links-trunk

  • Participants
  • Parent commits d7b72ef, 6211723

Comments (0)

Files changed (1)

File trac/ticket/model.py

 # Author: Jonas Borgström <jonas@edgewall.com>
 #         Christopher Lenz <cmlenz@gmx.de>
 
+import itertools
+import operator
 import re
 from datetime import date, datetime
 
                 
         # Fetch links 
         link_fields = [f['name'] for f in self.fields if f.get('link')]
+        cursor.execute("""SELECT type, destination 
+                       FROM ticket_links
+                       WHERE source=%%s and type IN (%s)
+                       ORDER BY type, destination
+                       """ 
+                       % ','.join(['%s'] * len(link_fields)), 
+                       [tkt_id] + link_fields)
+        grouped_recs = itertools.groupby(cursor, operator.itemgetter(0))
+        link_values = dict((end, ', '.join(['#%s' % r[1] for r in recs]))
+                           for end, recs in grouped_recs)
         for end in link_fields:
-            cursor.execute("""
-                SELECT destination FROM ticket_links
-                WHERE source=%s and type=%s
-            """, (tkt_id, end))
-            link_list = []
-            for destination in cursor:
-                link_list.append(destination)
-
-            link_list.sort()
-            self.values[end] = ', '.join(['#%s' % v for v in link_list])
+            self.values[end] = link_values.get(end, '')
             
 
     def __getitem__(self, name):