Source

trac-t10425 / default_order.diff

Full commit
# HG changeset patch
# Parent efb2b2e7af2b9204c99c084dfea6f37a7afde089

diff --git a/trac/ticket/query.py b/trac/ticket/query.py
--- a/trac/ticket/query.py
+++ b/trac/ticket/query.py
@@ -126,7 +126,13 @@
                      c == 'id']
         self.rows = [c for c in rows if c in field_names]
         if self.order != 'id' and self.order not in field_names:
-            self.order = 'priority'
+            default_order = QueryModule(self.env).default_order or 'priority'
+            if default_order[0] == '-':
+                (self.order, self.desc) = (default_order[1:], 1)
+            elif default_order[0] == '+':
+                (self.order, self.desc) = (default_order[1:], 0)
+            else:
+                (self.order, self.desc) = (default_order, self.desc or 0)
 
         if self.group not in field_names:
             self.group = None
@@ -849,6 +855,10 @@
         default=None,
         doc="""List of columns to show in query unless defined by the query.
             Default is to let Trac calculate this internally. (''since 0.13'')""")
+    
+    default_order = Option('query', 'default_order',
+        default='priority',
+        doc="""The default order to use for queries. (''since 0.13'')""")
 
     # IContentConverter methods
 
diff --git a/trac/ticket/tests/query.py b/trac/ticket/tests/query.py
--- a/trac/ticket/tests/query.py
+++ b/trac/ticket/tests/query.py
@@ -498,6 +498,67 @@
         self.assertEqual(['anonymous'], args)
         tickets = query.execute(self.req)
 
+    def test_default_orders_no_config_with_request(self):
+        self.env.config.remove('query', 'default_orders')
+        query = Query(self.env, order='summary')
+        sql, args = query.get_sql()
+        self.assertEqualSQL(sql,
+"""SELECT t.id AS id,t.summary AS summary,t.owner AS owner,t.type AS type,t.status AS status,t.priority AS priority,t.milestone AS milestone,t.time AS time,t.changetime AS changetime,priority.value AS priority_value
+FROM ticket AS t
+  LEFT OUTER JOIN enum AS priority ON (priority.type='priority' AND priority.name=priority)
+ORDER BY COALESCE(t.summary,'')='',t.summary,t.id""")
+        self.assertEqual([], args)
+        tickets = query.execute(self.req)
+    
+    def test_default_orders_no_config_no_request(self):
+        self.env.config.remove('query', 'default_order')
+        query = Query(self.env)
+        sql, args = query.get_sql()
+        self.assertEqualSQL(sql,
+"""SELECT t.id AS id,t.summary AS summary,t.owner AS owner,t.type AS type,t.status AS status,t.priority AS priority,t.milestone AS milestone,t.time AS time,t.changetime AS changetime,priority.value AS priority_value
+FROM ticket AS t
+  LEFT OUTER JOIN enum AS priority ON (priority.type='priority' AND priority.name=priority)
+ORDER BY COALESCE(priority.value,'')='',CAST(priority.value AS integer),t.id""")
+        self.assertEqual([], args)
+        tickets = query.execute(self.req)
+        
+    def test_default_orders_with_config_with_request(self):
+        self.env.config.set('query', 'default_order', 'type')
+        query = Query(self.env, order='summary')
+        sql, args = query.get_sql()
+        self.assertEqualSQL(sql,
+"""SELECT t.id AS id,t.summary AS summary,t.owner AS owner,t.type AS type,t.status AS status,t.priority AS priority,t.milestone AS milestone,t.time AS time,t.changetime AS changetime,priority.value AS priority_value
+FROM ticket AS t
+  LEFT OUTER JOIN enum AS priority ON (priority.type='priority' AND priority.name=priority)
+ORDER BY COALESCE(t.summary,'')='',t.summary,t.id""")
+        self.assertEqual([], args)
+        tickets = query.execute(self.req)
+        self.env.config.remove('query', 'default_orders')
+        
+    def test_default_orders_with_config_no_request(self): 
+        self.env.config.set('query', 'default_order', 'time')
+        query = Query(self.env)
+        sql, args = query.get_sql()
+        self.assertEqualSQL(sql,
+"""SELECT t.id AS id,t.summary AS summary,t.owner AS owner,t.type AS type,t.status AS status,t.priority AS priority,t.milestone AS milestone,t.time AS time,t.changetime AS changetime,priority.value AS priority_value
+FROM ticket AS t
+  LEFT OUTER JOIN enum AS priority ON (priority.type='priority' AND priority.name=priority)
+ORDER BY COALESCE(t.time,0)=0,t.time,t.id""")
+        self.assertEqual([], args)
+        tickets = query.execute(self.req)
+        
+        self.env.config.set('query', 'default_order', '-time')
+        query = Query(self.env)
+        sql, args = query.get_sql()
+        self.assertEqualSQL(sql,
+"""SELECT t.id AS id,t.summary AS summary,t.owner AS owner,t.type AS type,t.status AS status,t.priority AS priority,t.milestone AS milestone,t.time AS time,t.changetime AS changetime,priority.value AS priority_value
+FROM ticket AS t
+  LEFT OUTER JOIN enum AS priority ON (priority.type='priority' AND priority.name=priority)
+ORDER BY COALESCE(t.time,0)=0 DESC,t.time DESC,t.id""")
+        self.assertEqual([], args)
+        tickets = query.execute(self.req)
+        self.env.config.remove('query', 'default_order')
+
     def test_default_cols_with_config_no_request(self):
         self.env.config.set('query', 'default_columns', 'id,time,changetime,summary,milestone,type')
         query = Query(self.env)