1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/management/commands/pdb_deskpro_requeue.py
Matt Griswold cf56acbfc4 black formatted
2019-12-05 16:57:52 +00:00

50 lines
1.7 KiB
Python

from django.core.management.base import BaseCommand
from peeringdb_server.models import DeskProTicket
import re
class Command(BaseCommand):
help = "Reset a deskpro ticket and queue again for publish"
def add_arguments(self, parser):
parser.add_argument("id", nargs="?", help="ticket id")
parser.add_argument(
"--commit", action="store_true", help="will commit the changes"
)
parser.add_argument(
"--only-failed", action="store_true", help="only requeue failed tickets"
)
def log(self, msg):
if self.commit:
self.stdout.write(msg)
else:
self.stdout.write("[pretend] {}".format(msg))
def handle(self, *args, **options):
_id = options.get("id")
self.commit = options.get("commit")
self.only_failed = options.get("only_failed")
qset = DeskProTicket.objects
if _id[0] == "g":
self.log("Requeuing tickets with id greater than {}".format(_id[1:]))
qset = qset.filter(pk__gt=_id[1:])
elif _id[0] == "l":
self.log("Requeuing tickets with id smaller than {}".format(_id[1:]))
qset = qset.filter(pk__lt=_id[1:])
else:
qset = qset.filter(pk=_id)
for ticket in qset:
if self.only_failed and ticket.subject.find("[FAILED]") == -1:
continue
self.log("Requeuing ticket with id {}".format(ticket.id))
ticket.subject = ticket.subject.replace("[FAILED]", "")
ticket.body = re.sub(r"API Delivery Error(.+)$", "", ticket.body)
ticket.published = None
if self.commit:
ticket.save()