diff --git a/helpdesk/lib.py b/helpdesk/lib.py index 3a84596a..83c3b525 100644 --- a/helpdesk/lib.py +++ b/helpdesk/lib.py @@ -38,7 +38,11 @@ def query_from_base64(b64data): """ Converts base64-encoded bytes object back to a query dict object. """ - return json.loads(b64decode(b64data).decode('utf-8')) + query = {'search_string': ''} + query.update(json.loads(b64decode(b64data).decode('utf-8'))) + if query['search_string'] is None: + query['search_string'] = '' + return query def query_to_dict(results, descriptions): @@ -81,7 +85,7 @@ def apply_query(queryset, params): filter = {key: params['filtering'][key]} queryset = queryset.filter(**filter) - search = params.get('search_string', None) + search = params.get('search_string', '') if search: qset = ( Q(title__icontains=search) | @@ -257,7 +261,7 @@ def query_tickets_by_args(objects, order_by, **kwargs): function filters existing dataset on search string and returns a filtered filtered list. The `draw`, `length` etc parameters are for datatables to display meta data on the table contents. The returning queryset is passed - to a Serializer called TicketSerializer in serializers.py. + to a Serializer called DatatablesTicketSerializer in serializers.py. """ draw = int(kwargs.get('draw', None)[0]) length = int(kwargs.get('length', None)[0]) diff --git a/helpdesk/serializers.py b/helpdesk/serializers.py index bb7fca5d..c3b311b5 100644 --- a/helpdesk/serializers.py +++ b/helpdesk/serializers.py @@ -12,7 +12,7 @@ datatables for ticket_list.html. Called from staff.datatables_ticket_list. """ -class TicketSerializer(serializers.ModelSerializer): +class DatatablesTicketSerializer(serializers.ModelSerializer): ticket = serializers.SerializerMethodField() assigned_to = serializers.SerializerMethodField() created = serializers.SerializerMethodField() diff --git a/helpdesk/templates/helpdesk/filters/keywords.html b/helpdesk/templates/helpdesk/filters/keywords.html index e46a17ab..60af3fd6 100644 --- a/helpdesk/templates/helpdesk/filters/keywords.html +++ b/helpdesk/templates/helpdesk/filters/keywords.html @@ -4,7 +4,7 @@
Note: Your keyword search is case sensitive ' 'because of your database. This means the search will not ' @@ -965,13 +956,6 @@ def ticket_list(request): '' 'Django Documentation on string matching in SQLite.') - import json - from helpdesk.lib import query_to_base64 - urlsafe_query = query_to_base64(query_params) - - user_saved_queries = SavedSearch.objects.filter(Q(user=request.user) | Q(shared__exact=True)) - - cache.set('ticket_qs', ticket_qs) return render(request, 'helpdesk/ticket_list.html', dict( context, @@ -982,7 +966,7 @@ def ticket_list(request): urlsafe_query=urlsafe_query, user_saved_queries=user_saved_queries, query_params=query_params, - from_saved_query=from_saved_query, + from_saved_query=saved_query is not None, saved_query=saved_query, search_message=search_message, )) @@ -991,18 +975,43 @@ def ticket_list(request): ticket_list = staff_member_required(ticket_list) +class QueryLoadError(Exception): + pass + + +def load_saved_query(request, query_params=None): + saved_query = None + + if request.GET.get('saved_query', None): + try: + saved_query = SavedSearch.objects.get(pk=request.GET.get('saved_query')) + except SavedSearch.DoesNotExist: + raise QueryLoadError() + if not (saved_query.shared or saved_query.user == request.user): + raise QueryLoadError() + + try: + # we get a string like: b'stuff' + # so leave of the first two chars (b') and last (') + b64query = saved_query.query[2:-1] + query_params = query_from_base64(b64query) + except json.JSONDecodeError: + raise QueryLoadError() + return (saved_query, query_params) + + @helpdesk_staff_member_required -@api_view(['GET', 'POST']) +@api_view(['GET']) def datatables_ticket_list(request): """ Datatable on ticket_list.html uses this view from to get objects to display - on the table. query_tickets_by_args is at lib.py, TicketSerializer is in + on the table. query_tickets_by_args is at lib.py, DatatablesTicketSerializer is in serializers.py. The serializers and this view use django-rest_framework methods """ try: objects = cache.get('ticket_qs') model_object = query_tickets_by_args(objects, '-date_created', **request.query_params) - serializer = TicketSerializer(model_object['items'], many=True) + serializer = DatatablesTicketSerializer(model_object['items'], many=True) result = dict() result['data'] = serializer.data result['draw'] = model_object['draw'] @@ -1188,28 +1197,12 @@ def run_report(request, report): queue__in=_get_user_queues(request.user) ) - from_saved_query = False - saved_query = None + try: + saved_query, query_params = load_saved_query(request) + except QueryLoadError: + return HttpResponseRedirect(reverse('helpdesk:report_index')) if request.GET.get('saved_query', None): - from_saved_query = True - try: - saved_query = SavedSearch.objects.get(pk=request.GET.get('saved_query')) - except SavedSearch.DoesNotExist: - return HttpResponseRedirect(reverse('helpdesk:report_index')) - if not (saved_query.shared or saved_query.user == request.user): - return HttpResponseRedirect(reverse('helpdesk:report_index')) - - import json - from helpdesk.lib import query_from_base64 - try: - # we get a string like: b'stuff' - # so leave of the first two chars (b') and last (') - b64query = saved_query.query[2:-1] - query_params = query_from_base64(b64query) - except json.JSONDecodeError: - return HttpResponseRedirect(reverse('helpdesk:report_index')) - report_queryset = apply_query(report_queryset, query_params) from collections import defaultdict @@ -1372,7 +1365,7 @@ def run_report(request, report): 'headings': column_headings, 'series_names': series_names, 'morrisjs_data': morrisjs_data, - 'from_saved_query': from_saved_query, + 'from_saved_query': saved_query is not None, 'saved_query': saved_query, })