| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2006-2008 Noah Kantrowitz <noah@coderanger.net> |
|---|
| 4 | # Copyright (C) 2011-2016 Ryan J Ollos <ryan.j.ollos@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.config import BoolOption, ListOption |
|---|
| 11 | from trac.core import Component, implements |
|---|
| 12 | from trac.perm import IPermissionRequestor |
|---|
| 13 | from trac.web.api import IRequestFilter |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | class SimpleTicketModule(Component): |
|---|
| 17 | """A request filter to implement the SimpleTicket reduced ticket |
|---|
| 18 | entry form.""" |
|---|
| 19 | implements(IPermissionRequestor, IRequestFilter) |
|---|
| 20 | |
|---|
| 21 | fields = ListOption('simpleticket', 'fields', default='', |
|---|
| 22 | doc="""Fields to hide for the simple ticket entry |
|---|
| 23 | form.""") |
|---|
| 24 | |
|---|
| 25 | show_only = BoolOption('simpleticket', 'show_only', default=False, |
|---|
| 26 | doc="""If True, show only the specified fields |
|---|
| 27 | rather than hiding the specified fields""") |
|---|
| 28 | |
|---|
| 29 | required_fields = set(['summary', 'reporter', 'owner', |
|---|
| 30 | 'description', 'type', 'status']) |
|---|
| 31 | |
|---|
| 32 | # IPermissionRequestor methods |
|---|
| 33 | |
|---|
| 34 | def get_permission_actions(self): |
|---|
| 35 | yield 'TICKET_CREATE_SIMPLE', ['TICKET_CREATE'] |
|---|
| 36 | |
|---|
| 37 | # IRequestFilter methods |
|---|
| 38 | |
|---|
| 39 | def pre_process_request(self, req, handler): |
|---|
| 40 | return handler |
|---|
| 41 | |
|---|
| 42 | def post_process_request(self, req, template, data, content_type): |
|---|
| 43 | if template is not None and data is not None and \ |
|---|
| 44 | req.path_info.startswith('/newticket') and \ |
|---|
| 45 | 'fields' in data and \ |
|---|
| 46 | data['fields'] is not None: |
|---|
| 47 | if 'TICKET_CREATE_SIMPLE' in req.perm and \ |
|---|
| 48 | 'TRAC_ADMIN' not in req.perm: |
|---|
| 49 | self.log.debug('SimpleTicket: Filtering new ticket form ' |
|---|
| 50 | 'for %s', req.authname) |
|---|
| 51 | if self.show_only: |
|---|
| 52 | fields = set(self.fields) | self.required_fields |
|---|
| 53 | data['fields'] = [f for f in data['fields'] |
|---|
| 54 | if f is not None and 'name' in f and |
|---|
| 55 | f['name'] in fields] |
|---|
| 56 | else: |
|---|
| 57 | fields = set(self.fields) - self.required_fields |
|---|
| 58 | data['fields'] = [f for f in data['fields'] |
|---|
| 59 | if f is not None and 'name' in f and |
|---|
| 60 | f['name'] not in fields] |
|---|
| 61 | |
|---|
| 62 | return template, data, content_type |
|---|