| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Copyright (C) 2013, 2019 MATOBA Akihiro <matobaa+trac-hacks@gmail.com> |
|---|
| 5 | # All rights reserved. |
|---|
| 6 | # |
|---|
| 7 | # This software is licensed as described in the file COPYING, which |
|---|
| 8 | # you should have received as part of this distribution. |
|---|
| 9 | |
|---|
| 10 | from trac.core import implements, Component |
|---|
| 11 | from trac.ticket.default_workflow import get_workflow_config |
|---|
| 12 | from trac.web.api import IRequestFilter, IRequestHandler |
|---|
| 13 | from trac.web.chrome import ITemplateProvider, add_script, add_script_data |
|---|
| 14 | from pkg_resources import resource_filename |
|---|
| 15 | import json |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | class TicketValidatorDecolator(Component): |
|---|
| 19 | implements(ITemplateProvider, IRequestFilter, IRequestHandler) |
|---|
| 20 | |
|---|
| 21 | def __init__(self): |
|---|
| 22 | self.rules = self._get_validation_rules() |
|---|
| 23 | self.workflow = get_workflow_config(self.config) |
|---|
| 24 | |
|---|
| 25 | def _get_validation_rules(self): |
|---|
| 26 | sentinel = 'sentinel' # something that isn't None |
|---|
| 27 | conditionkeys = ['status', 'type'] |
|---|
| 28 | conditionkeys.sort() |
|---|
| 29 | rules = {'status=new&type=*': {'summary': sentinel}} |
|---|
| 30 | ''' compatible with TracTicketConditionalValidateFieldsPlugin ''' |
|---|
| 31 | |
|---|
| 32 | try: |
|---|
| 33 | from tracticketconditionalvalidatefield.api import TicketsValidator |
|---|
| 34 | if self.compmgr.is_enabled(TicketsValidator): |
|---|
| 35 | types = [k for k, v in self.config.options('ticket_validate') if k.find('.') == -1] |
|---|
| 36 | for ticket_type in types: |
|---|
| 37 | fields = self.config.getlist('ticket_validate', ticket_type) |
|---|
| 38 | condition = 'status=*&type=%s' % ticket_type |
|---|
| 39 | if condition not in rules: |
|---|
| 40 | rules[condition] = {} |
|---|
| 41 | rule = rules.get(condition) |
|---|
| 42 | for field in fields: |
|---|
| 43 | rule[field] = self.config.get('ticket_validate', '%s.tip' % field, default=sentinel) |
|---|
| 44 | except: |
|---|
| 45 | pass |
|---|
| 46 | |
|---|
| 47 | ''' compatible with TracTicketValidatorPlugin ''' |
|---|
| 48 | try: |
|---|
| 49 | from tracticketvalidator.ticketvalidator import TicketValidator |
|---|
| 50 | if self.compmgr.is_enabled(TicketValidator): |
|---|
| 51 | keys = [k[:-4] for k, v in self.config.options('ticketvalidator') if k.endswith('.rule')] # includes dot |
|---|
| 52 | for key in keys: |
|---|
| 53 | values = dict([(k[len(key):], v) for k, v in self.config.options('ticketvalidator') if k.startswith(key)]) |
|---|
| 54 | condition = '&'.join(['%s=%s' % (ck, values.get(ck, '*')) for ck in conditionkeys]) |
|---|
| 55 | if condition not in rules: |
|---|
| 56 | rules[condition] = {} |
|---|
| 57 | rule = rules.get(condition) |
|---|
| 58 | field = values.get('field', key[:-1]) |
|---|
| 59 | rule[field] = values.get('tip', sentinel) |
|---|
| 60 | except: |
|---|
| 61 | pass |
|---|
| 62 | return rules |
|---|
| 63 | |
|---|
| 64 | # IRequestHandler methods |
|---|
| 65 | def match_request(self, req): |
|---|
| 66 | return req.path_info == '/ticketvalidator.options' |
|---|
| 67 | |
|---|
| 68 | def process_request(self, req): |
|---|
| 69 | ''' return validation rules in json ''' |
|---|
| 70 | content = json.dumps({'rules': self.rules, 'workflow': self.workflow}, indent=4) |
|---|
| 71 | req.send_response(200) |
|---|
| 72 | req.send_header('Content-Type', 'application/json') |
|---|
| 73 | req.send_header('Content-Length', len(content)) |
|---|
| 74 | req.end_headers() |
|---|
| 75 | req.write(content) |
|---|
| 76 | |
|---|
| 77 | # IRequestFilter methods |
|---|
| 78 | def pre_process_request(self, req, handler): |
|---|
| 79 | return handler # unchanged |
|---|
| 80 | |
|---|
| 81 | def post_process_request(self, req, template, data, content_type): |
|---|
| 82 | if template in [ |
|---|
| 83 | 'ticket.html', # /ticket/{\d} or /newticket |
|---|
| 84 | ]: |
|---|
| 85 | add_script(req, "contextchrome/js/ticketvalidatordecolator.js") |
|---|
| 86 | status = 'ticket' in data and 'status' in data['ticket'].values and data['ticket'].values['status'] or 'sentinel' |
|---|
| 87 | add_script_data(req, {"contextchrome_tracstatus": status}) |
|---|
| 88 | add_script_data(req, contextchrome_ticketvalidator={'rules': self.rules, 'workflow': self.workflow}) |
|---|
| 89 | return template, data, content_type |
|---|
| 90 | |
|---|
| 91 | # ITemplateProvider methods |
|---|
| 92 | def get_htdocs_dirs(self): |
|---|
| 93 | return [('contextchrome', resource_filename(__name__, 'htdocs'))] |
|---|
| 94 | |
|---|
| 95 | def get_templates_dirs(self): |
|---|
| 96 | return [] |
|---|