1. Odd Simon Simonsen
  2. trac-t10425

Source

trac-t10425 / default_group.diff

# HG changeset patch
# Parent 51c05f4f7418051ab610b2b6c1538cfb901de235

diff --git a/trac/ticket/query.py b/trac/ticket/query.py
--- a/trac/ticket/query.py
+++ b/trac/ticket/query.py
@@ -134,8 +134,15 @@
             else:
                 (self.order, self.desc) = (default_order, self.desc or 0)
 
-        if self.group not in field_names:
-            self.group = None
+        if not self.group or self.group not in field_names:
+            default_group = QueryModule(self.env).default_group or None
+            if default_group:
+                if default_group[0] == '-':
+                    (self.group, self.groupdesc) = (default_group[1:], 1)
+                elif default_group[0] == '+':
+                    (self.group, self.groupdesc) = (default_group[1:], 0)
+                else:
+                    self.group = default_group
 
         constraint_cols = {}
         for clause in self.constraints:
@@ -416,7 +423,7 @@
         return href.query(constraints,
                           report=id,
                           order=order, desc=1 if desc else None,
-                          group=self.group or None,
+                          group=self.group,
                           groupdesc=1 if self.groupdesc else None,
                           col=cols,
                           row=self.rows,
@@ -859,6 +866,10 @@
     default_order = Option('query', 'default_order',
         default='priority',
         doc="""The default order to use for queries. (''since 0.13'')""")
+    
+    default_group = Option('query', 'default_group',
+        default=None,
+        doc="""The default group 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
@@ -497,6 +497,68 @@
 ORDER BY COALESCE(t.id,0)=0,t.id""")
         self.assertEqual(['anonymous'], args)
         tickets = query.execute(self.req)
+        
+    def test_default_group_no_config_with_request(self):
+        self.env.config.remove('query', 'default_group')
+        query = Query(self.env, group='summary')
+        sql, args = query.get_sql()
+        self.assertEqualSQL(sql,
+"""SELECT t.id AS id,t.owner AS owner,t.type AS type,t.status AS status,t.priority AS priority,t.milestone AS milestone,t.summary AS summary,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,COALESCE(priority.value,'')='',CAST(priority.value AS integer),t.id""")
+        self.assertEqual([], args)
+        tickets = query.execute(self.req)
+    
+    def test_default_group_no_config_no_request(self):
+        self.env.config.remove('query', 'default_group')
+        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_group_with_config_with_request(self):
+        self.env.config.set('query', 'default_group', 'type')
+        query = Query(self.env, order='component', group='milestone')
+        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.component AS component,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)
+  LEFT OUTER JOIN milestone ON (milestone.name=milestone)
+ORDER BY COALESCE(t.milestone,'')='',COALESCE(milestone.completed,0)=0,milestone.completed,COALESCE(milestone.due,0)=0,milestone.due,t.milestone,COALESCE(t.component,'')='',t.component,t.id""")
+        self.assertEqual([], args)
+        tickets = query.execute(self.req)
+        self.env.config.remove('query', 'default_group')
+        
+    def test_default_group_with_config_no_request(self): 
+        self.env.config.set('query', 'default_group', 'status')
+        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.priority AS priority,t.milestone AS milestone,t.status AS status,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.status,'')='',t.status,COALESCE(priority.value,'')='',CAST(priority.value AS integer),t.id""")
+        self.assertEqual([], args)
+        tickets = query.execute(self.req)
+        
+        self.env.config.set('query', 'default_group', '-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,COALESCE(priority.value,'')='',CAST(priority.value AS integer),t.id""")
+        self.assertEqual([], args)
+        tickets = query.execute(self.req)
+        self.env.config.remove('query', 'default_group')
 
     def test_default_orders_no_config_with_request(self):
         self.env.config.remove('query', 'default_orders')