2008-02-06 05:36:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
"""
|
2011-04-20 01:02:33 +02:00
|
|
|
django-helpdesk - A Django powered ticket tracker for small enterprise.
|
2008-01-16 05:38:24 +01:00
|
|
|
|
2008-02-06 05:36:07 +01:00
|
|
|
(c) Copyright 2008 Jutda. All Rights Reserved. See LICENSE for details.
|
2008-01-16 05:38:24 +01:00
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
scripts/escalate_tickets.py - Easy way to escalate tickets based on their age,
|
2008-02-06 05:36:07 +01:00
|
|
|
designed to be run from Cron or similar.
|
2008-01-16 05:38:24 +01:00
|
|
|
"""
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2022-07-22 03:26:41 +02:00
|
|
|
from datetime import date, timedelta
|
2024-10-19 00:31:00 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
2008-01-16 05:38:24 +01:00
|
|
|
from django.db.models import Q
|
2018-12-28 09:13:52 +01:00
|
|
|
from django.utils import timezone
|
2022-07-22 03:26:41 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2018-10-31 16:24:57 +01:00
|
|
|
from helpdesk.lib import safe_template_context
|
2024-10-19 00:31:00 +02:00
|
|
|
from helpdesk.models import EscalationExclusion, Queue, Ticket
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-08-18 23:29:31 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2024-10-19 00:31:00 +02:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument(
|
|
|
|
'-q',
|
|
|
|
'--queues',
|
|
|
|
nargs='*',
|
|
|
|
choices=list(Queue.objects.values_list('slug', flat=True)),
|
2024-10-19 16:50:17 +02:00
|
|
|
help='Queues to include (default: all). Enter the queues slug as space separated list.'
|
2024-10-19 00:31:00 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-x',
|
|
|
|
'--escalate-verbosely',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='Display escalated tickets'
|
2016-10-23 22:09:17 +02:00
|
|
|
)
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-08-18 23:29:31 +02:00
|
|
|
def handle(self, *args, **options):
|
2024-10-19 00:31:00 +02:00
|
|
|
verbose = options['escalate_verbosely']
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2024-10-19 00:31:00 +02:00
|
|
|
queue_slugs = options['queues']
|
|
|
|
# Only include queues with escalation configured
|
|
|
|
queues = Queue.objects.filter(escalate_days__isnull=False).exclude(escalate_days=0)
|
2008-08-18 23:29:31 +02:00
|
|
|
if queue_slugs is not None:
|
2024-10-19 00:31:00 +02:00
|
|
|
queues = queues.filter(slug__in=queue_slugs)
|
2008-08-18 23:29:31 +02:00
|
|
|
|
2024-10-19 00:31:00 +02:00
|
|
|
if verbose:
|
|
|
|
self.stdout.write(f"Processing: {queues}")
|
|
|
|
|
|
|
|
for queue in queues:
|
|
|
|
last = date.today() - timedelta(days=queue.escalate_days)
|
|
|
|
today = date.today()
|
|
|
|
workdate = last
|
|
|
|
|
|
|
|
days = 0
|
|
|
|
|
|
|
|
while workdate < today:
|
|
|
|
if not EscalationExclusion.objects.filter(date=workdate).exists():
|
|
|
|
days += 1
|
|
|
|
workdate = workdate + timedelta(days=1)
|
|
|
|
|
|
|
|
req_last_escl_date = timezone.now() - timedelta(days=days)
|
|
|
|
|
|
|
|
for ticket in queue.ticket_set.filter(
|
|
|
|
status__in=Ticket.OPEN_STATUSES
|
|
|
|
).exclude(
|
|
|
|
priority=1
|
|
|
|
).filter(
|
|
|
|
Q(on_hold__isnull=True) | Q(on_hold=False)
|
|
|
|
).filter(
|
|
|
|
Q(last_escalation__lte=req_last_escl_date) |
|
|
|
|
Q(last_escalation__isnull=True, created__lte=req_last_escl_date)
|
|
|
|
):
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2024-10-19 00:31:00 +02:00
|
|
|
ticket.last_escalation = timezone.now()
|
|
|
|
ticket.priority -= 1
|
|
|
|
ticket.save()
|
2008-01-16 05:38:24 +01:00
|
|
|
|
2024-10-19 00:31:00 +02:00
|
|
|
context = safe_template_context(ticket)
|
2008-01-22 06:54:22 +01:00
|
|
|
|
2024-10-19 00:31:00 +02:00
|
|
|
ticket.send(
|
|
|
|
{'submitter': ('escalated_submitter', context),
|
|
|
|
'ticket_cc': ('escalated_cc', context),
|
|
|
|
'assigned_to': ('escalated_owner', context)},
|
|
|
|
fail_silently=True,
|
|
|
|
)
|
2008-01-16 05:38:24 +01:00
|
|
|
|
2024-10-19 00:31:00 +02:00
|
|
|
if verbose:
|
|
|
|
self.stdout.write(f" - Esclating {ticket.ticket} from {ticket.priority + 1}>{ticket.priority}")
|
2008-01-22 06:54:22 +01:00
|
|
|
|
2024-10-19 00:31:00 +02:00
|
|
|
followup = ticket.followup_set.create(
|
|
|
|
title=_('Ticket Escalated'),
|
|
|
|
public=True,
|
|
|
|
comment=_('Ticket escalated after %(nb)s days') % {'nb': queue.escalate_days},
|
2016-03-01 21:01:58 +01:00
|
|
|
)
|
2008-01-22 06:54:22 +01:00
|
|
|
|
2024-10-19 00:31:00 +02:00
|
|
|
followup.ticketchange_set.create(
|
|
|
|
field=_('Priority'),
|
|
|
|
old_value=ticket.priority + 1,
|
|
|
|
new_value=ticket.priority,
|
|
|
|
)
|