Commit 3e532c9d authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Check rules on server side (and make code shared for cli and server)

parent 8a869c98
......@@ -57,29 +57,7 @@ class JflowArgumentParser (argparse.ArgumentParser):
return new_arg_strings
def _load_sub_parameters(sub_parameters, parameter, multiple_values=False):
"""
For Multiple parameters, get sub parameters given by the user
@param sub_parameters: sub parameters
@param parameter: parent parameter
@param multiple_values:
@return:
"""
parameters_used = {}
for sub_parameter in sub_parameters:
sp_name = sub_parameter[0].replace("-", "_")
param_name = parameter.name + ">" + sp_name
if multiple_values:
if param_name not in parameters_used:
parameters_used[param_name] = []
parameters_used[param_name].append(sub_parameter[1])
else:
parameters_used[param_name] = sub_parameter[1]
return parameters_used
def parse_parameters_rules(wf_instance):
from jflow.parameter import InputFile, InputFileList, MultiParameterList, MultiParameter
# Get users arguments:
arg_parser = JflowArgumentParser()
......@@ -91,51 +69,11 @@ def parse_parameters_rules(wf_instance):
arg_sub_parser.add_argument(parameter.flag, **parameter.export_to_argparse(True))
user_params = vars(arg_parser.parse_args())
# Get rules:
rules = {}
all_files = [] # Store all given files, used later
parameters_used = {} # Parameters given by the user
print(user_params)
for parameter in parameters:
# Get sub-parameters of MultiParameter or MultiParameterLists:
if isinstance(parameter, MultiParameterList) or isinstance(parameter, MultiParameter):
parameters_used[parameter.name] = {}
# Get parameters given by the user:
if isinstance(parameter, MultiParameterList):
for rows in user_params[parameter.name]:
parameters_used = {**parameters_used, **_load_sub_parameters(rows, parameter, True)}
else: # MultiParameter
parameters_used = {**parameters_used, **_load_sub_parameters(user_params[parameter.name], parameter)}
# Get all parameters and check rules for the ones given by the user:
for sub_param in parameter.sub_parameters:
param_name = parameter.name + ">" + sub_param.name
if param_name in parameters_used:
if sub_param.rules is not None:
rules[param_name] = sub_param.rules.split(";")
if isinstance(sub_param, InputFile):
all_files += parameters_used[param_name]
if parameter.rules is not None:
rules[parameter.name] = parameter.rules.split(";")
# Other parameters:
elif parameter.name in user_params and user_params[parameter.name] is not None and user_params[parameter.name]:
parameters_used[parameter.name] = user_params[parameter.name]
if parameter.rules is not None:
rules[parameter.name] = parameter.rules.split(";")
if isinstance(parameter, InputFile):
all_files.append(user_params[parameter.name])
elif isinstance(parameter, InputFileList):
all_files += user_params[parameter.name]
# Check rules:
for src_arg, arg_rules in rules.items():
for arg_rule in arg_rules:
try:
wf_instance.check_parameter_rule(arg_rule, src_arg, parameters_used, all_files)
except Exception as e:
arg_sub_parser.error(e)
try:
wf_instance.check_parameters_rules(user_params)
except Exception as e:
arg_sub_parser.error(e)
if __name__ == '__main__':
......
......@@ -348,11 +348,12 @@ class JFlowServer (object):
for sub_param in multi_sub_params[param]:
kwargs_modified[param].append(multi_sub_params[param][sub_param])
for param in kwargs_modified:
logging.debug(param)
logging.debug(str(kwargs_modified[param]))
workflow = self.wfmanager.run_workflow(kwargs_modified["workflow_class"], kwargs_modified)
try:
workflow.check_parameters_rules(kwargs_modified)
except Exception as e:
return {"status": 2, "content": str(e)}
return { "status" : 0, "content" : self.jsonify_workflow_status(workflow, True) }
except Exception as err:
return { "status" : 1, "content" : str(err) }
......
......@@ -1210,6 +1210,74 @@ class Workflow(threading.Thread):
for symbol in symbols:
self.globals[symbol] = getattr(m, symbol)
@staticmethod
def _load_sub_parameters(sub_parameters, parameter, multiple_values=False):
"""
For Multiple parameters, get sub parameters given by the user
@param sub_parameters: sub parameters
@param parameter: parent parameter
@param multiple_values:
@return:
"""
parameters_used = {}
for sub_parameter in sub_parameters:
sp_name = sub_parameter[0].replace("-", "_")
param_name = parameter.name + ">" + sp_name
if multiple_values:
if param_name not in parameters_used:
parameters_used[param_name] = []
parameters_used[param_name].append(sub_parameter[1])
else:
parameters_used[param_name] = sub_parameter[1]
return parameters_used
def check_parameters_rules(self, user_params):
parameters = self.get_parameters()
# Get rules:
rules = {}
all_files = [] # Store all given files, used later
parameters_used = {} # Parameters given by the user
print(user_params)
for parameter in parameters:
# Get sub-parameters of MultiParameter or MultiParameterLists:
if isinstance(parameter, MultiParameterList) or isinstance(parameter, MultiParameter):
parameters_used[parameter.name] = {}
# Get parameters given by the user:
if isinstance(parameter, MultiParameterList):
for rows in user_params[parameter.name]:
parameters_used = {**parameters_used, **self._load_sub_parameters(rows, parameter, True)}
else: # MultiParameter
parameters_used = {**parameters_used,
**self._load_sub_parameters(user_params[parameter.name], parameter)}
# Get all parameters and check rules for the ones given by the user:
for sub_param in parameter.sub_parameters:
param_name = parameter.name + ">" + sub_param.name
if param_name in parameters_used:
if sub_param.rules is not None:
rules[param_name] = sub_param.rules.split(";")
if isinstance(sub_param, InputFile):
all_files += parameters_used[param_name]
if parameter.rules is not None:
rules[parameter.name] = parameter.rules.split(";")
# Other parameters:
elif parameter.name in user_params and user_params[parameter.name] is not None and user_params[
parameter.name]:
parameters_used[parameter.name] = user_params[parameter.name]
if parameter.rules is not None:
rules[parameter.name] = parameter.rules.split(";")
if isinstance(parameter, InputFile):
all_files.append(user_params[parameter.name])
elif isinstance(parameter, InputFileList):
all_files += user_params[parameter.name]
# Check rules:
for src_arg, arg_rules in rules.items():
for arg_rule in arg_rules:
self.check_parameter_rule(arg_rule, src_arg, parameters_used, all_files)
def check_parameter_rule(self, rule: str, src_arg: str, user_args: dict, all_files: list):
"""
Check a parameter rule
......
......@@ -391,6 +391,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
}
var _validateAndSubmitForm = function($this) {
$this.$element.find(".error-wf").remove();
// check if the form is valid
if ($("#workflow_form").valid()) {
......@@ -504,7 +505,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
}
});
$this.$element.on("uploaded.wfform", function() {
$this.$element.off("uploaded.wfform").on("uploaded.wfform", function() {
$this.$element.off("uploaded");
$.ajax({
url: $this.options.serverURL + '/run_workflow?' + params + 'callback=?',
......@@ -512,7 +513,26 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
success: function(data) {
// Ajax success
if( data["status"] == 0 ) {
$this.$element.trigger('run.wfform', data["content"]);
$this.$element.trigger('run.wfform', data["content"]);
} else if(data["status"] == 2) {
//alert(data["content"]);
var alert_message = ['<div class="alert alert-danger error-wf" role="alert">',
'<strong>Error!</strong>',
data["content"],
'</div>'].join('\n');
$this.$element.prepend(alert_message);
$("#progress").html("");
$("#workflow_form").show();
$("#setAndRunModal").animate({ scrollTop: 0 }, "slow");
$("#run_workflow").closest(".modal-footer").show();
//Reset local input files:
$(".input-group").each(function() {
var input_id = $(this).find("input").attr("id")
if ($("#inputfile_" + input_id).html() == "local file") {
$("#" + input_id).val("");
}
})
// Ajax error
} else {
var alert_message = ['<div class="alert alert-danger" role="alert">',
......@@ -524,39 +544,50 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
}
});
});
$this.$element.trigger('uploading.wfform');
var nbdata2submit = 0;
// submit the data of each fileupload
$(".fileupload").each(function(){
var tid = $(this).attr("id").split("_"),
iid = tid.slice(1, tid.length).join("_");
if ($("#"+iid).data("data2upload")) {
$("#"+iid).data("data2upload").submit();
// submit the data of each fileupload
$(".fileupload").each(function () {
var tid = $(this).attr("id").split("_"),
iid = tid.slice(1, tid.length).join("_");
if ($("#" + iid).data("data2upload")) {
$("#" + iid).data("data2upload").submit();
nbdata2submit += 1;
}
});
}
});
console.log(nbdata2submit)
if (nbdata2submit > 0) {
if ($this.options.forceIframeTransport) {
// execute _uploadProgressIframe to init the display
// execute _uploadProgressIframe to init the display
_uploadProgressIframe($this, true);
// then loop to follow the file upload
timeriframe = setInterval(function() { _uploadProgressIframe($this, false) }, $this.options.timer);
timeriframe = setInterval(function () {
_uploadProgressIframe($this, false)
}, $this.options.timer);
} else {
var upload_file_status = new Array();
$(".fileupload").each(function(){
var tid = $(this).attr("id").split("_"),
iid = tid.slice(1, tid.length).join("_");
upload_file_status.push({param: iid.split("_").join(" "), param_id: iid, loaded: 0, total: 100});
var upload_file_status = new Array();
$(".fileupload").each(function () {
var tid = $(this).attr("id").split("_"),
iid = tid.slice(1, tid.length).join("_");
upload_file_status.push({
param: iid.split("_").join(" "),
param_id: iid,
loaded: 0,
total: 100
});
});
$("#workflow_form").hide();
$("#progress").html("");
$.tmpl($this.options.progressTemplate, {upload_file_status: upload_file_status}).appendTo($("#progress"));
// loop to follow the file upload
timerclassic = setInterval(function() { _uploadProgressClassic($this, nbdata2submit) }, $this.options.timer);
}
$("#workflow_form").hide();
$("#progress").html("");
$.tmpl($this.options.progressTemplate, {upload_file_status: upload_file_status}).appendTo($("#progress"));
// loop to follow the file upload
timerclassic = setInterval(function () {
_uploadProgressClassic($this, nbdata2submit)
}, $this.options.timer);
}
} else {
$this.$element.trigger('uploaded.wfform');
}
......@@ -1575,7 +1606,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
// add buttons if requested
' {{if display_reset_button || display_run_button}}',
' <div class="row"> <div class="col-md-3 col-md-offset-9">',
' <div class="btn-group">',
' <div class="btn-group launch-wf">',
' {{if display_reset_button}}',
' <button id="wfform_reset_btn" type="button" class="btn btn-default"><span class="glyphicon glyphicon-refresh"></span> Reset</button>',
' {{/if}}',
......
Markdown is supported
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