| 1 | import re |
|---|
| 2 | import dbhelper |
|---|
| 3 | from trac import util |
|---|
| 4 | from trac.web.api import ITemplateStreamFilter |
|---|
| 5 | from trac.web.api import IRequestFilter |
|---|
| 6 | from trac.core import * |
|---|
| 7 | from genshi.core import * |
|---|
| 8 | from genshi.builder import tag |
|---|
| 9 | from genshi.filters.transform import Transformer |
|---|
| 10 | from trac.web.chrome import add_script |
|---|
| 11 | |
|---|
| 12 | # This can go away once they fix http://genshi.edgewall.org/ticket/136 |
|---|
| 13 | # At that point we should use Transformer.filter THIS IS STILL SOLVING |
|---|
| 14 | # PROBLEMS WELL AFTER THAT TICKET HAS BEEN CLOSED - A new ticket #290 |
|---|
| 15 | # [1000] has fixed the bug, but is not the trac default yet Without |
|---|
| 16 | # this (using the default filter) I was getting omitted closing tags |
|---|
| 17 | # for some tags (Based on whitespace afaict) |
|---|
| 18 | |
|---|
| 19 | class FilterTransformation(object): |
|---|
| 20 | """Apply a normal stream filter to the selection. The filter is called once |
|---|
| 21 | for each contiguous block of marked events.""" |
|---|
| 22 | |
|---|
| 23 | def __init__(self, filter): |
|---|
| 24 | """Create the transform. |
|---|
| 25 | |
|---|
| 26 | :param filter: The stream filter to apply. |
|---|
| 27 | """ |
|---|
| 28 | self.filter = filter |
|---|
| 29 | |
|---|
| 30 | def __call__(self, stream): |
|---|
| 31 | """Apply the transform filter to the marked stream. |
|---|
| 32 | |
|---|
| 33 | :param stream: The marked event stream to filter |
|---|
| 34 | """ |
|---|
| 35 | def flush(queue): |
|---|
| 36 | if queue: |
|---|
| 37 | for event in self.filter(queue): |
|---|
| 38 | yield event |
|---|
| 39 | del queue[:] |
|---|
| 40 | |
|---|
| 41 | queue = [] |
|---|
| 42 | for mark, event in stream: |
|---|
| 43 | if mark: |
|---|
| 44 | queue.append(event) |
|---|
| 45 | else: |
|---|
| 46 | for e in flush(queue): |
|---|
| 47 | yield None,e |
|---|
| 48 | yield None,event |
|---|
| 49 | for event in flush(queue): |
|---|
| 50 | yield None,event |
|---|
| 51 | |
|---|
| 52 | #@staticmethod |
|---|
| 53 | def disable_field(field_stream): |
|---|
| 54 | value = Stream(field_stream).select('@value').render() |
|---|
| 55 | |
|---|
| 56 | for kind,data,pos in tag.span(value, id="field-totalhours").generate(): |
|---|
| 57 | yield kind,data,pos |
|---|
| 58 | |
|---|
| 59 | class TotalHoursFilter(Component): |
|---|
| 60 | """Disable editing of the Total Hours field so that we don't need Javascript.""" |
|---|
| 61 | implements(ITemplateStreamFilter) |
|---|
| 62 | |
|---|
| 63 | def match_stream(self, req, method, filename, stream, data): |
|---|
| 64 | #self.log.debug("matching: ticket.html") |
|---|
| 65 | return filename == 'ticket.html' |
|---|
| 66 | |
|---|
| 67 | def filter_stream(self, req, method, filename, stream, data): |
|---|
| 68 | return stream | Transformer( |
|---|
| 69 | '//input[@id="field-totalhours" and @type="text" and @name="field_totalhours"]' |
|---|
| 70 | ).apply(FilterTransformation(disable_field)) |
|---|
| 71 | |
|---|
| 72 | class ReportsFilter(Component): |
|---|
| 73 | """This component Removed rows from the report that require the |
|---|
| 74 | management screen to supply values""" |
|---|
| 75 | implements(IRequestFilter) |
|---|
| 76 | def pre_process_request(self, req, handler): |
|---|
| 77 | return handler |
|---|
| 78 | |
|---|
| 79 | def post_process_request(self, req, template, data, content_type): |
|---|
| 80 | if template == 'report_list.html': |
|---|
| 81 | add_script(req, "billing/report_filter.js") |
|---|
| 82 | return (template, data, content_type) |
|---|