Changeset 3818
- Timestamp:
- 06/10/08 15:29:10 (7 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
ticketsubmitpolicyplugin/0.11/ticketsubmitpolicy/ticketsubmitpolicy.py
r3817 r3818 1 1 # Plugin for trac 0.11 2 3 import re 2 4 3 5 from genshi.builder import tag 4 6 from genshi.filters import Transformer 5 6 7 7 8 from trac.core import * … … 9 10 from trac.web import ITemplateStreamFilter 10 11 11 12 def camelCase(string): 13 """returns the camelCase representation of a string""" 14 15 args = string.split() 16 args = [args[0]] + [ i.capitalize() for i in args[1:] ] 17 return ''.join(args) 18 19 20 ### policies 12 21 13 22 class ITicketSubmitPolicy(Interface): … … 47 56 var field=getValue("field-" + requiredfield); 48 57 49 if ( !comparitor(val, value))58 if (comparitor(val, value)) 50 59 { 51 60 … … 69 78 70 79 def onsubmit(self, field, comparitor, value, requiredfield): 71 requires = "requires('%s', %s, '%s', '%s');" % (field, comparitor, value, requiredfield)80 requires = "requires('%s', %s, %s, '%s');" % (field, comparitor, value, requiredfield) 72 81 return requires 73 82 … … 80 89 81 90 def filter_stream(self, stream, field, comparitor, value, excludedfield): 82 exclude = "exclude('%s', %s, '%s', '%s')" % ( field, comparitor, value, excludedfield )91 exclude = "exclude('%s', %s, %s, '%s')" % ( field, comparitor, value, excludedfield ) 83 92 stream |= Transformer("//select[@id='field-%s']" % field).attr('onchange', exclude) 84 93 return stream … … 92 101 if (comparitor(val, value)) 93 102 { 103 element.style.display="none"; 104 } 105 else 106 { 94 107 element.style.display=""; 95 108 } 96 else97 {98 element.style.display="none";99 }100 109 101 110 } … … 103 112 104 113 def onload(self, field, comparitor, value, excludedfield): 105 return "exclude('%s', %s, '%s', '%s');" % ( field, comparitor, value, excludedfield )114 return "exclude('%s', %s, %s, '%s');" % ( field, comparitor, value, excludedfield ) 106 115 107 116 def onsubmit(self, field, comparitor, value, excludedfield): … … 132 141 policies = ExtensionPoint(ITicketSubmitPolicy) 133 142 134 comparitors = {'!=': 'is', 135 '==': 'isNot', 136 'in': 'isIn', 137 'not in': 'isNotIn' } 143 # comparitors = {'!=': 'is', 144 # '==': 'isNot', 145 # 'in': 'isIn', 146 # 'not in': 'isNotIn' } 147 148 comparitors = { 'is': 1, 149 'is not': 1, 150 'is in': 'Array', 151 'is not in': 'Array' } 138 152 139 153 def policy_dict(self): … … 147 161 parse the [ticket-submit-policy] section of the config for policy rules 148 162 """ 163 164 # XXX wtf? 149 165 section = dict([i for i in self.config.options('ticket-submit-policy')]) 166 167 150 168 policies = {} # XXX this should probably be a real class, not an abused dict 151 169 for key in section: … … 160 178 if action == 'condition': 161 179 condition = section[key] 162 for comparitor in self.comparitors: 163 if comparitor in condition: 164 field, value = [i.strip() for i in condition.split(comparitor)] 165 policies[name]['condition'] = dict(field=field,value=value,comparitor=comparitor) 166 break 180 181 # look for longest match to prevent substring matching 182 comparitors = sorted(self.comparitors.keys(), key=lambda x: len(x), reverse=True) 183 match = re.match('.* (%s) .*' % '|'.join(comparitors), condition) 184 if match: 185 comparitor = match.groups()[0] 186 field, value = [i.strip() for i in condition.split(comparitor, 1)] 187 if self.comparitors[comparitor] == 'Array': 188 value = ', '.join(["'%s'" % i.strip() 189 for i in value.split(',')]) 190 value = '[%s]' % value 191 else: 192 value = "'%s'" % value 193 comparitor = camelCase(comparitor) 194 policies[name]['condition'] = dict(field=field,value=value,comparitor=comparitor) 195 167 196 else: 168 197 self.log.error("Invalid condition: %s" % condition) 198 169 199 continue 170 200 … … 199 229 200 230 policies = self.parse() 231 201 232 for key, policy in policies.items(): 202 233 … … 204 235 field = policy['condition']['field'] 205 236 comparitor = policy['condition']['comparitor'] 206 comparitor = self.comparitors[comparitor]207 237 value = policy['condition']['value'] 208 238
