Commits

Azoff committed 8bcff21

Clean up code a bit.
Added another testcase for Query.get_href().

Comments (0)

Files changed (4)

default_columns.diff

          return cols
  
      def count(self, req=None, db=None, cached_ids=None, authname=None,
-@@ -838,6 +845,11 @@ class QueryModule(Component):
+@@ -838,6 +845,12 @@ class QueryModule(Component):
          """Number of tickets displayed per page in ticket queries,
          by default (''since 0.11'')""")
  
 +    default_columns = ListOption('query', 'default_columns',
 +        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 is to let Trac calculate this internally.
++            (''since 0.13'')""")
 +
      # IContentConverter methods
  

default_group.diff

 # HG changeset patch
-# Parent 105c55be18a1c33dfad5585ca0f0f69e5b2f3123
+# Parent 24d2d98828269b69188559955bd697fc65890b1f
 
 diff --git a/trac/ticket/query.py b/trac/ticket/query.py
 --- a/trac/ticket/query.py
                            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',
+@@ -862,6 +868,10 @@ class QueryModule(Component):
          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
  
+     def get_supported_conversions(self):
 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

default_order.diff

 # HG changeset patch
-# Parent 4c77b779c8f5496ef02c25acd502cf875c09d0b6
+# Parent dc5fcae1dac41c1fb58bf5055cd350587a79c82f
 
 diff --git a/trac/ticket/query.py b/trac/ticket/query.py
 --- a/trac/ticket/query.py
  
          if self.group not in field_names:
              self.group = None
-@@ -849,6 +856,10 @@ class QueryModule(Component):
-         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'')""")
-+    
+@@ -851,6 +858,10 @@ class QueryModule(Component):
+             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
  
+     def get_supported_conversions(self):
 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
 # HG changeset patch
-# Parent 31006e174037e86e96401c43fff04af147e86cd2
+# Parent 80f408330393ca0b84d685e4a6ed7ee0dc1228fa
 
 diff --git a/trac/ticket/query.py b/trac/ticket/query.py
 --- a/trac/ticket/query.py
 -                (self.order, self.desc) = (self.order[1:], 0)
 -        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, self.groupdesc) = (None, 0)
 +        self.order = []
 +        self.group = False
 +        group = group or QueryModule(self.env).default_group
 +            else:
 +                order = [order]
 +        valid_order_fields = field_names.union(('id',))
