Changeset 147 for trunk/ticketvalidation
- Timestamp:
- 27/01/10 11:41:03 (7 months ago)
- Location:
- trunk/ticketvalidation/ticketvalidation
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/ticketvalidation/ticketvalidation/admin.py
r144 r147 10 10 from trac.web.chrome import add_script 11 11 12 __all__ = ['TicketValidationAdminPanel'] 13 12 14 13 15 class TicketValidationAdminPanel(Component): 14 """Provides an admin panel for ticket validation config."""16 """Provides an admin panel for configuring ticket validation rules.""" 15 17 16 18 implements(IAdminPanelProvider) -
trunk/ticketvalidation/ticketvalidation/main.py
r146 r147 17 17 from trac.web.chrome import ITemplateProvider 18 18 19 __all__ = ['TicketValidation Plugin']19 __all__ = ['TicketValidationRules'] 20 20 21 21 22 22 class BoolOperand(object): 23 ticket = None 23 24 def __init__(self, t): 24 25 self.args = t[0] … … 26 27 return "(" + " ".join(map(str,self.args)) + ")" 27 28 28 29 29 class BoolEquality(BoolOperand): 30 30 def __nonzero__(self): 31 31 # TODO - implement this 32 return True 33 32 return False 34 33 35 34 class BoolAnd(BoolOperand): 36 35 def __nonzero__(self): 37 # TODO - implement this 36 for a in self.args[0::2]: 37 v = bool(a) 38 if not v: 39 return False 38 40 return True 39 40 41 41 42 class BoolOr(BoolOperand): 42 43 def __nonzero__(self): 43 # TODO - implement this 44 return True 44 for a in self.args[0::2]: 45 v = bool(a) 46 if v: 47 return True 48 return False 45 49 46 50 47 class TicketValidation Plugin(Component):51 class TicketValidationRules(Component): 48 52 """Main component of the ticket validation plug-in. Fetches the validation 49 rules from the config and applies them when the user submits the ticket. If 50 any fields are found to violate the rules, the submission is disallowed and 51 the user is chastised.""" 53 rules from the config, parses them and applies them appropriately.""" 52 54 53 55 implements(ITemplateProvider, ITicketManipulator) … … 59 61 60 62 # grammar definition for parsing rule conditions 61 self._grammar = operatorPrecedence(Word(alphanums + '_') | quotedString ,63 self._grammar = operatorPrecedence(Word(alphanums + '_') | quotedString.setParseAction(removeQuotes), 62 64 [(oneOf('== !='), 2, opAssoc.LEFT, BoolEquality), 63 65 (oneOf('and &&'), 2, opAssoc.LEFT, BoolAnd), … … 73 75 'condition': config.get(name), 74 76 'required': config.get('%s.required' % name).split(), 77 'hidden': config.get('%s.hidden' % name).split(), 75 78 } 76 79 rules.append(rule) … … 113 116 """This API is called by Trac when the user tries to submit a ticket. 114 117 This is where the magic happens for required fields.""" 115 rules = self.get_rules()116 118 problems = [] 117 for r in rules: 119 BoolOperand.ticket = ticket 120 for r in self.get_rules(): 118 121 result = self._grammar.parseString(r['condition'])[0] 119 122 self.log.debug('required field rule "%s": %s is %s' % (r['name'], str(result), bool(result)))