Skip to content

Instantly share code, notes, and snippets.

@al-the-x al-the-x/WPGimmeTicket.py
Last active Aug 29, 2015

Embed
What would you like to do?
Trac Plugin for assigning oldest open Ticket to currently authenticated User, even if the `authuser` doesn't have `TICKET_UPDATE` permissions... all for my peeps at @WordPress
from trac.core import *
from trac.env import open_environment
from tracrpc.api import IXMLRPCHandler, expose_rpc
from tracrpc.util import to_timestamp
import trac.ticket.model as model
class WPGimmeTicketRPC(Component):
implements(IXMLRPCHandler)
def xmlrpc_namespace(self):
return 'gimme'
def xmlrpc_methods(self):
yield ('TICKET_CREATE', ((bool, str), (bool, str, int)), self.ticket)
def ticket(self, req):
env = open_environment() ## omitting the path uses $ENV[TRAC_ENV]
with env.db_transaction as db:
cursor = db.cursor()
cursor.execute("""
SELECT id FROM ticket
WHERE owner = %s LIMIT 1
""", (req.authuser,))
if len(cursor): return (False, 'You already have a ticket!')
## Honestly, I'm not 100% certain about the value of `ticket_enum.name`...
cursor.execute("""
SELECT id FROM ticket
JOIN ticket_enum ON ticket.type = ticket_enum.id
WHERE (owner IS NULL OR owner = '')
AND ticket_enum.name = 'open'
ORDER BY time ASC
LIMIT 1
""")
ticket_id = cursor.fetchone()[0]
ticket = model.Ticket(env, ticket_id)
ticket['owner'] = req.authuser
ticket.save_changes(comment=("Assigned to %s automatically via Magic Button" % req.authuser))
return (True, 'Assigned you ticket %s' % ticket_id, ticket_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.