-+        
+ 
+-        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, self.groupdesc) = (None, 0)
 +        def _validate_order_list(l):
 +            return [o for o in l or [] if \
 +                (o[0] in ('-', '+') and o[1:] in valid_order_fields) \
 +            if self.group and index == 0:
 +                if order_col in cols:
 +                    cols.remove(order_col)
-+            elif not order_col in cols:
++            elif order_col not in cols:
 +                cols.append(order_col)
  
          return cols
 +        if self.group:
 +            return self.get_order()[0] # Group is first order item
 +        return (None, None)
-+    
++
      def count(self, req=None, db=None, cached_ids=None, authname=None,
                tzinfo=None, locale=None):
          """Get the number of matching tickets for the present query.
              sql.append("\n  LEFT OUTER JOIN %s ON (%s.name=%s)"
                         % (col, col, col))
  
-@@ -641,11 +672,10 @@ class Query(object):
+@@ -639,13 +670,12 @@ class Query(object):
+                 sql.append(" OR ")
+                 sql.append("id in (%s)" %
                             (','.join([str(id) for id in cached_ids])))
-             
+-            
++
          sql.append("\nORDER BY ")
 -        order_cols = [(self.order, self.desc)]
 -        if self.group and self.group != self.order:
 -            order_cols.insert(0, (self.group, self.groupdesc))
--
--        for name, desc in order_cols:
 +        for index, (name, desc) in enumerate(self.get_order()):
 +            if self.group and index != 0 and name == self.get_group()[0]:
 +                continue
-+                
+ 
+-        for name, desc in order_cols:
              if name in enum_columns:
                  col = name + '.value'
              elif name in custom_fields:
 +                if field == col:
 +                    return '-' + field if not desc else field
 +            return col
-+                    
++
          headers = [{
              'name': col, 'label': labels.get(col, _('Ticket')),
              'wikify': col in wikify,
                  groups.setdefault(group_key, []).append(ticket)
                  if not groupsequence or group_key not in groupsequence:
                      groupsequence.append(group_key)
-@@ -861,15 +898,19 @@ class QueryModule(Component):
-     default_columns = ListOption('query', 'default_columns',
-         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 is to let Trac calculate this internally.
-+            (''since 0.13'')""")
-     
--    default_order = Option('query', 'default_order',
-+    default_order = ListOption('query', 'default_order',
+@@ -866,11 +903,14 @@ class QueryModule(Component):
+ 
+     default_order = Option('query', 'default_order',
          default='priority',
 -        doc="""The default order to use for queries. (''since 0.13'')""")
 +        doc="""The default order to use for queries. Use `-field` for
 +            descending sort order. Multiple order fields supported.
 +            (''since 0.13'')""")
-     
+ 
      default_group = Option('query', 'default_group',
          default=None,
 -        doc="""The default group to use for queries. (''since 0.13'')""")
  
      # IContentConverter methods
  
-@@ -932,10 +973,6 @@ class QueryModule(Component):
+@@ -933,10 +973,6 @@ class QueryModule(Component):
                  query = Query.from_string(self.env, qstring)
                  args = {'order': query.order, 'group': query.group,
                          'col': query.cols, 'max': query.max}
                  constraints = query.constraints
  
              # Substitute $USER, or ensure no field constraints that depend
-@@ -949,6 +986,18 @@ class QueryModule(Component):
+@@ -950,6 +986,18 @@ class QueryModule(Component):
                              del clause[field]
                              break
  
 +            desc = True if args.get(_desc) else False
 +            if field and desc and not field[0] in ['-', '+']:
 +                args[_field] = '-' + field
-+                
++
 +        order = args.get('order')
 +        if isinstance(order, basestring):
 +            order = order.split('|')
          cols = args.get('col')
          if isinstance(cols, basestring):
              cols = [cols]
-@@ -963,13 +1012,11 @@ class QueryModule(Component):
+@@ -964,13 +1012,11 @@ class QueryModule(Component):
          max = args.get('max')
          if max is None and format in ('csv', 'tab'):
              max = 0 # unlimited unless specified explicitly
  
          if 'update' in req.args:
              # Reset session vars
-@@ -1423,19 +1470,21 @@ class TicketQueryMacro(WikiMacroBase):
+@@ -1424,19 +1470,21 @@ class TicketQueryMacro(WikiMacroBase):
  
          def ticket_groups():
              groups = []
                  href = q.get_href(formatter.context)
                  groups.append((v, [t for t in g], href, title))
              return groups
-@@ -1456,7 +1505,7 @@ class TicketQueryMacro(WikiMacroBase):
+@@ -1457,7 +1505,7 @@ class TicketQueryMacro(WikiMacroBase):
                      [(tag.p(tag_('%(groupvalue)s %(groupname)s tickets:',
                                   groupvalue=tag.a(v, href=href, class_='query',
                                                    title=title),
  
      def test_default_cols_with_config_no_request(self):
          self.env.config.set('query', 'default_columns', 'id,time,changetime,summary,milestone,type')
-@@ -741,6 +747,49 @@ ORDER BY COALESCE(priority.value,'')='',
+@@ -741,6 +747,53 @@ ORDER BY COALESCE(priority.value,'')='',
          self.assertEqual([], args)
          tickets = query.execute(self.req)
  
 +        self.assertEqual(['-milestone', '-priority', 'id'], query.order)
 +        self.assertEqual([('milestone', True), ('priority', True),
 +                          ('id', False)], query.get_order())
-+    
++
 +    def test_get_group(self):
 +        query = Query(self.env, group='-milestone', order=['-priority', 'id'])
 +        self.assertEqual(True, query.group)
 +        self.assertEqual(('milestone', True), query.get_group())
-+        
++
 +    def test_get_href_1(self):
 +        query = Query(self.env, group='milestone', order='type')
 +        self.assertEqual('foo/query?group=milestone&order=type', query.get_href(Href('foo')))
-+        
++
 +    def test_get_href_2(self):
++        query = Query(self.env, group='milestone', order='type', desc=1)
++        self.assertEqual('foo/query?group=milestone&order=-type', query.get_href(Href('foo')))
++
++    def test_get_href_3(self):
 +        query = Query(self.env, group='milestone', order=['type', '-id'])
 +        self.assertEqual('/query?group=milestone&order=type%7C-id', query.get_href(Href('')))
-+        
-+    def test_get_href_3(self):
++
++    def test_get_href_4(self):
 +        query = Query(self.env, group='milestone', order=['-type', '-time'])
 +        self.assertEqual('/query?group=milestone&order=foo', query.get_href(Href(''), order='foo'))
-+        
-+    def test_get_href_4(self):
++
++    def test_get_href_5(self):
 +        query = Query(self.env, group='milestone', order=['-type', '-time'])
 +        self.assertEqual('/query?group=milestone&order=-foo', query.get_href(Href(''), order=['-foo']))
-+        
-+    def test_get_href_5(self):
++
++    def test_get_href_6(self):
 +        query = Query(self.env, group='milestone', order=['-type', '-time'])
 +        self.assertEqual('/query?group=milestone&order=-foo', query.get_href(Href(''), order='-foo'))
-+        
-+    def test_get_href_6(self):
++
++    def test_get_href_7(self):
 +        query = Query(self.env, group='milestone', order=['-type', '-time'])
 +        self.assertEqual('/query?group=milestone&order=-foo', query.get_href(Href(''), order='foo', desc=1))
-+        
-+    def test_get_href_7(self):
++
++    def test_get_href_8(self):
 +        query = Query(self.env, group='milestone', order=['-type', '-time'])
 +        self.assertEqual('/query?group=milestone&order=foo%7C-bar', query.get_href(Href(''), order=['foo', '-bar'], desc=1))
-+        
-+    def test_get_href_8(self):
++
++    def test_get_href_9(self):
 +        query = Query(self.env, group='milestone', cols=['id', 'type', 'time'])
 +        self.assertEqual('/query?group=milestone&order=priority&col=id&col=type&col=time', query.get_href(Href('')))
 +