Source

trac-t10425 / default_group.diff

Full commit
# HG changeset patch
# Parent 6216bce8ce8484e5d4f79bbb436e5f94d5d1f53c

diff --git a/trac/ticket/query.py b/trac/ticket/query.py
--- a/trac/ticket/query.py
+++ b/trac/ticket/query.py
@@ -135,8 +135,14 @@
         if self.order != 'id' and self.order not in field_names:
             (self.order, self.desc) = ('priority', 0)
 
+        self.group = self.group or QueryModule(self.env).default_group
+        if self.group:
+            if self.group[0] == '-':
+                (self.group, self.groupdesc) = (self.group[1:], 1)
+            elif self.group[0] == '+':
+                (self.group, self.groupdesc) = (self.group[1:], 0)
         if self.group not in field_names:
-            self.group = None
+            (self.group, self.groupdesc) = (None, 0)
 
         constraint_cols = {}
         for clause in self.constraints:
@@ -417,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,
@@ -860,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,74 @@
 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_group_invalid_field(self):
+        self.env.config.set('query', 'default_group', '-foo')
+        query = Query(self.env)
+        self.assertEquals(None, query.group)
+        self.assertEquals(0, query.groupdesc)
 
     def test_default_order_no_config_with_request(self):
         self.env.config.remove('query', 'default_order')