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.

7 comments:

  1. Hi, I copied this code into the templates/ticket.html and where I must call the sorting function when I want to order the fields for create new ticket?
    Thanks
    K

    ReplyDelete
  2. The above code already calls that function for you in the last line.

    The "fields" list comes from the templating engine and then it's sorted according to the order in the "field_types" list.

    ReplyDelete
  3. thanks, I copied, inserted code to the but any changes of the order my fields.

    ReplyDelete
  4. Well, either you didn't define "field_types" properly, or the code is not in the right ticket.html, or the old template is compiled and cached somewhere.

    ReplyDelete
  5. oh.now it's all right.
    Thank you very much

    ReplyDelete
  6. Thank you very much. Your codes are wonderful.

    ReplyDelete
  7. thanks for the good work and to make it available to all.
    I have found out that fields' name have to be all lower case for this to work properly (I have custom fields).

    ReplyDelete