| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright 2008 Optaros, Inc |
|---|
| 4 | |
|---|
| 5 | from trac.core import implements, Component |
|---|
| 6 | from trac.resource import Resource |
|---|
| 7 | |
|---|
| 8 | from genshi.core import QName |
|---|
| 9 | from genshi.builder import Element, tag |
|---|
| 10 | |
|---|
| 11 | from ticketsystem.api import ITicketFieldProvider, \ |
|---|
| 12 | ITicketUIWrapper, TypedTicket, TypedTicketSystem |
|---|
| 13 | from ticketsystem.tickettypes.util import TicketMetadataProvider |
|---|
| 14 | |
|---|
| 15 | class OForgeTicketHandler(Component) : |
|---|
| 16 | """ |
|---|
| 17 | This class adds logic for making the ticket submit buttons work and to |
|---|
| 18 | implement oforge ticket types. |
|---|
| 19 | """ |
|---|
| 20 | |
|---|
| 21 | implements(ITicketFieldProvider, ITicketUIWrapper) |
|---|
| 22 | |
|---|
| 23 | # ITicketUIWrapper |
|---|
| 24 | def match_for_render(self, type): |
|---|
| 25 | if type in self.get_ticket_types() : |
|---|
| 26 | return True |
|---|
| 27 | return False |
|---|
| 28 | |
|---|
| 29 | def get_content_to_inject(self, req, data): |
|---|
| 30 | return dict() |
|---|
| 31 | |
|---|
| 32 | def process_changes(self, req, ticket): |
|---|
| 33 | # add in the session the redirect to new |
|---|
| 34 | if req and 'submit' in req.args and req.args['submit'] == 'Save & New' : |
|---|
| 35 | req.session['ticket-redirect'] = 'new' |
|---|
| 36 | if req and 'submit' in req.args and req.args['submit'] == 'Save & Close' : |
|---|
| 37 | req.session['ticket-redirect'] = 'query' |
|---|
| 38 | if req and 'submit' in req.args and req.args['submit'] == 'Change' : |
|---|
| 39 | new_data = {} |
|---|
| 40 | for field in req.args.keys() : |
|---|
| 41 | if field.startswith('field_') : |
|---|
| 42 | new_data[field.lstrip('field')[1:]] = req.args[field] |
|---|
| 43 | req.redirect(req.href.newticket(**new_data)) |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | def handle_request(self, req, data, content_type): |
|---|
| 47 | if data.has_key('action_controls') : |
|---|
| 48 | # add class to inputs |
|---|
| 49 | for key, label, action_control, hints in data['action_controls'] : |
|---|
| 50 | for el in action_control.children : |
|---|
| 51 | if type(el) is Element : |
|---|
| 52 | el.attrib |= [(QName('class'), 'fieldClass-text')] |
|---|
| 53 | el.attrib |= [(QName('style'), 'width: 10em;float: none;!important')] |
|---|
| 54 | el.append(tag.br()) |
|---|
| 55 | if req.chrome['links'].has_key('up') : |
|---|
| 56 | data['from_query'] = True |
|---|
| 57 | else : |
|---|
| 58 | data['from_query'] = False |
|---|
| 59 | |
|---|
| 60 | ticket_redirect=req.session.get('ticket-redirect', False) |
|---|
| 61 | if ticket_redirect: |
|---|
| 62 | del(req.session['ticket-redirect']) |
|---|
| 63 | if ticket_redirect == 'new': |
|---|
| 64 | # create the get parameter |
|---|
| 65 | new_data = {'type': self.get_ticket_type(),} |
|---|
| 66 | # redirect to new |
|---|
| 67 | req.redirect(req.href.newticket(**new_data)) |
|---|
| 68 | elif ticket_redirect == 'query' : |
|---|
| 69 | req.redirect(req.chrome['links']['up'][0]['href']) |
|---|
| 70 | |
|---|
| 71 | return "ticket.html", data, content_type |
|---|
| 72 | |
|---|
| 73 | # ITicketFieldProvider methods |
|---|
| 74 | |
|---|
| 75 | def get_fields(self, type): |
|---|
| 76 | # get the relationships described in the ini file |
|---|
| 77 | resource_filters = TicketMetadataProvider(self.env) \ |
|---|
| 78 | .get_field_filters(Resource('ticket'), type) |
|---|
| 79 | if resource_filters is not None : |
|---|
| 80 | return resource_filters['fields'] |
|---|
| 81 | |
|---|
| 82 | return None |
|---|
| 83 | |
|---|
| 84 | def get_ticket_types(self): |
|---|
| 85 | return self.env.config.getlist('ticket-system', 'ticket.type', []) |
|---|
| 86 | |
|---|
| 87 | def get_value(self, ticket, field_name): |
|---|
| 88 | return None |
|---|
| 89 | |
|---|