summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-12-03 01:46:28 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2018-12-03 01:46:28 +0100
commitec4a9c91cbdfdfb904a123fc0270e8bf3e951ed2 (patch)
treea17fbf56ae06fc3038abc22ccbfd1f15ada1f070 /ishtar_common
parent4d701751d0eba67cceb6e9f435e03f7142b1f8fc (diff)
downloadIshtar-ec4a9c91cbdfdfb904a123fc0270e8bf3e951ed2.tar.bz2
Ishtar-ec4a9c91cbdfdfb904a123fc0270e8bf3e951ed2.zip
Search: "today" keyword for dates with '-' and '+' days - add find criteria
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/views_item.py53
1 files changed, 48 insertions, 5 deletions
diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py
index 85db87605..1203d717d 100644
--- a/ishtar_common/views_item.py
+++ b/ishtar_common/views_item.py
@@ -22,7 +22,8 @@ from django.db.models.fields import FieldDoesNotExist
from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader
-from django.utils.translation import ugettext, ugettext_lazy as _
+from django.utils.translation import ugettext, ugettext_lazy as _, \
+ activate, deactivate, pgettext_lazy
from tidylib import tidy_document as tidy
from unidecode import unidecode
from weasyprint import HTML, CSS
@@ -524,6 +525,8 @@ def _search_manage_search_vector(model, dct, exc_dct, request_keys):
search_query = \
search_query.replace(u'(', u'').replace(u')', u'').strip()
if search_query:
+ if 'extras' not in dct:
+ dct['extras'] = []
dct['extras'].append(
{'where': [model._meta.db_table +
".search_vector @@ (to_tsquery(%s, %s)) = true"],
@@ -563,13 +566,49 @@ def _manage_bool_fields(model, bool_fields, reversed_bool_fields, dct, or_reqs):
pass
+today_lbl = pgettext_lazy("key for text search", u"today"),
+TODAYS = ['today']
+
+for language_code, language_lbl in settings.LANGUAGES:
+ activate(language_code)
+ TODAYS.append(unicode(today_lbl))
+ deactivate()
+
+
def _manage_dated_fields(dated_fields, dct):
for k in dated_fields:
if k in dct:
if not dct[k]:
dct.pop(k)
+ continue
+ value = dct[k].replace('"', '').strip()
+ has_today = False
+ for today in TODAYS:
+ if value.startswith(today):
+ base_date = datetime.date.today()
+ value = value[len(today):].replace(' ', '')
+ if value and value[0] in (u"-", u"+"):
+ sign = value[0]
+ try:
+ days = int(value[1:])
+ except ValueError:
+ days = 0
+ print(days)
+ if days:
+ if sign == u"-":
+ base_date = base_date - datetime.timedelta(
+ days=days)
+ else:
+ base_date = base_date + datetime.timedelta(
+ days=days)
+ dct[k] = base_date.strftime('%Y-%m-%d')
+ print(dct[k])
+ has_today = True
+ break
+ if has_today:
+ continue
try:
- items = dct[k].replace('"', '').split('/')
+ items = value.split('/')
assert len(items) == 3
dct[k] = virtualtime.datetime(*map(lambda x: int(x),
reversed(items))) \
@@ -1176,9 +1215,13 @@ def get_item(model, func_name, default_name, extra_request_keys=None,
_manage_facet_search(model, dct, and_reqs)
_manage_facet_search(model, excluded_dct, exc_and_reqs)
- extras = dct.pop('extras')
- and_reqs += dct.pop('and_reqs')
- exc_and_reqs += dct.pop('exc_and_reqs')
+ extras = []
+ if 'extras' in dct:
+ extras = dct.pop('extras')
+ if 'and_reqs' in dct:
+ and_reqs += dct.pop('and_reqs')
+ if 'exc_and_reqs' in dct:
+ exc_and_reqs += dct.pop('exc_and_reqs')
_manage_clean_search_field(dct)
_manage_clean_search_field(excluded_dct)