diff --git a/apps/home/urls.py b/apps/home/urls.py index 8d52334..11f21dc 100644 --- a/apps/home/urls.py +++ b/apps/home/urls.py @@ -16,6 +16,7 @@ urlpatterns = [ path('get/', include([ path('one/', views.get_ticket, name="ticket-getone"), path('many/', views.get_tickets, name="ticket-getmany"), + path("filtercounts/", views.get_filter_counts, name="ticket-getfiltercounts"), ])), ])), diff --git a/apps/home/views.py b/apps/home/views.py index abd9bbd..0e6c761 100644 --- a/apps/home/views.py +++ b/apps/home/views.py @@ -57,6 +57,33 @@ def get_tickets(request): return JsonResponse(data) +@login_required +@require_POST +def get_filter_counts(request): + + priorities = TicketPriority.objects.all() + tags = TicketTag.objects.all() + departments = Department.objects.all() + + tickets = Ticket.objects.all() + + data = { + "priority_counts": {}, + "tag_counts": {}, + "department_counts": {}, + "ticket_count": tickets.count() + } + + for priority in priorities: + data["priority_counts"][str(priority.id)] = tickets.filter(priority=priority).count() + + for tag in tags: + data["tag_counts"][str(tag.id)] = tickets.filter(tags__in=[tag]).count() + + for department in departments: + data["department_counts"][str(department.id)] = tickets.filter(author__department=department).count() + + return JsonResponse(data) @login_required() @require_POST diff --git a/apps/templates/home/tickets.html b/apps/templates/home/tickets.html index 61853ff..e0d65a7 100644 --- a/apps/templates/home/tickets.html +++ b/apps/templates/home/tickets.html @@ -21,138 +21,86 @@
-
@@ -399,6 +347,7 @@ console.error(error) }); + loadFilterCounts(); loadAllTickets(); }); @@ -429,6 +378,47 @@ } } + function loadFilterCounts() { + // $("#filterSidebar") + + $.ajax({ + url: "{% url 'ticket-getfiltercounts' %}", + type: "POST", + dataType: "json", + data: { + csrfmiddlewaretoken: "{{ csrf_token }}", + }, + success: function(data) { + alert(JSON.stringify(data, null, 4)); + + var priorities = data.priority_counts; + var tags = data.tag_counts; + var departments = data.department_counts; + + $("#filterSidebar .filter-priority").each(function() { + var uuid = $(this).data("uuid"); + var count = priorities[uuid]; + $(this).find(".badge").text(count); + }); + + $("#filterSidebar .filter-tag").each(function() { + var uuid = $(this).data("uuid"); + var count = tags[uuid]; + $(this).find(".badge").text(count); + }); + + $("#filterSidebar .filter-department").each(function() { + var uuid = $(this).data("uuid"); + var count = departments[uuid]; + $(this).find(".badge").text(count); + }); + }, + error: function(data) { + alert(JSON.stringify(data, null, 4)) + } + }); + } + function loadAllTickets() { $("#ticketsContainer").empty();