Edgewall Software

Ticket #10178: trac_ordering.3.patch

File trac_ordering.3.patch, 6.0 KB (added by david@…, 6 years ago)

3rd Revision

  • (a) query.py.orig vs. (b) /usr/local/lib/python2.6/dist-packages/Trac-0.12.2-py2.6.egg/trac/ticket/query.py

    a b  
    7070            constraints = [constraints]
    7171        self.constraints = constraints
    7272        synonyms = TicketSystem(self.env).get_field_synonyms()
    73         self.order = synonyms.get(order, order)     # 0.11 compatibility
     73        if not isinstance(order, list):
     74            if order is None:
     75                order = []
     76            else:
     77                order = [order]
     78        if not isinstance(desc, list):
     79            if desc is None:
     80                desc = []
     81            else:
     82                desc = [desc]
     83
     84        self.order = [synonyms.get(o, o) for o in order]     # 0.11 compatibility
    7485        self.desc = desc
    7586        self.group = group
    7687        self.groupdesc = groupdesc
     
    121132        self.cols = [c for c in cols or [] if c in field_names or
    122133                     c == 'id']
    123134        self.rows = [c for c in rows if c in field_names]
    124         if self.order != 'id' and self.order not in field_names:
    125             self.order = 'priority'
     135        if 'id' not in self.order and not (set(self.order) & set(field_names)):
     136            self.order = ['priority']
    126137
    127138        if self.group not in field_names:
    128139            self.group = None
     
    138149   
    139150    @classmethod
    140151    def from_string(cls, env, string, **kw):
    141         kw_strs = ['order', 'group', 'page', 'max', 'format']
    142         kw_arys = ['rows']
    143         kw_bools = ['desc', 'groupdesc', 'verbose']
     152        kw_strs = ['group', 'page', 'max', 'format']
     153        kw_bools = ['groupdesc', 'verbose']
     154        kw_arys_str = ['order', 'rows']
     155        kw_arys_bool = ['desc']
    144156        kw_synonyms = {'row': 'rows'}
    145157        # i18n TODO - keys will be unicode
    146158        synonyms = TicketSystem(env).get_field_synonyms()
     
    177189                                for val in cls._item_splitter.split(values)]
    178190            if field in kw_strs:
    179191                kw[as_str(field)] = processed_values[0]
    180             elif field in kw_arys:
     192            elif field in kw_arys_str:
    181193                kw.setdefault(as_str(field), []).extend(processed_values)
     194            elif field in kw_arys_bool:
     195                kw.setdefault(as_str(field), []).extend([as_bool(p) for p in processed_values])
    182196            elif field in kw_bools:
    183197                kw[as_str(field)] = as_bool(processed_values[0])
    184198            elif field == 'col':
     
    254268
    255269        # Only display the first seven columns by default
    256270        cols = cols[:7]
    257         # Make sure the column we order by is visible, if it isn't also
    258         # the column we group by
    259         if not self.order in cols and not self.order == self.group:
    260             cols[-1] = self.order
     271        # Make sure the columns we order by are visible, if they don't also
     272        # overlap the column we group by
     273        if not (set(self.order) & set(cols)) and not self.group in self.order:
     274            cols = [c for c in set(cols) | set(self.order)]
    261275        return cols
    262276
    263277    def count(self, req=None, db=None, cached_ids=None, authname=None,
     
    440454            add_cols(self.group)
    441455        if self.rows:
    442456            add_cols('reporter', *self.rows)
    443         add_cols('status', 'priority', 'time', 'changetime', self.order)
     457
     458        add_cols('status', 'priority', 'time', 'changetime', *self.order)
    444459        cols.extend([c for c in self.constraint_cols if not c in cols])
    445460
    446461        custom_fields = [f['name'] for f in self.fields if 'custom' in f]
     
    461476
    462477        # Join with the enum table for proper sorting
    463478        for col in [c for c in enum_columns
    464                     if c == self.order or c == self.group or c == 'priority']:
     479                    if c in self.order or c == self.group or c == 'priority']:
    465480            sql.append("\n  LEFT OUTER JOIN enum AS %s ON "
    466481                       "(%s.type='%s' AND %s.name=%s)"
    467482                       % (col, col, col, col, col))
    468483
    469484        # Join with the version/milestone tables for proper sorting
    470485        for col in [c for c in ['milestone', 'version']
    471                     if c == self.order or c == self.group]:
     486                    if c in self.order or c == self.group]:
    472487            sql.append("\n  LEFT OUTER JOIN %s ON (%s.name=%s)"
    473488                       % (col, col, col))
    474489
     
    619634                           (','.join([str(id) for id in cached_ids])))
    620635           
    621636        sql.append("\nORDER BY ")
    622         order_cols = [(self.order, self.desc)]
    623         if self.group and self.group != self.order:
     637        desc = self.desc + [
     638            0 for c in range(len(self.order) - len(self.desc))
     639            if len(self.order) > len(self.desc)
     640        ]
     641        order_cols = [(col, dsc) for col,dsc in zip(self.order, desc)]
     642        if self.group and self.group not in self.order:
    624643            order_cols.insert(0, (self.group, self.groupdesc))
    625644
     645        count = 0 
    626646        for name, desc in order_cols:
     647            count += 1
    627648            if name in enum_columns:
    628649                col = name + '.value'
    629650            elif name in custom_fields:
     
    652673                           % (desc, desc, col, desc))
    653674            else:
    654675                sql.append("%s%s" % (col, desc))
    655             if name == self.group and not name == self.order:
     676            if count != len(order_cols):
    656677                sql.append(",")
    657         if self.order != 'id':
     678        if 'id' not in self.order:
    658679            sql.append(",t.id") 
    659680
    660681        if errors:
    661682            raise QueryValueError(errors)
     683
    662684        return "".join(sql), args
    663685
    664686    @staticmethod
     
    715737            'name': col, 'label': labels.get(col, _('Ticket')),
    716738            'wikify': col in wikify,
    717739            'href': self.get_href(context.href, order=col,
    718                                   desc=(col == self.order and not self.desc))
     740                                  desc=(col in self.order and not self.desc))
    719741        } for col in cols]
    720742
    721743        fields = {'id': {'type': 'id', 'label': _("Ticket")}}