Source

trac-t10425 / default_group.diff

# HG changeset patch
# Parent 105c55be18a1c33dfad5585ca0f0f69e5b2f3123

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 @@ class Query(object):
         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 @@ class Query(object):
         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 @@ class QueryModule(Component):
     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
@@ -498,6 +498,105 @@ 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):
+        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):
+        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)
+
+    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)
+
+    def test_default_group_with_config_no_request_desc(self):
+        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)
+
+    def test_default_group_invalid_field_1(self):
+        # invalid argument and no config
+        query = Query(self.env, group='foo', groupdesc=1)
+        self.assertEqual(None, query.group)
+        self.assertEqual(0, query.groupdesc)
+
+    def test_default_group_invalid_field_2(self):
+        # valid argument and no config
+        query = Query(self.env, group='status')
+        self.assertEqual('status', query.group)
+        self.assertEqual(0, query.groupdesc)
+
+    def test_default_group_invalid_field_3(self):
+        # no argument and invalid config
+        self.env.config.set('query', 'default_group', '-foo')
+        query = Query(self.env)
+        self.assertEqual(None, query.group)
+        self.assertEqual(0, query.groupdesc)
+
+    def test_default_group_invalid_field_4(self):
+        # invalid argument and invalid config
+        self.env.config.set('query', 'default_group', '-foo')
+        query = Query(self.env, group='bar')
+        self.assertEqual(None, query.group)
+        self.assertEqual(0, query.groupdesc)
+
+    def test_default_group_invalid_field_5(self):
+        # invalid argument but valid config
+        self.env.config.set('query', 'default_group', '-milestone')
+        query = Query(self.env, group='bar')
+        self.assertEqual('milestone', query.group)
+        self.assertEqual(1, query.groupdesc)
+
+    def test_default_group_invalid_field_6(self):
+        # valid argument and valid config
+        self.env.config.set('query', 'default_group', '-milestone')
+        query = Query(self.env, group='type')
+        self.assertEqual('type', query.group)
+        self.assertEqual(0, query.groupdesc)
+
     def test_default_order_no_config_with_request(self):
         query = Query(self.env, order='summary')
         sql, args = query.get_sql()