| 1 | #------------------------------------------------------------------------------ |
|---|
| 2 | # $Id$ |
|---|
| 3 | # $URL$ |
|---|
| 4 | # Copyright (c) 2006, Ilias Lazaridis - All Rights Reserved |
|---|
| 5 | #------------------------------------------------------------------------------ |
|---|
| 6 | |
|---|
| 7 | from trac.core import * |
|---|
| 8 | from trac.env import IEnvironmentSetupParticipant |
|---|
| 9 | from trac.ticket.query import Query |
|---|
| 10 | |
|---|
| 11 | #------------------------------------------------------------------------------ |
|---|
| 12 | |
|---|
| 13 | class QueryFieldsets(Component): |
|---|
| 14 | |
|---|
| 15 | implements(IEnvironmentSetupParticipant) |
|---|
| 16 | |
|---|
| 17 | def get_fieldsets(self): |
|---|
| 18 | """ (dev-status: beta) returns a dictionary of user-defined fieldsets |
|---|
| 19 | |
|---|
| 20 | Those fieldsets are defined in trac-ini within the section |
|---|
| 21 | |
|---|
| 22 | [ticket-query-fieldsets] |
|---|
| 23 | fieldsetname=id,summary,owner,changetime |
|---|
| 24 | anothersetname=id,summary,time |
|---|
| 25 | default=id,summary |
|---|
| 26 | |
|---|
| 27 | specify a query parameter &fieldset=fieldsetname |
|---|
| 28 | |
|---|
| 29 | if fieldset is ommited, the default fieldset is used |
|---|
| 30 | if default is unspecified, the standard-trac fieldset is used |
|---|
| 31 | |
|---|
| 32 | """ |
|---|
| 33 | |
|---|
| 34 | config = self.config['ticket-query-fieldsets'] |
|---|
| 35 | |
|---|
| 36 | fieldsets = {} |
|---|
| 37 | for option, value in config.options(): |
|---|
| 38 | fieldsets[option] = config.getlist(option, sep=',') |
|---|
| 39 | |
|---|
| 40 | return fieldsets |
|---|
| 41 | |
|---|
| 42 | def __init__(self): |
|---|
| 43 | # on Component activation, override existent method with new one |
|---|
| 44 | get_default_columns_patch() |
|---|
| 45 | from_string_patch() |
|---|
| 46 | pass |
|---|
| 47 | |
|---|
| 48 | #TODO trac:#4190 - replace dummies to force component initialization |
|---|
| 49 | # IEnvironmentSetupParticipant |
|---|
| 50 | def environment_created(self): pass |
|---|
| 51 | def environment_needs_upgrade(self, db): return False |
|---|
| 52 | def upgrade_environment(self, db): pass |
|---|
| 53 | |
|---|
| 54 | #------------------------------------------------------------------------------ |
|---|
| 55 | |
|---|
| 56 | get_default_columns_old = Query.get_default_columns |
|---|
| 57 | |
|---|
| 58 | def get_default_columns_new(self, *args): |
|---|
| 59 | """ Overrides trac.ticket.query.Query.get_default_columns |
|---|
| 60 | """ |
|---|
| 61 | |
|---|
| 62 | # call original function |
|---|
| 63 | cols = get_default_columns_old(self, *args) |
|---|
| 64 | |
|---|
| 65 | # retrieve user defined fieldsets |
|---|
| 66 | fieldsets = QueryFieldsets(self.env).get_fieldsets() |
|---|
| 67 | |
|---|
| 68 | if hasattr(self, 'fieldset'): |
|---|
| 69 | #if self.fieldset: |
|---|
| 70 | if fieldsets.has_key(self.fieldset): |
|---|
| 71 | cols = fieldsets[self.fieldset] |
|---|
| 72 | else: |
|---|
| 73 | self.env.log.debug('fieldset %s not defined, please check trac.ini', self.fieldset) |
|---|
| 74 | else: |
|---|
| 75 | if fieldsets.has_key('default'): |
|---|
| 76 | cols = fieldsets['default'] |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | return cols |
|---|
| 80 | |
|---|
| 81 | def get_default_columns_patch(): |
|---|
| 82 | if Query.get_default_columns != get_default_columns_new: |
|---|
| 83 | get_default_columns_old = Query.get_default_columns |
|---|
| 84 | Query.get_default_columns = get_default_columns_new |
|---|
| 85 | |
|---|
| 86 | #------------------------------------------------------------------------------ |
|---|
| 87 | |
|---|
| 88 | from_string_old = Query.from_string |
|---|
| 89 | |
|---|
| 90 | def from_string_new(cls,env, string, **kw): |
|---|
| 91 | """ Overrides the default behaviour of trac.ticket.query.Query |
|---|
| 92 | """ |
|---|
| 93 | |
|---|
| 94 | # call original method to instantiate a query object |
|---|
| 95 | query = from_string_old(env, string, **kw) |
|---|
| 96 | |
|---|
| 97 | # retrieve the fieldset parameter from the query-string |
|---|
| 98 | fieldset = '' |
|---|
| 99 | filters = string.split('&') |
|---|
| 100 | for keyval in filters: |
|---|
| 101 | param, value = keyval.split('=') |
|---|
| 102 | param = str(param) |
|---|
| 103 | if param == 'fieldset': |
|---|
| 104 | fieldset = value |
|---|
| 105 | |
|---|
| 106 | # set the fieldset attribute on the query-object |
|---|
| 107 | setattr(query, 'fieldset', fieldset) |
|---|
| 108 | |
|---|
| 109 | #remove the fieldset which has gone into the contraints |
|---|
| 110 | query.constraints.pop('fieldset', '') |
|---|
| 111 | |
|---|
| 112 | return query |
|---|
| 113 | |
|---|
| 114 | def from_string_patch(): |
|---|
| 115 | if Query.from_string != from_string_new: |
|---|
| 116 | from_string_old = Query.from_string |
|---|
| 117 | Query.from_string = classmethod(from_string_new ) |
|---|