diff --git a/ckanext/odsh/helpers.py b/ckanext/odsh/helpers.py index 09f81a96938d7027bd303b6fe23c3b3dd1282b5f..2cefbd48495ecc10af3db7d66d97e0b414cd1a0b 100644 --- a/ckanext/odsh/helpers.py +++ b/ckanext/odsh/helpers.py @@ -106,8 +106,17 @@ def odsh_get_spatial_text(pkg_dict): def odsh_render_datetime(datetime_, date_format='{0.day:02d}.{0.month:02d}.{0.year:4d}'): - dt = parser.parse(datetime_) - return date_format.format(dt) + if not datetime_: + return '' + try: + DATETIME_FORMAT = '%Y-%m-%d' + dt = datetime.datetime.strptime( + datetime_, DATETIME_FORMAT) + return dt.strftime('%d.%m.%Y') + # dt = parser.parse(datetime_, dayfirst=False) + # return date_format.format(dt) + except: + return '' def odsh_upload_known_formats(): diff --git a/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.mo b/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.mo index fd674326210052b3d4a7aca6618bab63dcb260d8..fad2be93b305da5ac2ae00cffa567377b95df015 100644 Binary files a/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.mo and b/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.mo differ diff --git a/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.po b/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.po index 95e251720cd1c7c630d52d8e65fe6acf0953faf7..84f15ec8e0b894373f015fc18c252d999cb7e649 100644 --- a/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.po +++ b/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.po @@ -288,4 +288,10 @@ msgid "map search active" msgstr "Kartensuche aktiv" msgid "map" -msgstr "Karte" \ No newline at end of file +msgstr "Karte" + +msgid "wrong_start_date_for_search" +msgstr "Das Datum ist ungültig" + +msgid "wrong_end_date_for_search" +msgstr "Das Datum ist ungültig" diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py index b5e43d38f49a8087565f53873fc7bc79eae5b564..e916fdfcbdd289e1c849df3ed32f679ffaf052c9 100644 --- a/ckanext/odsh/plugin.py +++ b/ckanext/odsh/plugin.py @@ -99,7 +99,8 @@ def known_spatial_uri(key, data, errors, context): spatial = json.dumps(loaded['geometry']) break if not_found: - raise toolkit.Invalid('spatial_uri:odsh_spatial_uri_unknown_error_label') + raise toolkit.Invalid( + 'spatial_uri:odsh_spatial_uri_unknown_error_label') # Get the current extras index current_indexes = [k[1] for k in data.keys() @@ -137,7 +138,8 @@ def odsh_validate_extra_date(key, field, data, errors, context): try: # date.split('T')[0] will yield "2012-01-01" # no matter if the date is like "2012-01-01" or "2012-01-01T00:00:00" - datetime.datetime.strptime(value.split('T')[0],'%Y-%m-%d').isoformat() + datetime.datetime.strptime( + value.split('T')[0], '%Y-%m-%d').isoformat() except ValueError: raise toolkit.Invalid(field+':odsh_'+field+'_not_date_error_label') @@ -370,10 +372,15 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm # There are no extras in the search params, so do nothing. return search_params - start_date = self.extend_search_convert_local_to_utc_timestamp( - extras.get('ext_startdate')) - end_date = self.extend_search_convert_local_to_utc_timestamp( - extras.get('ext_enddate')) + start_date=None + end_date=None + try: + start_date = self.extend_search_convert_local_to_utc_timestamp( + extras.get('ext_startdate')) + end_date = self.extend_search_convert_local_to_utc_timestamp( + extras.get('ext_enddate')) + except ValueError: + return search_params if not start_date and not end_date: return search_params diff --git a/ckanext/odsh/public/odsh.css b/ckanext/odsh/public/odsh.css index ce8616e684baca6901b66252253cdf05a5a5cb71..0edcf7b61fd58b58556272417c80ae462053d4b8 100644 --- a/ckanext/odsh/public/odsh.css +++ b/ckanext/odsh/public/odsh.css @@ -1424,4 +1424,11 @@ display: none; .required-text{ font-size: 14px; -} \ No newline at end of file +} + +.error-reange-search{ + color: #d4004b; + margin-top: -5px; + font-size: 12px; + margin-bottom: 5px; +} diff --git a/ckanext/odsh/templates/package/search.html b/ckanext/odsh/templates/package/search.html index 299ae586b6819ceb27d069cbfb7df9ae5b336a25..14ff1f517d788c6979fca30481165f093588b20b 100644 --- a/ckanext/odsh/templates/package/search.html +++ b/ckanext/odsh/templates/package/search.html @@ -39,16 +39,28 @@ default_extent="{ \"type\": \"Polygon\", \"coordinates\": [[[7.6574,53.1632],[11 <form id="date-search-form" method="get" action="{% url_for controller='package', action='search' %}"> <div class='controls rangesearch'> {%- set start = request.params['ext_startdate'] -%} + {%- set start_error = h.odsh_render_datetime(start) == '' and start -%} {%- set end = request.params['ext_enddate'] -%} - {%- set typeStart = 'text' if not start else 'date' -%} - {%- set typeEnd = 'text' if not end else 'date' -%} + {%- set end_error = h.odsh_render_datetime(end) == '' and end -%} + {%- set typeStart = 'text' if (start_error or not start) else 'date' -%} + {%- set typeEnd = 'text' if (end_error or not end) else 'date' -%} {%- set onFocus = 'onfocus=(this.type=\'date\')' -%} <label for="ext_startdate">{{_('from')|title}}</label> <input id="ext_startdate" name="ext_startdate" type="{{ typeStart }}" value="{{start}}" placeholder="{{ _('Date') }}" {{ onFocus }} class='rangesearch' /> + {%if start_error %} + <div class="error-reange-search"> + {{_('wrong_start_date_for_search')}} + </div> + {%endif%} <label for="ext_enddate">{{_('to')|title}}</label> <input id="ext_enddate" name="ext_enddate" type="{{ typeEnd }}" value="{{end}}" placeholder="{{ _('Date') }}" {{ onFocus }} class='rangesearch' /> + {%if end_error %} + <div class="error-reange-search"> + {{_('wrong_end_date_for_search')}} + </div> + {%endif%} <a class='pull-right' href="javascript:{}" onclick="$('#date-search-form').submit();" class="action">{{_('submit date search') }}</a> </div> </form> diff --git a/ckanext/odsh/templates/snippets/search_form.html b/ckanext/odsh/templates/snippets/search_form.html index aa97eaf98ea94e72be4e46597bb6ccc3e8280e21..8e3add90ca3e35aaefc6a5ef455729d370296203 100644 --- a/ckanext/odsh/templates/snippets/search_form.html +++ b/ckanext/odsh/templates/snippets/search_form.html @@ -73,20 +73,22 @@ {% endif %} {% endif %} <div id="datesearch-filter"> - {%set start_date=request.params.get('ext_startdate')%} - {%set end_date=request.params.get('ext_enddate')%} - {% if start_date or end_date %} + {%set start_date=h.odsh_render_datetime(request.params.get('ext_startdate'))%} + {%set end_date=h.odsh_render_datetime(request.params.get('ext_enddate'))%} + {%- set start_error = start_date == '' and request.params.get('ext_startdate')-%} + {%- set end_error = end_date == '' and request.params.get('ext_enddate')-%} + {% if (start_date or end_date) and (not start_error and not end_error)%} <p class="filter-list"> {% if start_date %} <span class="filtered pill"> - {{_('daterange')}}: {{_('from')}} {{ h.odsh_render_datetime(start_date)}} + {{_('daterange')}}: {{_('from')}} {{ start_date}} <a href="{{ h.remove_url_param('ext_startdate') }}" class="remove" title="{{ _('Remove') }}"><i class="fa fa-times"></i></a> </span> {% endif %} {% if end_date %} <span class="filtered pill"> - {{_('daterange')}}: {{_('to')}} {{ h.odsh_render_datetime(end_date)}} + {{_('daterange')}}: {{_('to')}} {{ end_date}} <a href="{{ h.remove_url_param('ext_enddate') }}" class="remove" title="{{ _('Remove') }}"><i class="fa fa-times"></i></a> </span> {% endif %} diff --git a/ckanext/odsh/tests/test_search.py b/ckanext/odsh/tests/test_search.py index a0eb81915003fe0f962205f1ccd2b27c535a34ed..99a3a07c344b833896303455727f25d40d2ebb1c 100644 --- a/ckanext/odsh/tests/test_search.py +++ b/ckanext/odsh/tests/test_search.py @@ -8,11 +8,9 @@ from ckan.lib.mailer import create_reset_key from nose.tools import assert_true, assert_false, assert_equal, assert_in from routes import url_for import ckan.plugins +from test_helpers import odsh_test -def odsh_test(): return helpers.change_config('ckanext.odsh.spatial.mapping', - 'file:///usr/lib/ckan/default/src/ckanext-odsh/ckanext/odsh/tests/spatial_mapping.csv') - class TestSearch(helpers.FunctionalTestBase): @@ -54,6 +52,30 @@ class TestSearch(helpers.FunctionalTestBase): # assert assert dataset['name'] in response + @odsh_test() + def test_query_with_wrong_dates_shows_error(self): + # arrange + dataset = self._create_dataset() + + # act + response1 = self._perform_date_search('foo', None) + response2 = self._perform_date_search(None, 'foo') + response3 = self._perform_date_search('11-11-11', None) + + # assert + assert 'wrong_start_date_for_search' in response1 + self._assert_datasets_in_response([dataset], response1) + assert 'daterange: to' not in response1 + assert 'daterange: from' not in response1 + assert 'wrong_end_date_for_search' in response2 + self._assert_datasets_in_response([dataset], response2) + assert 'daterange: to' not in response2 + assert 'daterange: from' not in response2 + assert 'wrong_start_date_for_search' in response3 + assert 'daterange: to' not in response3 + assert 'daterange: from' not in response3 + self._assert_datasets_in_response([dataset], response3) + @odsh_test() def test_query_with_start_date_finds_one_dataset(self): # arrange @@ -71,14 +93,19 @@ class TestSearch(helpers.FunctionalTestBase): # assert self._assert_datasets_in_response([datasetA, datasetB], response1) self._assert_datasets_not_in_response([datasetC], response1) + assert 'daterange: to' in response1 self._assert_datasets_in_response( [datasetA, datasetB, datasetC], response2) + assert 'daterange: to' in response2 self._assert_no_results(response3) + assert 'daterange: from' in response3 self._assert_datasets_in_response([datasetB], response4) self._assert_datasets_not_in_response([datasetA, datasetC], response4) + assert 'daterange: to' in response4 + assert 'daterange: from' in response4 self._assert_datasets_in_response([datasetC], response5) self._assert_datasets_not_in_response([datasetA, datasetB], response5)