Commit 137fd65e authored by Frédéric Escudié's avatar Frédéric Escudié
Browse files

Fix serialisation bug with DateParameter.

parent c0924f6e
......@@ -28,6 +28,8 @@ limit_submission = 100
server_socket_host = 127.0.0.1
# on which socket port should run the web server
server_socket_port = 8080
# date format
date_format = %d/%m/%Y
[email]
# if you want an email to be sent at the end of the workflow execution
......
......@@ -22,6 +22,7 @@ import json
import sys
from functools import wraps
import time
import datetime
import os
import argparse
import logging
......@@ -76,6 +77,12 @@ def CORS():
cherrypy.response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Content-Range, Content-Disposition'
cherrypy.tools.CORS = cherrypy.Tool('before_finalize', CORS)
class JFlowJSONEncoder (json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (datetime.date, datetime.datetime)):
return obj.strftime( JFlowConfigReader().get_date_format() )
else:
return json.JSONEncoder.default(self, obj)
class JFlowServer (object):
......@@ -94,7 +101,7 @@ class JFlowServer (object):
value = func(*args, **kw)
cherrypy.response.headers["Content-Type"] = "application/json"
# if JSONP request
if kw.has_key("callback"): return kw["callback"] + "(" + json.dumps(value) + ")"
if kw.has_key("callback"): return kw["callback"] + "(" + json.dumps(value, cls=JFlowJSONEncoder) + ")"
# else return the JSON
else: return json.dumps(value)
return wrapper
......@@ -173,6 +180,16 @@ class JFlowServer (object):
"name": param.name,
"display_name": param.display_name,
"group": param.group}
if hash_param["type"] == "date":
hash_param["format"] = self.jflow_config_reader.get_date_format()
if hash_param["format"] == '%d/%m/%Y':
hash_param["format"] = 'dd/mm/yyyy'
elif hash_param["format"] == '%d/%m/%y':
hash_param["format"] = 'dd/mm/yy'
elif hash_param["format"] == '%Y/%m/%d':
hash_param["format"] = 'yyyy/mm/dd'
elif hash_param["format"] == '%y/%m/%d':
hash_param["format"] = 'yy/mm/dd'
# if it's a multiple type add sub parameters
if type(param.type) == jflow.parameter.MultipleParameters:
hash_param["sub_parameters"] = []
......@@ -186,6 +203,16 @@ class JFlowServer (object):
"name": param.name + JFlowServer.MULTIPLE_TYPE_SPLITER + sub_param.flag,
"display_name": sub_param.display_name,
"group": param.group})
if hash_param["type"] == "date":
hash_param["format"] = self.jflow_config_reader.get_date_format()
if hash_param["format"] == '%d/%m/%Y':
hash_param["format"] = 'dd/mm/yyyy'
elif hash_param["format"] == '%d/%m/%y':
hash_param["format"] = 'dd/mm/yy'
elif hash_param["format"] == '%Y/%m/%d':
hash_param["format"] = 'yyyy/mm/dd'
elif hash_param["format"] == '%y/%m/%d':
hash_param["format"] = 'yy/mm/dd'
parameters.append(hash_param)
if parameters_per_groups.has_key(param.group):
parameters_per_groups[param.group].append(hash_param)
......@@ -218,7 +245,6 @@ class JFlowServer (object):
# if this is a MultiParameterList
# TODO: du cote interface faire qq chose du genre: key.sub_key.1 ... donc si len == 3
# l'objectif etant d'avoir une structure de type: [[(sub_key1: val), (sub_key2: val)], [(sub_key1: val2), (sub_key2: val2)]]
workflow = self.wfmanager.run_workflow(kwargs_modified["workflow_class"], kwargs_modified)
return { "status" : 0, "content" : self.jsonify_workflow_status(workflow, True) }
except Exception as err:
......
......@@ -74,6 +74,13 @@ class JFlowConfigReader(object):
sys.exit(1)
return exec_path
def get_date_format(self):
try:
date_format = self.reader.get("global", "date_format")
except:
raise Error("Failed when parsing the config file, no parameter date_format!")
return date_format
def get_batch(self):
try:
type = self.reader.get("global", "batch_system_type")
......
......@@ -587,6 +587,14 @@ class DateParameter(datetime.datetime, AbstractParameter):
else:
return (True if datetime.date(self.year, self.month, self.day) else False)
def __reduce__(self):
return (DateParameter, (self.name, self.help, self.default, date, self.choices, self.required,
self.flag, self.sub_parameters, self.group, self.display_name), None, None, None)
def __reduce_ex__(self, protocol):
return (DateParameter, (self.name, self.help, self.default, date, self.choices, self.required,
self.flag, self.sub_parameters, self.group, self.display_name), None, None, None)
class AbstractInputFile(AbstractIOFile):
"""
......
......@@ -508,7 +508,7 @@ jQuery.validator.addMethod("exclude_required", function(value, element, options)
' </select>',
// if param is a date
'{{else param.type == "date"}}',
' <div class="input-append date" data-date="${param.default}" data-date-format="dd/mm/yyyy">',
' <div class="input-append date" data-date="${param.default}" data-date-format="${param.format}">',
' <input id="${param.name}" name="${param.name}" class="${param.group} ${input_size} span2" type="text" value="${param.default}">',
' <button class="btn" type="button"><i class="icon-calendar"></i></button>',
' </div>',
......
......@@ -17,12 +17,12 @@
import argparse
import datetime
from jflow.config_reader import JFlowConfigReader
def date(datestr):
try:
return datetime.datetime.strptime(datestr, '%d/%m/%Y')
return datetime.datetime.strptime(datestr, JFlowConfigReader().get_date_format())
except:
raise argparse.ArgumentTypeError("'" + datestr + "' is an invalid date!")
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment