0
votes

How can I get a list of ticket fields (like milestone, version, and all the custom fields) in Trac via Python?

In the Trac documentation I have found the TicketSystem class, but it is a component and I cannot instantiate it. So how do I access its methods if I need an instance for it?

Edit: I found out how I can access the default fields. E.g. for milestones, it is model.Milestone.select(self.env). Now the problem is, how can I access custom ticket fields? There must be a way to do it without having to access the database manually, since the method get_custom_fields() in the TicketSystem class exists.

Edit 2: I just found out that get_custom_fields() only returns the available fields, but not their values. What I want to do is get all available values of a specific custom field.

1
Did you tried that one stackoverflow.com/a/9508120/524743 ?Samuel
That gives me the value of a field from ONE ticket. However, I want all values from all Tickets. The custom field has predefined values, accessible via dropdown menu on ticket creation. Now I need to access those values, that would be usually in a dropdown menu, via Python.Raimund Krämer

1 Answers

2
votes

In the Trac documentation I have found the TicketSystem class, but it is a component and I cannot instantiate it.

You can obtain a reference to the Component, which is a singleton, with the statement ts = TicketSystem(self.env).

I just found out that get_custom_fields() only returns the available fields, but not their values. What I want to do is get all available values of a specific custom field.

That is incorrect. You can obtain the possible values of a ticket custom field with the statements:

fields = TicketSystem(self.env).get_custom_fields()
options = fields[idx].get('options', [])

where idx is the index of the field in the list for which you wish to retrieve the options. The list will only be non-empty if field['type'] is select or radio. See trac.ticket.api.TicketSystem.customfields.

I found out how I can access the default fields. E.g. for milestones, it is model.Milestone.select(self.env).

You should access all fields using TicketSystem(self.env).get_ticket_fields(). That statement will return all fields, including custom fields.