Refactor ticket list

This commit is contained in:
Timothy Hobbs 2019-10-08 15:35:07 +02:00
parent 86444e2bc7
commit f127b2c224

View File

@ -6,6 +6,8 @@ django-helpdesk - A Django powered ticket tracker for small enterprise.
views/staff.py - The bulk of the application - provides most business logic and views/staff.py - The bulk of the application - provides most business logic and
renders all staff-facing views. renders all staff-facing views.
""" """
from copy import deepcopy
from django import VERSION as DJANGO_VERSION from django import VERSION as DJANGO_VERSION
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
@ -840,6 +842,10 @@ def ticket_list(request):
'keyword': None, 'keyword': None,
'search_string': None, 'search_string': None,
} }
default_query_params = {
'filtering': {'status__in': [1, 2, 3]},
'sorting': 'created',
}
from_saved_query = False from_saved_query = False
@ -898,46 +904,25 @@ def ticket_list(request):
# Query deserialization failed. (E.g. was a pickled query) # Query deserialization failed. (E.g. was a pickled query)
return HttpResponseRedirect(reverse('helpdesk:list')) return HttpResponseRedirect(reverse('helpdesk:list'))
elif not ('queue' in request.GET or elif not {'queue', 'assigned_to', 'status', 'q', 'sort', 'sortreverse'}.intersection(request.GET):
'assigned_to' in request.GET or # Fall-back if no querying is being done
'status' in request.GET or
'q' in request.GET or
'sort' in request.GET or
'sortreverse' in request.GET):
# Fall-back if no querying is being done, force the list to only
# show open/reopened/resolved (not closed) cases sorted by creation
# date.
all_queues = Queue.objects.all() all_queues = Queue.objects.all()
query_params = { query_params = deepcopy(default_query_params)
'filtering': {'status__in': [1, 2, 3]},
'sorting': 'created',
}
else: else:
queues = request.GET.getlist('queue') filter_in_params = [
if queues: ('queue', 'queue__id__in'),
try: ('assigned_to', 'assigned_to__id__in'),
queues = [int(q) for q in queues] ('status', 'status__in'),
query_params['filtering']['queue__id__in'] = queues ]
except ValueError:
pass
owners = request.GET.getlist('assigned_to') for param, filter_command in filter_in_params:
if owners: patterns = request.GET.getlist(param)
try: if patterns:
owners = [int(u) for u in owners] try:
query_params['filtering']['assigned_to__id__in'] = owners pattern_pks = [int(pattern) for pattern in patterns]
except ValueError: query_params['filtering'][filter_command] = pattern_pks
pass except ValueError:
pass
statuses = request.GET.getlist('status')
if statuses:
try:
statuses = [int(s) for s in statuses]
query_params['filtering']['status__in'] = statuses
except ValueError:
pass
date_from = request.GET.get('date_from') date_from = request.GET.get('date_from')
if date_from: if date_from:
@ -969,11 +954,7 @@ def ticket_list(request):
ticket_qs = apply_query(tickets, query_params) ticket_qs = apply_query(tickets, query_params)
except ValidationError: except ValidationError:
# invalid parameters in query, return default query # invalid parameters in query, return default query
query_params = { ticket_qs = apply_query(tickets, default_query_params)
'filtering': {'status__in': [1, 2, 3]},
'sorting': 'created',
}
ticket_qs = apply_query(tickets, query_params)
search_message = '' search_message = ''
if 'query' in context and settings.DATABASES['default']['ENGINE'].endswith('sqlite'): if 'query' in context and settings.DATABASES['default']['ENGINE'].endswith('sqlite'):
@ -1001,7 +982,6 @@ def ticket_list(request):
return render(request, 'helpdesk/ticket_list.html', dict( return render(request, 'helpdesk/ticket_list.html', dict(
context, context,
tickets=ticket_qs,
default_tickets_per_page=request.user.usersettings_helpdesk.tickets_per_page, default_tickets_per_page=request.user.usersettings_helpdesk.tickets_per_page,
user_choices=User.objects.filter(is_active=True, is_staff=True), user_choices=User.objects.filter(is_active=True, is_staff=True),
queue_choices=user_queues, queue_choices=user_queues,