2008-02-06 05:36:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
"""
|
|
|
|
Jutda Helpdesk - A Django powered ticket tracker for small enterprise.
|
2008-01-22 06:54:22 +01:00
|
|
|
|
2008-02-06 05:36:07 +01:00
|
|
|
(c) Copyright 2008 Jutda. All Rights Reserved. See LICENSE for details.
|
2008-01-22 06:54:22 +01:00
|
|
|
|
2008-02-06 05:36:07 +01:00
|
|
|
scripts/create_escalation_exclusion.py - Easy way to routinely add particular
|
|
|
|
days to the list of days on which no
|
|
|
|
escalation should take place.
|
2008-01-22 06:54:22 +01:00
|
|
|
"""
|
2018-12-28 09:13:52 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
|
|
|
|
from helpdesk.models import EscalationExclusion, Queue
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2013-01-23 01:35:18 +01:00
|
|
|
from datetime import timedelta, date
|
2008-08-19 10:50:38 +02:00
|
|
|
import getopt
|
|
|
|
from optparse import make_option
|
|
|
|
import sys
|
|
|
|
|
2008-08-18 23:29:31 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2016-10-23 22:09:17 +02:00
|
|
|
|
2008-08-18 23:29:31 +02:00
|
|
|
def __init__(self):
|
|
|
|
BaseCommand.__init__(self)
|
|
|
|
|
|
|
|
self.option_list += (
|
|
|
|
make_option(
|
2008-08-19 10:50:38 +02:00
|
|
|
'--days', '-d',
|
2008-08-18 23:29:31 +02:00
|
|
|
help='Days of week (monday, tuesday, etc)'),
|
|
|
|
make_option(
|
2008-08-19 10:50:38 +02:00
|
|
|
'--occurrences', '-o',
|
2008-08-18 23:29:31 +02:00
|
|
|
type='int',
|
|
|
|
default=1,
|
|
|
|
help='Occurrences: How many weeks ahead to exclude this day'),
|
|
|
|
make_option(
|
2008-08-19 10:50:38 +02:00
|
|
|
'--queues', '-q',
|
2008-08-18 23:29:31 +02:00
|
|
|
help='Queues to include (default: all). Use queue slugs'),
|
|
|
|
make_option(
|
2008-11-18 01:00:36 +01:00
|
|
|
'--escalate-verbosely', '-x',
|
2008-08-18 23:29:31 +02:00
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2015-06-17 16:22:44 +02:00
|
|
|
dest='escalate-verbosely',
|
2008-08-18 23:29:31 +02:00
|
|
|
help='Display a list of dates excluded'),
|
2016-10-23 22:09:17 +02:00
|
|
|
)
|
2008-08-18 23:29:31 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
days = options['days']
|
2016-10-21 17:14:12 +02:00
|
|
|
# optparse should already handle the `or 1`
|
|
|
|
occurrences = options['occurrences'] or 1
|
2008-08-18 23:29:31 +02:00
|
|
|
verbose = False
|
|
|
|
queue_slugs = options['queues']
|
|
|
|
queues = []
|
|
|
|
|
2008-11-18 01:00:36 +01:00
|
|
|
if options['escalate-verbosely']:
|
2008-08-18 23:29:31 +02:00
|
|
|
verbose = True
|
|
|
|
|
|
|
|
if not (days and occurrences):
|
|
|
|
raise CommandError('One or more occurrences must be specified.')
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-08-18 23:29:31 +02:00
|
|
|
if queue_slugs is not None:
|
|
|
|
queue_set = queue_slugs.split(',')
|
|
|
|
for queue in queue_set:
|
|
|
|
try:
|
|
|
|
q = Queue.objects.get(slug__exact=queue)
|
|
|
|
except Queue.DoesNotExist:
|
|
|
|
raise CommandError("Queue %s does not exist." % queue)
|
|
|
|
queues.append(q)
|
|
|
|
|
|
|
|
create_exclusions(days=days, occurrences=occurrences, verbose=verbose, queues=queues)
|
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-22 06:54:22 +01:00
|
|
|
day_names = {
|
|
|
|
'monday': 0,
|
|
|
|
'tuesday': 1,
|
|
|
|
'wednesday': 2,
|
|
|
|
'thursday': 3,
|
|
|
|
'friday': 4,
|
|
|
|
'saturday': 5,
|
|
|
|
'sunday': 6,
|
|
|
|
}
|
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-22 06:54:22 +01:00
|
|
|
def create_exclusions(days, occurrences, verbose, queues):
|
|
|
|
days = days.split(',')
|
|
|
|
for day in days:
|
|
|
|
day_name = day
|
|
|
|
day = day_names[day]
|
|
|
|
workdate = date.today()
|
|
|
|
i = 0
|
|
|
|
while i < occurrences:
|
|
|
|
if day == workdate.weekday():
|
|
|
|
if EscalationExclusion.objects.filter(date=workdate).count() == 0:
|
|
|
|
esc = EscalationExclusion(name='Auto Exclusion for %s' % day_name, date=workdate)
|
|
|
|
esc.save()
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-22 06:54:22 +01:00
|
|
|
if verbose:
|
2016-03-01 21:01:58 +01:00
|
|
|
print("Created exclusion for %s %s" % (day_name, workdate))
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-22 06:54:22 +01:00
|
|
|
for q in queues:
|
|
|
|
esc.queues.add(q)
|
|
|
|
if verbose:
|
2016-03-01 21:01:58 +01:00
|
|
|
print(" - for queue %s" % q)
|
2008-01-22 06:54:22 +01:00
|
|
|
|
|
|
|
i += 1
|
|
|
|
workdate += timedelta(days=1)
|
|
|
|
|
|
|
|
|
|
|
|
def usage():
|
2016-03-01 21:01:58 +01:00
|
|
|
print("Options:")
|
|
|
|
print(" --days, -d: Days of week (monday, tuesday, etc)")
|
|
|
|
print(" --occurrences, -o: Occurrences: How many weeks ahead to exclude this day")
|
|
|
|
print(" --queues, -q: Queues to include (default: all). Use queue slugs")
|
|
|
|
print(" --verbose, -v: Display a list of dates excluded")
|
|
|
|
|
2008-01-22 06:54:22 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2008-08-19 10:50:38 +02:00
|
|
|
# This script can be run from the command-line or via Django's manage.py.
|
2008-01-22 06:54:22 +01:00
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 'd:o:q:v', ['days=', 'occurrences=', 'verbose', 'queues='])
|
|
|
|
except getopt.GetoptError:
|
|
|
|
usage()
|
|
|
|
sys.exit(2)
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-22 06:54:22 +01:00
|
|
|
days = None
|
2016-10-21 17:14:12 +02:00
|
|
|
occurrences = 1
|
2008-01-22 06:54:22 +01:00
|
|
|
verbose = False
|
|
|
|
queue_slugs = None
|
|
|
|
queues = []
|
|
|
|
|
|
|
|
for o, a in opts:
|
2008-11-18 01:00:36 +01:00
|
|
|
if o in ('-x', '--escalate-verbosely'):
|
2008-01-22 06:54:22 +01:00
|
|
|
verbose = True
|
|
|
|
if o in ('-d', '--days'):
|
|
|
|
days = a
|
|
|
|
if o in ('-q', '--queues'):
|
|
|
|
queue_slugs = a
|
|
|
|
if o in ('-o', '--occurrences'):
|
2016-10-21 17:14:12 +02:00
|
|
|
occurrences = int(a) or 1
|
2008-01-22 06:54:22 +01:00
|
|
|
|
|
|
|
if not (days and occurrences):
|
|
|
|
usage()
|
|
|
|
sys.exit(2)
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2008-01-22 06:54:22 +01:00
|
|
|
if queue_slugs is not None:
|
|
|
|
queue_set = queue_slugs.split(',')
|
|
|
|
for queue in queue_set:
|
|
|
|
try:
|
|
|
|
q = Queue.objects.get(slug__exact=queue)
|
2008-08-18 23:29:31 +02:00
|
|
|
except Queue.DoesNotExist:
|
2016-03-01 21:01:58 +01:00
|
|
|
print("Queue %s does not exist." % queue)
|
2008-01-22 06:54:22 +01:00
|
|
|
sys.exit(2)
|
|
|
|
queues.append(q)
|
|
|
|
|
|
|
|
create_exclusions(days=days, occurrences=occurrences, verbose=verbose, queues=queues)
|