From 867d51339c9e2460562da43a5956863cf49f5024 Mon Sep 17 00:00:00 2001
From: anonymous <anonymous>
Date: Tue, 11 Dec 2018 17:12:48 +0100
Subject: [PATCH] ODPSH-5: add date range search

---
 .../odsh/i18n/de/LC_MESSAGES/ckanext-odsh.mo  | Bin 4966 -> 5056 bytes
 .../odsh/i18n/de/LC_MESSAGES/ckanext-odsh.po  |   8 ++-
 ckanext/odsh/plugin.py                        |  54 ++++++++++++++++++
 ckanext/odsh/templates/package/search.html    |  14 ++++-
 .../snippets/package_basic_fields.html        |   6 +-
 .../odsh/templates/snippets/search_form.html  |  21 +++++++
 6 files changed, 98 insertions(+), 5 deletions(-)

diff --git a/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.mo b/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.mo
index 7be3b9dd8e3005b739e5a055e89edb4793ba60ca..952393e23e4bfc2b8588c20b80efe76d2ca4437b 100644
GIT binary patch
delta 1429
zcmaE+c0j%Uo)F7a1_lO(AO;2o83qQ1HB2BL0^6`MFbFX)Fxav(FmN+4Fa)qNFsL#x
zFvPGz)Hks*Ft9Q(FwAFVU|?rpU|0%Ox0aQGfuDhaVJ9mCgE9jH!%0>K1}+8$hPSK?
z41x>{3_n;I7!(*7>KVA$7#JiO7#OtKAQG-@5QX7v5DSyoAQt4YK^#)T#=yYFz`)SX
z#=yYIz`)SQ#=s!Kz`!sAD!!2o;*cFsa}GfDpN7g`WMg39V_;yo!3Oe3Jp;pYHU<V2
z1_lORb_NC!1_lNrb_NDn1_lOqb_NDs1_p*yc8CLu*&!Nxq3Y(aLmaq~oq<7`fq`K!
zI|G9@0|UcNc8JeeIT#qE7#J8tIT#pN7#J7~I2ags7#J8#IUwdZaWF9G)-y0L1adGi
zh%zuR)N??5I+cTgfrEj8VKxWEr%Rw3Rzt;iaWF85GcYh5=73mq8*0I8sJahO@&6o<
z5aQ>Am?y~z30Wmhhy!#v85rt8F0<i;1eqgLp(m6Mf{I6TLL8F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/ckanext/odsh/i18n/de/LC_MESSAGES/ckanext-odsh.po
@@ -266,4 +266,10 @@ msgid "borders of map"
 msgstr "Grenzen der Karte"
 
 msgid "Map showing the borders of {map_text}"
-msgstr "Karte, die die Grenzen von {map_text} zeigt"
\ No newline at end of file
+msgstr "Karte, die die Grenzen von {map_text} zeigt"
+
+msgid "submit date search"
+msgstr "Zeitbezug anwenden"
+
+msgid "daterange"
+msgstr "Zeitbezug"
\ No newline at end of file
diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py
index da8a03fb..bea06ebf 100644
--- a/ckanext/odsh/plugin.py
+++ b/ckanext/odsh/plugin.py
@@ -146,6 +146,7 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
     plugins.implements(plugins.IFacets)
     plugins.implements(plugins.IDatasetForm)
     plugins.implements(plugins.IValidators)
+    plugins.implements(plugins.IPackageController, inherit=True)
 
     # IConfigurer
 
@@ -301,4 +302,57 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
         return { 'odsh_convert_groups_string': odsh_convert_groups_string,
                  'known_spatial_uri': known_spatial_uri,
                  'odsh_tag_name_validator': odsh_tag_name_validator}
