working on filter functionality #4
This commit is contained in:
parent
8acb8c3aca
commit
03b34ebd20
@ -1,5 +1,6 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
import json
|
||||
from datetime import timedelta, datetime
|
||||
|
||||
from django import template
|
||||
@ -9,7 +10,7 @@ from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
|
||||
from django.template import loader
|
||||
from django.shortcuts import render
|
||||
from django.urls import reverse
|
||||
from django.forms.models import model_to_dict
|
||||
from django.db.models import Q
|
||||
|
||||
from ..authentication.models import Department
|
||||
from .models import Ticket, TicketPriority, TicketTag
|
||||
@ -50,9 +51,28 @@ def get_ticket(request):
|
||||
@require_POST
|
||||
def get_tickets(request):
|
||||
|
||||
filters = dict(request.POST.get("filters", {}))
|
||||
filters = json.loads(request.POST.get("filters", "{}"))
|
||||
|
||||
tickets = Ticket.objects.filter(**filters).order_by("-create_timestamp")
|
||||
# conditions = [Q(**{key: value}) for key, value in filters.items()]
|
||||
# combined_conditions = Q()
|
||||
# for condition in conditions:
|
||||
# combined_conditions &= condition
|
||||
|
||||
# print(combined_conditions)
|
||||
|
||||
# query = Q()
|
||||
# for key, values in filters.items():
|
||||
# print("keyvalues", key, values)
|
||||
# query &= Q(**{key: values})
|
||||
|
||||
query = Q()
|
||||
for key, values in filters.items():
|
||||
for value in values:
|
||||
query &= Q(**{key: [value]})
|
||||
|
||||
print(query)
|
||||
|
||||
tickets = Ticket.objects.filter(query).order_by("-create_timestamp")
|
||||
data = {"tickets": [ticket.serialize() for ticket in tickets]}
|
||||
|
||||
return JsonResponse(data)
|
||||
|
@ -335,6 +335,7 @@
|
||||
{% block javascripts %}
|
||||
<script>
|
||||
var displayedTicketID = -1;
|
||||
filters = {};
|
||||
|
||||
$(document).ready(function() {
|
||||
// $(".email-list-item").on("click", function() {
|
||||
@ -347,6 +348,59 @@
|
||||
console.error(error)
|
||||
});
|
||||
|
||||
$("#filterSidebar .filter-department").each(function() {
|
||||
var uuid = $(this).data("uuid");
|
||||
var checkbox = $(this).find("input[type=checkbox]");
|
||||
checkbox.on("change", function() {
|
||||
var departmentKey = "author__department__in";
|
||||
|
||||
if ($(this).is(":checked")) {
|
||||
if (!filters.hasOwnProperty(departmentKey)) {
|
||||
filters[departmentKey] = [uuid];
|
||||
}
|
||||
else {
|
||||
filters[departmentKey].push(uuid);
|
||||
}
|
||||
}
|
||||
else {
|
||||
filters[departmentKey].splice(filters[departmentKey].indexOf(uuid), 1);
|
||||
|
||||
if (filters.hasOwnProperty(departmentKey) && filters[departmentKey].length === 0) {
|
||||
delete filters[departmentKey]
|
||||
}
|
||||
}
|
||||
|
||||
loadAllTickets();
|
||||
alert(JSON.stringify(filters, null, 4));
|
||||
});
|
||||
});
|
||||
|
||||
$("#filterSidebar .filter-tag").each(function() {
|
||||
var uuid = $(this).data("uuid");
|
||||
var checkbox = $(this).find("input[type=checkbox]");
|
||||
checkbox.on("change", function() {
|
||||
var departmentKey = "tags__in";
|
||||
|
||||
if ($(this).is(":checked")) {
|
||||
if (!filters.hasOwnProperty(departmentKey)) {
|
||||
filters[departmentKey] = [uuid];
|
||||
}
|
||||
else {
|
||||
filters[departmentKey].push(uuid);
|
||||
}
|
||||
}
|
||||
else {
|
||||
filters[departmentKey].splice(filters[departmentKey].indexOf(uuid), 1);
|
||||
|
||||
if (filters.hasOwnProperty(departmentKey) && filters[departmentKey].length === 0) {
|
||||
delete filters[departmentKey]
|
||||
}
|
||||
}
|
||||
|
||||
loadAllTickets();
|
||||
});
|
||||
});
|
||||
|
||||
loadFilterCounts();
|
||||
loadAllTickets();
|
||||
});
|
||||
@ -378,9 +432,15 @@
|
||||
}
|
||||
}
|
||||
|
||||
function loadFilterCounts() {
|
||||
// $("#filterSidebar")
|
||||
function updateFilterCounts(filterType, data) {
|
||||
$("#filterSidebar .filter-" + filterType).each(function() {
|
||||
var uuid = $(this).data("uuid");
|
||||
var count = data[filterType + '_counts'][uuid];
|
||||
$(this).find(".badge").text(count);
|
||||
});
|
||||
}
|
||||
|
||||
function loadFilterCounts() {
|
||||
$.ajax({
|
||||
url: "{% url 'ticket-getfiltercounts' %}",
|
||||
type: "POST",
|
||||
@ -389,29 +449,11 @@
|
||||
csrfmiddlewaretoken: "{{ csrf_token }}",
|
||||
},
|
||||
success: function(data) {
|
||||
alert(JSON.stringify(data, null, 4));
|
||||
console.log(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);
|
||||
});
|
||||
updateFilterCounts('priority', data);
|
||||
updateFilterCounts('tag', data);
|
||||
updateFilterCounts('department', data);
|
||||
},
|
||||
error: function(data) {
|
||||
alert(JSON.stringify(data, null, 4))
|
||||
@ -428,7 +470,7 @@
|
||||
dataType: "json",
|
||||
data: {
|
||||
csrfmiddlewaretoken: "{{ csrf_token }}",
|
||||
filters: {}
|
||||
filters: JSON.stringify(filters)
|
||||
},
|
||||
success: function(data) {
|
||||
console.log(JSON.stringify(data, null, 4))
|
||||
|
Loading…
x
Reference in New Issue
Block a user