Merge pull request #603 from andreyyudin/master

Replacement of hard-coded SQL query for issue #601
This commit is contained in:
Garret Wassermann 2018-03-29 22:00:23 -04:00 committed by GitHub
commit ff95fa5d6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -151,21 +151,6 @@ def dashboard(request):
else: else:
where_clause = """WHERE q.id = t.queue_id""" where_clause = """WHERE q.id = t.queue_id"""
cursor = connection.cursor()
cursor.execute("""
SELECT q.id as queue,
q.title AS name,
COUNT(CASE t.status WHEN '1' THEN t.id WHEN '2' THEN t.id END) AS open,
COUNT(CASE t.status WHEN '3' THEN t.id END) AS resolved,
COUNT(CASE t.status WHEN '4' THEN t.id END) AS closed
%s
%s
GROUP BY queue, name
ORDER BY q.id;
""" % (from_clause, where_clause))
dash_tickets = query_to_dict(cursor.fetchall(), cursor.description)
return render(request, 'helpdesk/dashboard.html', { return render(request, 'helpdesk/dashboard.html', {
'user_tickets': tickets, 'user_tickets': tickets,
'user_tickets_closed_resolved': tickets_closed_resolved, 'user_tickets_closed_resolved': tickets_closed_resolved,
@ -1103,31 +1088,18 @@ def report_index(request):
# Open Resolved # Open Resolved
# Queue 1 10 4 # Queue 1 10 4
# Queue 2 4 12 # Queue 2 4 12
Queues = user_queues if user_queues else Queue.objects.all()
queues = _get_user_queues(request.user).values_list('id', flat=True) dash_tickets = []
for queue in Queues:
from_clause = """FROM helpdesk_ticket t, dash_ticket = {
helpdesk_queue q""" 'queue': queue.id,
if queues: 'name': queue.title,
where_clause = """WHERE q.id = t.queue_id AND 'open': queue.ticket_set.filter(status__in=[1, 2]).count(),
q.id IN (%s)""" % (",".join(("%d" % pk for pk in queues))) 'resolved': queue.ticket_set.filter(status=3).count(),
else: 'closed': queue.ticket_set.filter(status=4).count(),
where_clause = """WHERE q.id = t.queue_id""" }
dash_tickets.append(dash_ticket)
cursor = connection.cursor()
cursor.execute("""
SELECT q.id as queue,
q.title AS name,
COUNT(CASE t.status WHEN '1' THEN t.id WHEN '2' THEN t.id END) AS open,
COUNT(CASE t.status WHEN '3' THEN t.id END) AS resolved,
COUNT(CASE t.status WHEN '4' THEN t.id END) AS closed
%s
%s
GROUP BY queue, name
ORDER BY q.id;
""" % (from_clause, where_clause))
dash_tickets = query_to_dict(cursor.fetchall(), cursor.description)
return render(request, 'helpdesk/report_index.html', { return render(request, 'helpdesk/report_index.html', {
'number_tickets': number_tickets, 'number_tickets': number_tickets,