summaryrefslogtreecommitdiff
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
commit3399828d16f12caecf6eed8acd2869d5f20f1817 (patch)
treea17fbf56ae06fc3038abc22ccbfd1f15ada1f070
parente7165c5915d5ecddf1248f0cbfb03e5efccd45a7 (diff)
downloadIshtar-3399828d16f12caecf6eed8acd2869d5f20f1817.tar.bz2
Ishtar-3399828d16f12caecf6eed8acd2869d5f20f1817.zip
Search: "today" keyword for dates with '-' and '+' days - add find criteria
-rw-r--r--archaeological_finds/forms.py5
-rw-r--r--archaeological_finds/models_finds.py6
-rw-r--r--ishtar_common/views_item.py53
3 files changed, 57 insertions, 7 deletions
diff --git a/archaeological_finds/forms.py b/archaeological_finds/forms.py
index 3019d4aa2..66f99d08b 100644
--- a/archaeological_finds/forms.py
+++ b/archaeological_finds/forms.py
@@ -883,8 +883,11 @@ class FindSelect(HistorySelect):
base_finds__batch = forms.ChoiceField(
label=_(u"Batch/object"), choices=[])
checked_type = forms.ChoiceField(label=_("Check"))
- loan = forms.NullBooleanField(label=_(u"Loan?"))
documents__image__isnull = forms.NullBooleanField(label=_(u"Has an image?"))
+ loan = forms.NullBooleanField(label=_(u"Loan?"))
+ treatments_file_end_date = forms.DateField(
+ label=_(u"Treatment file end date before"), widget=DatePicker
+ )
TYPES = [
FieldType('datings__period', Period),
diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py
index 08fb15f0e..3ee7d12c7 100644
--- a/archaeological_finds/models_finds.py
+++ b/archaeological_finds/models_finds.py
@@ -759,7 +759,7 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, OwnPerms,
'base_finds__context_record__',
}
- DATED_FIELDS = ['last_modified__gte']
+ DATED_FIELDS = ['last_modified__gte', 'treatments__file__end_date__lte']
BASE_REQUEST = {'downstream_treatment__isnull': True}
EXTRA_REQUEST_KEYS = {
'base_finds__context_record':
@@ -971,6 +971,10 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, OwnPerms,
'loan': (
pgettext_lazy("key for text search", u"loan"),
query_loan
+ ),
+ 'treatments_file_end_date': (
+ pgettext_lazy("key for text search", u"treatment-end-date-before"),
+ 'treatments__file__end_date__lte'
)
}
for v in ALT_NAMES.values():
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)