Edgewall Software

Ticket #10178: trac_ordering.2.patch

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

Updated patch

  • (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 order is not None and type(order) != type([]):
     74          order = [order]
     75        if desc is not None and type(desc) != type([]):
     76          desc = [desc]
     77
     78        self.order = [synonyms.get(o, o) for o in order]     # 0.11 compatibility
    7479        self.desc = desc
    7580        self.group = group
    7681        self.groupdesc = groupdesc
     
    121126        self.cols = [c for c in cols or [] if c in field_names or
    122127                     c == 'id']
    123128        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'
     129        if 'id' not in self.order and not (set(self.order) & set(field_names)):
     130            self.order = ['priority']
    126131
    127132        if self.group not in field_names:
    128133            self.group = None
     
    138143   
    139144    @classmethod
    140145    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']
     146        def as_str(s):
     147            if isinstance(s, unicode):
     148                return s.encode('utf-8')
     149            return s
     150
     151        kw_strs = ['group', 'page', 'max', 'format']
     152        kw_bools = ['groupdesc', 'verbose']
     153        kw_arys_str = ['order', 'rows']
     154        kw_arys_bool = ['desc']
    144155        kw_synonyms = {'row': 'rows'}
    145156        # i18n TODO - keys will be unicode
    146157        synonyms = TicketSystem(env).get_field_synonyms()
    147158        constraints = [{}]
    148159        cols = []
    149160        report = None
    150         def as_str(s):
    151             if isinstance(s, unicode):
    152                 return s.encode('utf-8')
    153             return s
    154161        for filter_ in cls._clause_splitter.split(string):
    155162            if filter_ == 'or':
    156163                constraints.append({})
     
    177184                                for val in cls._item_splitter.split(values)]
    178185            if field in kw_strs:
    179186                kw[as_str(field)] = processed_values[0]
    180             elif field in kw_arys:
     187            elif field in kw_arys_str:
    181188                kw.setdefault(as_str(field), []).extend(processed_values)
     189            elif field in kw_arys_bool:
     190                kw.setdefault(as_str(field), []).extend([as_bool(p) for p in processed_values])
    182191            elif field in kw_bools:
    183192                kw[as_str(field)] = as_bool(processed_values[0])
    184193            elif field == 'col':
     
    254263
    255264        # Only display the first seven columns by default
    256265        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
     266        # Make sure the columns we order by are visible, if they don't also
     267        # overlap the column we group by
     268        if not (set(self.order) & set(cols)) and not self.group in self.order:
     269            cols = [c for c in set(cols) | set(self.order)]
    261270        return cols
    262271
    263272    def count(self, req=None, db=None, cached_ids=None, authname=None,
     
    440449            add_cols(self.group)
    441450        if self.rows:
    442451            add_cols('reporter', *self.rows)
    443         add_cols('status', 'priority', 'time', 'changetime', self.order)
     452
     453        add_cols('status', 'priority', 'time', 'changetime', *self.order)
    444454        cols.extend([c for c in self.constraint_cols if not c in cols])
    445455
    446456        custom_fields = [f['name'] for f in self.fields if 'custom' in f]
     
    461471
    462472        # Join with the enum table for proper sorting
    463473        for col in [c for c in enum_columns
    464                     if c == self.order or c == self.group or c == 'priority']:
     474                    if c in self.order or c == self.group or c == 'priority']:
    465475            sql.append("\n  LEFT OUTER JOIN enum AS %s ON "
    466476                       "(%s.type='%s' AND %s.name=%s)"
    467477                       % (col, col, col, col, col))
    468478
    469479        # Join with the version/milestone tables for proper sorting
    470480        for col in [c for c in ['milestone', 'version']
    471                     if c == self.order or c == self.group]:
     481                    if c in self.order or c == self.group]:
    472482            sql.append("\n  LEFT OUTER JOIN %s ON (%s.name=%s)"
    473483                       % (col, col, col))
    474484
     
    619629                           (','.join([str(id) for id in cached_ids])))
    620630           
    621631        sql.append("\nORDER BY ")
    622         order_cols = [(self.order, self.desc)]
    623         if self.group and self.group != self.order:
     632        desc = self.desc + [0 for c in range(len(self.order) - len(self.desc)) if self.order > self.desc]
     633        order_cols = [(col, dsc) for col,dsc in zip(self.order, desc)]
     634        if self.group and self.group not in self.order:
    624635            order_cols.insert(0, (self.group, self.groupdesc))
    625636
     637        count = 0 
    626638        for name, desc in order_cols:
     639            count += 1
    627640            if name in enum_columns:
    628641                col = name + '.value'
    629642            elif name in custom_fields:
     
    652665                           % (desc, desc, col, desc))
    653666            else:
    654667                sql.append("%s%s" % (col, desc))
    655             if name == self.group and not name == self.order:
     668            if count != len(order_cols):
    656669                sql.append(",")
    657         if self.order != 'id':
     670        if 'id' not in self.order:
    658671            sql.append(",t.id") 
    659672
    660673        if errors:
    661674            raise QueryValueError(errors)
     675
    662676        return "".join(sql), args
    663677
    664678    @staticmethod
     
    715729            'name': col, 'label': labels.get(col, _('Ticket')),
    716730            'wikify': col in wikify,
    717731            'href': self.get_href(context.href, order=col,
    718                                   desc=(col == self.order and not self.desc))
     732                                  desc=(col in self.order and not self.desc))
    719733        } for col in cols]
    720734
    721735        fields = {'id': {'type': 'id', 'label': _("Ticket")}}
     
    777791
    778792        pagedata = []
    779793        shown_pages = results.get_shown_pages(21)
    780         for page in shown_pages:
     794        for page in shown_pages:
    781795            pagedata.append([self.get_href(context.href, page=page), None,
    782                             str(page), _('Page %(num)d', num=page)])
     796                                str(page), _('Page %(num)d', num=page)])
    783797
    784798        results.shown_pages = [dict(zip(['href', 'class', 'string', 'title'],
    785799                                        p)) for p in pagedata]