From 797ad778a9683d37cc19946c2f2aeb0a34fa0d02 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Sat, 29 Feb 2020 11:57:33 +0000 Subject: [PATCH] Improve search logic and allow search of event archive (#248) * Added search to person, venue, organisation and event archive * Added search to invoice archive * Added event search to homepage * Tidy up event search logic and optimise * Fixed merge issues * Stopped 404 on failed search * Set default ordering of people, organisations & venues to alphabetical (rather than order of addition to database) * Added invoice search to home page (if you have permissions) * Made invoice archive sort by reverse invoice date (rather than order added to database) * Added search help page (very pretty) * Made single search box for all search types * FIX: Missing date field breaking archive view * FEAT: Add omnisearch to header Tis a bit broken on mobile at the moment... * CHORE: Conform old code to pep8 * FIX: Select the event form, not the search one in tests! * Revert "FEAT: Add omnisearch to header" This reverts commit 6bcb242d6be25caef7f9c2d8619e12faa4f22b3b because it caused MANY more problems than anticipated... * FIX: Stop 404 on failed search, again * FEAT: Basic testing of search * Use a tooltip to help explain the UX Obviously since it needs a tooltip it isn't brilliant UX but the best I can think of for now... Co-authored-by: Tom Price Co-authored-by: David Taylor Co-authored-by: Arona Jones --- RIGS/finance.py | 29 +++++ RIGS/rigboard.py | 47 ++++++-- RIGS/templates/RIGS/event_archive.html | 53 +++++---- RIGS/templates/RIGS/index.html | 64 ++++++----- RIGS/templates/RIGS/invoice_list.html | 1 + RIGS/templates/RIGS/invoice_list_archive.html | 11 ++ RIGS/templates/RIGS/search_help.html | 70 ++++++++++++ RIGS/test_functional.py | 60 ++++++++-- RIGS/test_unit.py | 104 ++++++++++++++++++ RIGS/urls.py | 2 + RIGS/views.py | 61 +++++++--- 11 files changed, 423 insertions(+), 79 deletions(-) create mode 100644 RIGS/templates/RIGS/search_help.html diff --git a/RIGS/finance.py b/RIGS/finance.py index b64b2e71..d536969b 100644 --- a/RIGS/finance.py +++ b/RIGS/finance.py @@ -11,6 +11,7 @@ from django.template.loader import get_template from django.views import generic from django.db.models import Q from z3c.rml import rml2pdf +from django.db.models import Q from RIGS import models @@ -122,6 +123,34 @@ class InvoiceArchive(generic.ListView): template_name = 'RIGS/invoice_list_archive.html' paginate_by = 25 + def get_queryset(self): + q = self.request.GET.get('q', "") + + filter = Q(event__name__icontains=q) + + # try and parse an int + try: + val = int(q) + filter = filter | Q(pk=val) + filter = filter | Q(event__pk=val) + except: # noqa + # not an integer + pass + + try: + if q[0] == "N": + val = int(q[1:]) + filter = Q(event__pk=val) # If string is Nxxxxx then filter by event number + elif q[0] == "#": + val = int(q[1:]) + filter = Q(pk=val) # If string is #xxxxx then filter by invoice number + except: # noqa + pass + + object_list = self.model.objects.filter(filter).order_by('-invoice_date') + + return object_list + class InvoiceWaiting(generic.ListView): model = models.Event diff --git a/RIGS/rigboard.py b/RIGS/rigboard.py index abb0ed9e..ae960f4f 100644 --- a/RIGS/rigboard.py +++ b/RIGS/rigboard.py @@ -226,10 +226,18 @@ class EventPrint(generic.View): return response -class EventArchive(generic.ArchiveIndexView): +class EventArchive(generic.ListView): model = models.Event - date_field = "start_date" paginate_by = 25 + template_name = "RIGS/event_archive.html" + + def get_context_data(self, **kwargs): + # get super context + context = super(EventArchive, self).get_context_data(**kwargs) + + context['start'] = self.request.GET.get('start', None) + context['end'] = self.request.GET.get('end', datetime.date.today().strftime('%Y-%m-%d')) + return context def get_queryset(self): start = self.request.GET.get('start', None) @@ -241,19 +249,34 @@ class EventArchive(generic.ArchiveIndexView): "Muppet! Check the dates, it has been fixed for you.") start, end = end, start # Stop the impending fail - filter = False + filter = Q() if end != "": - filter = Q(start_date__lte=end) + filter &= Q(start_date__lte=end) if start: - if filter: - filter = filter & Q(start_date__gte=start) - else: - filter = Q(start_date__gte=start) + filter &= Q(start_date__gte=start) - if filter: - qs = self.model.objects.filter(filter).order_by('-start_date') - else: - qs = self.model.objects.all().order_by('-start_date') + q = self.request.GET.get('q', "") + + if q is not "": + qfilter = Q(name__icontains=q) | Q(description__icontains=q) | Q(notes__icontains=q) + + # try and parse an int + try: + val = int(q) + qfilter = qfilter | Q(pk=val) + except: # noqa not an integer + pass + + try: + if q[0] == "N": + val = int(q[1:]) + qfilter = Q(pk=val) # If string is N###### then do a simple PK filter + except: # noqa + pass + + filter &= qfilter + + qs = self.model.objects.filter(filter).order_by('-start_date') # Preselect related for efficiency qs.select_related('person', 'organisation', 'venue', 'mic') diff --git a/RIGS/templates/RIGS/event_archive.html b/RIGS/templates/RIGS/event_archive.html index 4fc5642e..18459073 100644 --- a/RIGS/templates/RIGS/event_archive.html +++ b/RIGS/templates/RIGS/event_archive.html @@ -5,34 +5,49 @@ {% block content %}
-

Event Archive

- -