| 1 |
from trac.core import * |
|---|
| 2 |
from trac.web.api import ITemplateStreamFilter |
|---|
| 3 |
|
|---|
| 4 |
from genshi.builder import tag |
|---|
| 5 |
from genshi.filters import Transformer |
|---|
| 6 |
|
|---|
| 7 |
revision = "$Rev: 2 $" |
|---|
| 8 |
url = "$URL: /mirror/sample-plugins/ticket_clone.py $" |
|---|
| 9 |
|
|---|
| 10 |
class SimpleTicketCloneButton(Component): |
|---|
| 11 |
"""Add a 'Clone' button to the ticket box. |
|---|
| 12 |
|
|---|
| 13 |
This button is located next to the 'Reply' to description button, |
|---|
| 14 |
and pressing it will send a request for creating a new ticket |
|---|
| 15 |
which will be based on the cloned one. |
|---|
| 16 |
""" |
|---|
| 17 |
|
|---|
| 18 |
implements(ITemplateStreamFilter) |
|---|
| 19 |
|
|---|
| 20 |
# ITemplateStreamFilter methods |
|---|
| 21 |
|
|---|
| 22 |
def filter_stream(self, req, method, filename, stream, data): |
|---|
| 23 |
if filename == 'ticket.html': |
|---|
| 24 |
ticket = data.get('ticket') |
|---|
| 25 |
if ticket and ticket.exists and \ |
|---|
| 26 |
'TICKET_ADMIN' in req.perm(ticket.resource): |
|---|
| 27 |
filter = Transformer('//h3[@id="comment:description"]') |
|---|
| 28 |
return stream | filter.after(self._clone_form(req, ticket, data)) |
|---|
| 29 |
return stream |
|---|
| 30 |
|
|---|
| 31 |
def _clone_form(self, req, ticket, data): |
|---|
| 32 |
fields = {} |
|---|
| 33 |
for f in data.get('fields', []): |
|---|
| 34 |
name = f['name'] |
|---|
| 35 |
if name == 'summary': |
|---|
| 36 |
fields['summary'] = ticket['summary'] + " (cloned)" |
|---|
| 37 |
elif name == 'description': |
|---|
| 38 |
fields['description'] = "Cloned from #%s: \n----\n%s" % \ |
|---|
| 39 |
(ticket.id, ticket['description']) |
|---|
| 40 |
else: |
|---|
| 41 |
fields[name] = ticket[name] |
|---|
| 42 |
return tag.form( |
|---|
| 43 |
tag.div( |
|---|
| 44 |
tag.input(type="submit", name="clone", value="Clone", |
|---|
| 45 |
title="Create a copy of this ticket"), |
|---|
| 46 |
[tag.input(type="hidden", name=n, value=v) for n, v in |
|---|
| 47 |
fields.items()], |
|---|
| 48 |
class_="inlinebuttons"), |
|---|
| 49 |
method="get", action=req.href.newticket()) |
|---|
| 50 |
|
|---|