From a36b1fdc93f6ba4b5482f02b2df7901a1fb10ba5 Mon Sep 17 00:00:00 2001 From: Zambom Date: Tue, 31 Jan 2017 16:16:59 -0200 Subject: [PATCH] Adjusts in notification search --- notifications/templates/notifications/_history.html | 2 +- notifications/utils.py | 8 ++++++++ notifications/views.py | 21 +++++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/notifications/templates/notifications/_history.html b/notifications/templates/notifications/_history.html index a9bb6c3..c15dd33 100644 --- a/notifications/templates/notifications/_history.html +++ b/notifications/templates/notifications/_history.html @@ -10,7 +10,7 @@
- +
diff --git a/notifications/utils.py b/notifications/utils.py index 76a4570..6380f65 100644 --- a/notifications/utils.py +++ b/notifications/utils.py @@ -1,6 +1,7 @@ from datetime import date from django.utils import timezone from django.db.models import Q +from dateutil.parser import parse from log.models import Log from pendencies.models import Pendencies @@ -96,3 +97,10 @@ def get_order_by(order): return ["-meta"] else: return ["meta"] + +def is_date(string): + try: + parse(string) + return True + except ValueError: + return False \ No newline at end of file diff --git a/notifications/views.py b/notifications/views.py index 61435f1..22320c1 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -17,7 +17,7 @@ from amadeus.permissions import has_subject_view_permissions from subjects.models import Subject from .models import Notification -from .utils import get_order_by +from .utils import get_order_by, is_date class SubjectNotifications(LoginRequiredMixin, generic.ListView): login_url = reverse_lazy("users:login") @@ -90,7 +90,23 @@ class SubjectHistory(LoginRequiredMixin, generic.ListView): self.total = notifications.filter(creation_date = datetime.now()).count() if search: - notifications = notifications.filter(Q(task__resource__name__icontains = search)).order_by(*order) + queries = Q(task__resource__name__icontains = search) + queries |= Q(task__action__icontains = search) + + if search.isdigit(): + queries |= Q(level = search) + + if is_date(search): + search_date = parser.parse(search) + search_date = timezone.make_aware(search_date, timezone.get_current_timezone()) + + queries |= Q(creation_date = search_date) + queries |= Q(task__limit_date = search_date) + queries |= Q(task__end_date = search_date) + queries |= Q(meta__date = search_date) + + + notifications = notifications.filter(queries).order_by(*order) self.num_rows = notifications.count() @@ -107,6 +123,7 @@ class SubjectHistory(LoginRequiredMixin, generic.ListView): context['history'] = True context['total'] = self.total context['rows'] = self.num_rows + context['searched'] = self.request.GET.get("search", "") return context -- libgit2 0.21.2