Modify

Opened 12 years ago

Last modified 12 years ago

#10804 new enhancement

Expand SortMilestoneVersion to cover Custom Query milestone field

Reported by: joshua@… Owned by: Franz
Priority: normal Component: TicketNavPlugin
Severity: normal Keywords: sort milestone
Cc: Trac Release: 1.2

Description

Expand the plugin to cover the custom query milestone field as well. Here are the changes to the SortMilestoneVersion class I made to get this to work:

class SortMilestoneVersion(Component):
    """Sorts drop-down lists of version and milestone regardless of the case and
make milestone a must field, when a default milestone is set.

Default behavior of Trac for sorting milestones is:
inbox, v1, v2, Inbox, V1, V2

This plugin sorts it as following:
inbox, Inbox, v1, V1, v2, V2
"""
    implements (ITemplateStreamFilter)

    #ITemplateStreamFilter
    def filter_stream(self, req, method, filename, stream, data):
        if filename and (filename == 'ticket.html' or filename == 'newticket' or filename == 'query.html'):
#            print "filename '%s' matches" % filename
            if not data:
                return stream
            try:
                fields = data['fields']
                if not fields:
                    return stream
                version = self.get_field_list(fields, 'version')
                version['options'].sort(key=unicode.lower)

                milestones = self.get_field_list(fields, 'milestone')
                if self.config.get('ticket', 'default_milestone'):
                    milestones['optional'] = False

                for opt in milestones['optgroups']:
                    opt['options'].sort(key=unicode.lower)

            except Exception, e:
                self.log.error('error has occured by sorting: %s' % e)
        return stream

    def get_field_list(self, fields, fieldname):
        if not fields or not fieldname:
            return None
        for fld in fields:
            if isinstance(fld, dict):
                if fld['name'] == fieldname:
                    return fld
            elif isinstance(fld, str):
                if fields[fld]['name'] == fieldname:
                    return fields[fld]
            else:
                pass
        return None # return None if fieldname not found

Attachments (0)

Change History (1)

comment:1 Changed 12 years ago by joshua@…

Trac Release: 1.2

Update: minor modification to support 0.12 and 1.0

class SortMilestoneVersion(Component):
    """Sorts drop-down lists of version and milestone regardless of the case and
make milestone a must field, when a default milestone is set.

Default behavior of Trac for sorting milestones is:
{{{
inbox, v1, v2, Inbox, V1, V2
}}}

This plugin sorts it as following:
{{{
inbox, Inbox, v1, V1, v2, V2
}}}
"""
    implements (ITemplateStreamFilter)

    #ITemplateStreamFilter
    def filter_stream(self, req, method, filename, stream, data):
        if filename and (filename == 'ticket.html' or filename == 'newticket' or filename == 'query.html'):
#            print "filename '%s' matches" % filename
            if not data:
                return stream
            try:
                fields = data['fields']
                if not fields:
                    return stream
                version = self.get_field_list(fields, 'version')
                version['options'].sort(key=unicode.lower)

                milestones = self.get_field_list(fields, 'milestone')
                if self.config.get('ticket', 'default_milestone'):
                    milestones['optional'] = False

                if "optgroups" in milestones:
                    for opt in milestones['optgroups']:
                        opt['options'].sort(key=unicode.lower)
                # 0.12 support for milestone field in custom query
                elif "options" in milestones:
                    milestones["options"].sort(key=unicode.lower)

            except Exception, e:
                self.log.error('error has occured by sorting: %s' % e)
        return stream

    def get_field_list(self, fields, fieldname):
        if not fields or not fieldname:
            return None
        for fld in fields:
            if isinstance(fld, dict):
                if fld['name'] == fieldname:
                    return fld
            elif isinstance(fld, str):
                if fields[fld]['name'] == fieldname:
                    return fields[fld]
            else:
                pass
        return None # return None if fieldname not found

Modify Ticket

Change Properties
Set your email in Preferences
Action
as new The owner will remain Franz.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.