Source

trac-t10425 / default_order.diff

# HG changeset patch
# Parent 8b262cb0aff9d59a6669c9f001a56927184b8610

diff --git a/trac/ticket/query.py b/trac/ticket/query.py
--- a/trac/ticket/query.py
+++ b/trac/ticket/query.py
@@ -125,8 +125,15 @@
         self.cols = [c for c in cols or [] if c in field_names or 
                      c == 'id']
         self.rows = [c for c in rows if c in field_names]
+
+        self.order = self.order or QueryModule(self.env).default_order
+        if self.order:
+            if self.order[0] == '-':
+                (self.order, self.desc) = (self.order[1:], 1)
+            elif self.order[0] == '+':
+                (self.order, self.desc) = (self.order[1:], 0)
         if self.order != 'id' and self.order not in field_names:
-            self.order = 'priority'
+            (self.order, self.desc) = ('priority', 0)
 
         if self.group not in field_names:
             self.group = None
@@ -849,6 +856,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,73 @@
         self.assertEqual(['anonymous'], args)
         tickets = query.execute(self.req)
 
+    def test_default_order_no_config_with_request(self):
+        self.env.config.remove('query', 'default_order')
+        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_order_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_order_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_order_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_order_invalid_field(self):
+        self.env.config.set('query', 'default_order', '-foo')
+        query = Query(self.env)
+        self.assertEquals('priority', query.order)
+        self.assertEquals(0, query.desc)
+
     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)