Edgewall Software

Ticket #5641: ITicketCustomFieldValues.patch

File ITicketCustomFieldValues.patch, 2.0 KB (added by Colin Guthrie, 13 months ago)

OK, here is an updated patch that adds a new Interface/ExtensionPoint that means that plugins can provide a list of custom field values to custom fields. The change is quite simple and I think useful. It means plugins can do a lot more with field values. If possible I'd love to see this in 0.11... I'd say this can replace the previous patch as although it's not exactly the same, it's much more flexible.

  • trac/ticket/api.py

     
    132132        ticket. Therefore, a return value of `[]` means everything is OK.""" 
    133133 
    134134 
     135class ITicketCustomFieldValues(Interface): 
     136    """Allow for plugins to provide a list of possible values for custom fields.""" 
     137 
     138    def get_values(): 
     139        """Get a list of possible values for a custom field 
     140         
     141        Must return a list of `(value, display)` tuples, one for each custom 
     142        value. `display` is currently ignored (e.g. just make it `None`) 
     143        but is included for future compatibility. A return value of `[]` 
     144        will suppress the display of the custom field""" 
     145 
     146 
    135147class TicketContext(Context): 
    136148    """Context used for describing Ticket resources.""" 
    137149 
     
    176188        include_missing=False, 
    177189        doc="""Ordered list of workflow controllers to use for ticket actions 
    178190            (''since 0.11'').""") 
     191    custom_values = ExtensionPoint(ITicketCustomFieldValues) 
    179192 
    180193    restrict_owner = BoolOption('ticket', 'restrict_owner', 'false', 
    181194        """Make the owner field of tickets use a drop-down menu. See 
     
    294307            } 
    295308            if field['type'] == 'select' or field['type'] == 'radio': 
    296309                field['options'] = config.getlist(name + '.options', sep='|') 
     310                if len(field['options']) == 1 and field['options'][0].startswith('custom:'): 
     311                    custom_name = field['options'][0].split(':')[1] 
     312                    field['options'].pop() 
     313                    for custom_value in self.custom_values: 
     314                        if custom_value.__class__.__name__ == custom_name: 
     315                            for value,display in custom_value.get_values(): 
     316                                field['options'].append(value) 
     317                            break 
    297318                if '' in field['options']: 
    298319                    field['optional'] = True 
    299320                    field['options'].remove('')