diff --git a/helpdesk/query.py b/helpdesk/query.py index 869e800a..c1808594 100644 --- a/helpdesk/query.py +++ b/helpdesk/query.py @@ -164,12 +164,12 @@ class __Query__: """ objects = self.get() order_by = '-date_created' - draw = int(kwargs.get('draw', None)[0]) - length = int(kwargs.get('length', None)[0]) - start = int(kwargs.get('start', None)[0]) - search_value = kwargs.get('search[value]', None)[0] - order_column = kwargs.get('order[0][column]', None)[0] - order = kwargs.get('order[0][dir]', None)[0] + draw = int(kwargs.get('draw', [0])[0]) + length = int(kwargs.get('length', [25])[0]) + start = int(kwargs.get('start', [0])[0]) + search_value = kwargs.get('search[value]', [""])[0] + order_column = kwargs.get('order[0][column]', ['5'])[0] + order = kwargs.get('order[0][dir]', ["asc"])[0] order_column = DATATABLES_ORDER_COLUMN_CHOICES[order_column] # django orm '-' -> desc diff --git a/helpdesk/tests/test_query.py b/helpdesk/tests/test_query.py new file mode 100644 index 00000000..c722c723 --- /dev/null +++ b/helpdesk/tests/test_query.py @@ -0,0 +1,103 @@ +from django.contrib.auth import get_user_model +from django.test import TestCase +from django.urls import reverse + +from helpdesk.models import KBCategory, KBItem, Queue, Ticket +from helpdesk.query import query_to_base64 + +from helpdesk.tests.helpers import (get_staff_user, reload_urlconf, User, create_ticket, print_response) + + +class QueryTests(TestCase): + def setUp(self): + self.queue = Queue.objects.create( + title="Test queue", + slug="test_queue", + allow_public_submission=True, + ) + self.queue.save() + cat = KBCategory.objects.create( + title="Test Cat", + slug="test_cat", + description="This is a test category", + queue=self.queue, + ) + cat.save() + self.kbitem1 = KBItem.objects.create( + category=cat, + title="KBItem 1", + question="What?", + answer="A KB Item", + ) + self.user = get_staff_user() + self.ticket1 = Ticket.objects.create( + title="unassigned to kbitem", + queue=self.queue, + description="lol", + ) + self.ticket1.save() + self.ticket2 = Ticket.objects.create( + title="assigned to kbitem", + queue=self.queue, + description="lol", + kbitem=self.kbitem1, + ) + self.ticket2.save() + + def loginUser(self, is_staff=True): + """Create a staff user and login""" + User = get_user_model() + self.user = User.objects.create( + username='User_1', + is_staff=is_staff, + ) + self.user.set_password('pass') + self.user.save() + self.client.login(username='User_1', password='pass') + + def test_query_basic(self): + self.loginUser() + query = query_to_base64({}) + response = self.client.get(reverse('helpdesk:datatables_ticket_list', args=[query])) + self.assertEqual( + response.json(), + {"data": + [{"ticket": "1 [test_queue-1]", "id": 1, "priority": 3, "title": "unassigned to kbitem", "queue": {"title": "Test queue", "id": 1}, "status": "Open", "created": "now", "due_date": None, "assigned_to": "None", "submitter": None, "row_class": "", "time_spent": "", "kbitem": ""}, + {"ticket": "2 [test_queue-2]", "id": 2, "priority": 3, "title": "assigned to kbitem", "queue": {"title": "Test queue", "id": 1}, "status": "Open", "created": "now", "due_date": None, "assigned_to": "None", "submitter": None, "row_class": "", "time_spent": "", "kbitem": "KBItem 1"}], + "recordsFiltered": 2, + "recordsTotal": 2, + "draw": 0, + }, + ) + + def test_query_by_kbitem(self): + self.loginUser() + query = query_to_base64( + {'filtering': {'kbitem__in': [self.kbitem1.pk]}} + ) + response = self.client.get(reverse('helpdesk:datatables_ticket_list', args=[query])) + self.assertEqual( + response.json(), + {"data": + [{"ticket": "2 [test_queue-2]", "id": 2, "priority": 3, "title": "assigned to kbitem", "queue": {"title": "Test queue", "id": 1}, "status": "Open", "created": "now", "due_date": None, "assigned_to": "None", "submitter": None, "row_class": "", "time_spent": "", "kbitem": "KBItem 1"}], + "recordsFiltered": 1, + "recordsTotal": 1, + "draw": 0, + }, + ) + + def test_query_by_no_kbitem(self): + self.loginUser() + query = query_to_base64( + {'filtering_or': {'kbitem__in': [self.kbitem1.pk]}} + ) + response = self.client.get(reverse('helpdesk:datatables_ticket_list', args=[query])) + self.assertEqual( + response.json(), + {"data": + [{"ticket": "2 [test_queue-2]", "id": 2, "priority": 3, "title": "assigned to kbitem", "queue": {"title": "Test queue", "id": 1}, "status": "Open", "created": "now", "due_date": None, "assigned_to": "None", "submitter": None, "row_class": "", "time_spent": "", "kbitem": "KBItem 1"}], + "recordsFiltered": 1, + "recordsTotal": 1, + "draw": 0, + }, + ) diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 78869041..80c9dc69 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -898,6 +898,8 @@ def ticket_list(request): pattern_pks = [int(pattern) for pattern in patterns] if -1 in pattern_pks: query_params['filtering_or'][filter_null_params[param]] = True + else: + query_params['filtering_or'][filter_command] = pattern_pks query_params['filtering'][filter_command] = pattern_pks except ValueError: pass diff --git a/quicktest.py b/quicktest.py index c7912124..be829453 100644 --- a/quicktest.py +++ b/quicktest.py @@ -28,7 +28,11 @@ class QuickDjangoTest(object): 'django.contrib.sites', 'django.contrib.staticfiles', 'bootstrap4form', + 'account', + 'pinax.invitations', + 'pinax.teams', 'helpdesk', + 'reversion', ) MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware',