Monday, December 21, 2009

Arrange Fields in Trac Tickets

I was struggling one day to organize fields in the order I want when I create tickets in Trac.

  1. Copy templates/ticket.html into trac/myproject/templates/ticket.html

  2. Insert the following code snippet to the beginning of the template:

  3. <?python
    # define the order
    field_types = ["type", "priority", "milestone", "keywords", "cc", "component"]

    # Sorting function
    def sort_nicely(field1, field2):
    try:
    idx1 = field_types.index(field1['name'])
    except ValueError:
    idx1 = 1000 # no match, push to the end

    try:
    idx2 = field_types.index(field2['name'])
    except ValueError:
    idx2 = 1000 # no match, push to the end

    return cmp(idx1, idx2)

    fields.sort(cmp=sort_nicely)
    ?>


That's it, if you have any custom fields and you want them ordered, put them into the field_types list.