Commit a36b1fdc93f6ba4b5482f02b2df7901a1fb10ba5

Authored by Zambom
1 parent 277a890b

Adjusts in notification search

notifications/templates/notifications/_history.html
... ... @@ -10,7 +10,7 @@
10 10 <div class="form-group">
11 11 <label class="col-md-4 history-control-label control-label">{% trans 'Search' %}:</label>
12 12 <div class="col-md-8">
13   - <input type="text" class="form-control" name="search" placeholder="{% trans 'Search...' %}" />
  13 + <input type="text" class="form-control" name="search" value="{{ searched }}" placeholder="{% trans 'Search...' %}" />
14 14 </div>
15 15 </div>
16 16 </form>
... ...
notifications/utils.py
1 1 from datetime import date
2 2 from django.utils import timezone
3 3 from django.db.models import Q
  4 +from dateutil.parser import parse
4 5  
5 6 from log.models import Log
6 7 from pendencies.models import Pendencies
... ... @@ -96,3 +97,10 @@ def get_order_by(order):
96 97 return ["-meta"]
97 98 else:
98 99 return ["meta"]
  100 +
  101 +def is_date(string):
  102 + try:
  103 + parse(string)
  104 + return True
  105 + except ValueError:
  106 + return False
99 107 \ No newline at end of file
... ...
notifications/views.py
... ... @@ -17,7 +17,7 @@ from amadeus.permissions import has_subject_view_permissions
17 17 from subjects.models import Subject
18 18  
19 19 from .models import Notification
20   -from .utils import get_order_by
  20 +from .utils import get_order_by, is_date
21 21  
22 22 class SubjectNotifications(LoginRequiredMixin, generic.ListView):
23 23 login_url = reverse_lazy("users:login")
... ... @@ -90,7 +90,23 @@ class SubjectHistory(LoginRequiredMixin, generic.ListView):
90 90 self.total = notifications.filter(creation_date = datetime.now()).count()
91 91  
92 92 if search:
93   - notifications = notifications.filter(Q(task__resource__name__icontains = search)).order_by(*order)
  93 + queries = Q(task__resource__name__icontains = search)
  94 + queries |= Q(task__action__icontains = search)
  95 +
  96 + if search.isdigit():
  97 + queries |= Q(level = search)
  98 +
  99 + if is_date(search):
  100 + search_date = parser.parse(search)
  101 + search_date = timezone.make_aware(search_date, timezone.get_current_timezone())
  102 +
  103 + queries |= Q(creation_date = search_date)
  104 + queries |= Q(task__limit_date = search_date)
  105 + queries |= Q(task__end_date = search_date)
  106 + queries |= Q(meta__date = search_date)
  107 +
  108 +
  109 + notifications = notifications.filter(queries).order_by(*order)
94 110  
95 111 self.num_rows = notifications.count()
96 112  
... ... @@ -107,6 +123,7 @@ class SubjectHistory(LoginRequiredMixin, generic.ListView):
107 123 context['history'] = True
108 124 context['total'] = self.total
109 125 context['rows'] = self.num_rows
  126 + context['searched'] = self.request.GET.get("search", "")
110 127  
111 128 return context
112 129  
... ...