From eb16cd6029947b06100ed668a94200178cf9a5cc Mon Sep 17 00:00:00 2001
From: anonymous <anonymous>
Date: Thu, 2 May 2019 11:47:53 +0200
Subject: [PATCH] fix date handling

---
 ckanext/odsh/fanstatic/odsh_datepicker.js | 7 +++++--
 ckanext/odsh/helpers.py                   | 2 +-
 ckanext/odsh/plugin.py                    | 2 +-
 ckanext/odsh/validation.py                | 6 ++++++
 4 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/ckanext/odsh/fanstatic/odsh_datepicker.js b/ckanext/odsh/fanstatic/odsh_datepicker.js
index 8a6b2601..f4aa9e4a 100644
--- a/ckanext/odsh/fanstatic/odsh_datepicker.js
+++ b/ckanext/odsh/fanstatic/odsh_datepicker.js
@@ -24,16 +24,19 @@ this.ckan.module('odsh_datepicker', function ($, _)
                 autoclose: true,
                 language: 'de',
                 clearBtn: true,
-                forceParse: true
+                forceParse: false
             }
             var onChange = function (ev)
             {
                 var dateString = $(ev.target).val()
                 var date = moment(dateString, "DD.MM.YYYY", true)
+                console.log(date)
                 var isValid = date.isValid() && (dateString.length == 10)
                 if (isValid)
                 {
-                    target.val(date.format(serverFormat));
+                    var serverDate = date.format(serverFormat);
+                    console.log(serverDate)
+                    target.val(serverDate);
                 }
                 else
                 {
diff --git a/ckanext/odsh/helpers.py b/ckanext/odsh/helpers.py
index 0b7eec7b..138f96b5 100644
--- a/ckanext/odsh/helpers.py
+++ b/ckanext/odsh/helpers.py
@@ -117,7 +117,7 @@ def extend_search_convert_local_to_utc_timestamp(str_timestamp):
     return dt+"Z"
 
 def odsh_render_datetime(datetime_, fromIso=True):
-    date_format='{0.day:02d}.{0.month:02d}.{0.year:4d}'
+    date_format='{0.day:02d}.{0.month:02d}.{0.year:04d}'
     if not datetime_:
         return ''
     if not re.match(r'\d\d\d\d-\d\d-\d\d', datetime_):
diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py
index 32d10c84..fe3a6007 100644
--- a/ckanext/odsh/plugin.py
+++ b/ckanext/odsh/plugin.py
@@ -445,7 +445,7 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
             field = 'extras_' + field
             if field in dict_pkg and dict_pkg[field]:
                 d = parse(dict_pkg[field])
-                dict_pkg[field] = '{0.year:4d}-{0.month:02d}-{0.day:02d}T00:00:00Z'.format(d)
+                dict_pkg[field] = '{0.year:04d}-{0.month:02d}-{0.day:02d}T00:00:00Z'.format(d)
         # if 'res_format' in dict_pkg:
         #     dict_pkg['res_format']=[e.lower() for e in dict_pkg['res_format']]
 
diff --git a/ckanext/odsh/validation.py b/ckanext/odsh/validation.py
index 90efa8f2..7a93469f 100644
--- a/ckanext/odsh/validation.py
+++ b/ckanext/odsh/validation.py
@@ -82,6 +82,8 @@ def _set_value(data, field, value):
 def validate_extra_date_new(key, field, data, optional, errors):
     value = _extract_value(data, field)
 
+    print("DATE", value)
+
     if not value:
         if not optional:
             errors[field] = 'empty'
@@ -89,11 +91,15 @@ def validate_extra_date_new(key, field, data, optional, errors):
     else:
         if re.match(r'\d\d\d\d-\d\d-\d\d', value):
             try:
+                print ("BEOFRE PARSE", value)
                 dt=parse(value)
+                print("PARSED DATE", dt)
                 _set_value(data, field, dt.isoformat())
                 return
             except ValueError:
+                print('ERROR from Exception')
                 pass
+        print('ERROR')
         errors[field] = 'not a valid date'
 
 def validate_licenseAttributionByText(key, data, errors,context):
-- 
GitLab