+    
+
+    def extend_search_convert_local_to_utc_timestamp(self, str_timestamp):
+        DATETIME_FORMAT = '%Y-%m-%d'
+        if not str_timestamp:
+            return ''
+        
+        ##Todo: do we need timezone conversions?
+
+        local_datetime  = datetime.datetime.strptime(str_timestamp, DATETIME_FORMAT);
+        # tz_code = config.get('ckan.timezone', 'Australia/Melbourne')
+        # local = timezone(tz_code)
+        # utc_datetime = _make_aware(local_datetime, local)
+        # local_datetime = utc_datetime.astimezone(pytz.utc)
+        return local_datetime.strftime(DATETIME_FORMAT)+"T00:00:00Z"
+
+    # Add the custom parameters to Solr's facet queries
+    def before_search(self, search_params):
+
+        extras = search_params.get('extras')
+        if not extras:
+            # There are no extras in the search params, so do nothing.
+            return search_params
+
+
+        print(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'))
+
+        if not start_date and not end_date:
+            # The user didn't select any additional params, so do nothing.
+            return search_params
+
+        if not start_date:
+            start_date='*'
+        if not end_date:
+            end_date='*'
+
+        fq = search_params['fq']
+
+        if start_date and end_date:
+            # Add a date-range query with the selected start and end dates into the
+            # Solr facet queries.
+            print('change queary')
+            fq = '{fq} +metadata_modified:[{start_date} TO {end_date}]'.format(
+                fq=fq, start_date=start_date, end_date=end_date)
+            print(fq)
+
+        #return modified facet queries
+        search_params['fq'] = fq
+
+        return search_params
 
diff --git a/ckanext/odsh/templates/package/search.html b/ckanext/odsh/templates/package/search.html
index 3e23c668..5daf0295 100644
--- a/ckanext/odsh/templates/package/search.html
+++ b/ckanext/odsh/templates/package/search.html
@@ -30,7 +30,19 @@ default_extent="{ \"type\": \"Polygon\", \"coordinates\": [[[7.6574,53.1632],[11
     {{ h.snippet('snippets/facet_list.html', title=c.facet_titles[facet], name=facet) }}
     {% endfor %}
   </div>
-  <a class="close no-text hide-filters"><i class="fa fa-times-circle"></i><span class="text">close</span></a>
+
+  {% block datereange_search %}
+  <form id="date-search-form" method="get" action="{% url_for controller='package', action='search' %}">
+    {{ form.input_raw('ext_startdate', id='ext_startdate', value=request.params['ext_startdate'],
+    classes=['control-full'],type='date',placeholder=_('from'))
+    }}
+    {{ form.input_raw('ext_enddate', id='ext_enddate', value=request.params['ext_enddate'],
+    classes=['control-full'],type='date',placeholder=_('to'))
+    }}
+    <a href="javascript:{}" onclick="$('#date-search-form').submit();" class="action">{{_('submit date search') }}</a>
+  </form>
+  {% endblock %}
+
 </div>
 {% endblock %}
 {% block package_search_results_api %}
diff --git a/ckanext/odsh/templates/package/snippets/package_basic_fields.html b/ckanext/odsh/templates/package/snippets/package_basic_fields.html
index 276525bf..e0b85049 100644
--- a/ckanext/odsh/templates/package/snippets/package_basic_fields.html
+++ b/ckanext/odsh/templates/package/snippets/package_basic_fields.html
@@ -75,7 +75,7 @@ is_required=true,placeholder=_('Enter title')) }}
         <div id='start-end' class='span3'>
             <!-- field temporal_start -->
             {% set temporal_start_label=_('odsh_temporal_start_label') %}
-	    {% set data_temporal_start = data.temporal_start.split('T')[0] if data.temporal_start else None %}
+            {% set data_temporal_start = data.temporal_start.split('T')[0] if data.temporal_start else None %}
             {{ form.input_raw('temporal_start', id='field-temporal-start', value=data_temporal_start,
             error=errors.temporal_start,
             classes=['control-full'],type='date',is_required=true,placeholder=_('from'))
@@ -84,7 +84,7 @@ is_required=true,placeholder=_('Enter title')) }}
         <div class='span3'>
             <!-- field temporal_end -->
             {% set temporal_end_label='Ende des Zeitraumes' %}
-	    {% set data_temporal_end = data.temporal_end.split('T')[0] if data.temporal_end else None %}
+            {% set data_temporal_end = data.temporal_end.split('T')[0] if data.temporal_end else None %}
             {{ form.input_raw('temporal_end', id='field-temporal-end', value=data_temporal_end,
             error=errors.temporal_end, classes=['control-full'],type='date',is_required=true, placeholder=_('to'))
             }}
@@ -200,4 +200,4 @@ is_required=true,placeholder=_('Enter title')) }}
             </div>
         </div>
     </div>
-    {% endif %}
+    {% endif %}
\ No newline at end of file
diff --git a/ckanext/odsh/templates/snippets/search_form.html b/ckanext/odsh/templates/snippets/search_form.html
index cd01cdb9..c1d4b041 100644
--- a/ckanext/odsh/templates/snippets/search_form.html
+++ b/ckanext/odsh/templates/snippets/search_form.html
@@ -72,6 +72,27 @@
     <p class="filter-list"></p>
     {% 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 %}
+        <p class="filter-list">
+            {% if start_date %}
+            <span class="filtered pill">
+                {{_('daterange')}}: {{_('from')}} {{ h.odsh_render_datetime(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)}}
+                <a href="{{ h.remove_url_param('ext_enddate') }}" class="remove" title="{{ _('Remove') }}"><i class="fa fa-times"></i></a>
+            </span>
+            {% endif %}
+        </p>
+        {% endif %}
+    </div>
     {% endblock %}
 
 </form>
-- 
GitLab