Commit e929e5cd authored by Jerome Mariette's avatar Jerome Mariette
Browse files

No commit message

No commit message
parent d436a891
......@@ -108,7 +108,8 @@ class JFlowServer (object):
"components": components}
return status
"""
@cherrypy.expose
@jsonify
def upload_urlfile(self, **kwargs):
......@@ -140,6 +141,7 @@ class JFlowServer (object):
return {"file_name": os.path.basename(file_path), "file_path": file_path, "uploaded": True}
else:
return {"errorMsg": "URL '" + url + "' does not contain any file name!", "uploaded": False}
"""
@cherrypy.expose
@jsonify
......@@ -251,31 +253,6 @@ class JFlowServer (object):
except Exception, e:
return str(e)
@cherrypy.expose
@jsonify
def validate_urlfile(self, **kwargs):
url = None
try:
for key in kwargs.keys():
if key != "callback" and key != "_":
value_key = key
break
url = kwargs[value_key]
uri_object = urlparse(url)
opener = urllib2.urlopen(url)
except Exception, e:
return "URL '" + url + "' is invalid!"
file_name = os.path.basename(uri_object.path)
if file_name is not None and file_name != "":
metadata = opener.info()
file_size = int(metadata.getheaders("Content-Length")[0])
if file_size == 0:
return "The URL file '" + url + "' is empty!"
return True
else:
return "URL '" + url + "' does not contain any file name!"
if __name__ == '__main__':
parser = argparse.ArgumentParser()
......
......@@ -32,12 +32,16 @@
if ($element.parent().parent("blockquote").find("div").length > 0) {
// if it's a multiple type
$element.parent().parent("blockquote").parent("div.controls").parent("div.control-group").addClass(errorClass);
//highlight append button as well$
// highlight append button as well
$element.parent().parent("blockquote").find("div.input-append").each(function() {
$(this).find(".btn").addClass("btn-danger");
$(this).find(".icon-calendar").addClass("icon-white");
$(this).find(".icon-upload").addClass("icon-white");
});
//highlight prepend button as well
$element.parent().parent("blockquote").find("div.input-prepend").each(function() {
$(this).find(".btn").addClass("btn-danger");
});
} else {
$element.parents("div.control-group").addClass(errorClass);
}
......@@ -47,6 +51,10 @@
$element.parent("div.input-append").find(".icon-calendar").addClass("icon-white");
$element.parent("div.input-append").find(".icon-upload").addClass("icon-white");
}
// if it's an inputfile
if ($element.parent("div.input-prepend").find(".btn-group").length > 0) {
$element.parent("div.input-prepend").find(".btn").addClass("btn-danger");
}
},
unhighlight: function (element, errorClass, validClass) {
......@@ -77,6 +85,9 @@
$element.parent("div.input-append").find(".icon-calendar").removeClass("icon-white");
$element.parent("div.input-append").find(".icon-upload").removeClass("icon-white");
}
if ($element.parent("div.input-prepend").find(".btn-group").length > 0) {
$element.parent("div.input-prepend").find(".btn").removeClass("btn-danger");
}
}
},
errorPlacement: function(error, element) {
......@@ -132,8 +143,6 @@ jQuery.validator.addMethod("exclude_required", function(value, element, options)
var WFForm = function (element, options) {
this.$element = $(element)
this.options = $.extend({}, $.fn.wfform.defaults, options);
// a hash providing the correspondance between what should be displayed and the server file path
this.upload_files = {};
if (this.options.serverURL == "") { this.options.serverURL = $.fn.activewf.defaults.serverURL; }
}
......@@ -160,9 +169,6 @@ jQuery.validator.addMethod("exclude_required", function(value, element, options)
$.each ( $('#workflow_form').serializeArray(), function(_, kv) {
params += kv.name + "=" +escape(kv.value) + "&";
});
$(".upload:disabled").each(function() {
params += $(this).attr("id") + "=" +escape($this.upload_files[$(this).val()]) + "&";
});
$("#workflow_form :checkbox").each(function(){
if (!$(this).prop('checked')) { params += $(this).attr("name") + "=false&"; }
});
......@@ -176,44 +182,6 @@ jQuery.validator.addMethod("exclude_required", function(value, element, options)
}
}
var urlfile_handler = function(e) {
var parts = $(this).attr('id').split("_"),
field_id = parts.slice( 2, parts.length ).join( "_" ),
$button = $(this) ;
$("#"+field_id).attr("disabled", "disabled");
$(this).attr("disabled", "disabled");
e.preventDefault();
var l = Ladda.create(this);
l.start();
$.ajax({
url: e.data.ref.options.serverURL + '/upload_urlfile?callback=?',
dataType: "json",
timeout: 20000,
data:{ 'url' : $("#"+field_id).val() },
error: function (xhr, ajaxOptions, thrownError) {
//TODO: handle the error
},
success: function(data) {
l.stop();
if( data["uploaded"] ) {
$("#"+field_id).val(data["file_name"]);
e.data.ref.upload_files[data["file_name"]] = data["file_path"];
$button.html('<i class="icon-remove-circle"></i>');
$button.unbind( "click" );
$button.bind( "click", function(){
$("#"+field_id).removeAttr('disabled');
$("#"+field_id).val("");
$(this).html('<i class="icon-upload"></i>');
$(this).unbind("click");
$(this).bind("click", {'serverURL':e.data.ref}, urlfile_handler);
});
} else {
$("#"+field_id).removeAttr('disabled');
}
}
});
}
WFForm.prototype.load = function() {
var $this = this,
waiting_animation = ['<div class="row-fluid"><div class="span1 offset3"><div class="inline floatingBarsG">',
......@@ -254,8 +222,18 @@ jQuery.validator.addMethod("exclude_required", function(value, element, options)
display_reset_button: $this.options.displayResetButton, parameters: $this.options.parameters,
getParameterDisplay:$this._getParameterDisplay, parameterTemplate: $this.options.parameterTemplate}).appendTo($this.$element);
// handle the url file upload button
$('[id^=urlfile_btn_]').bind("click", {'ref':$this}, urlfile_handler);
// handle inputfile
$("a[class^=inputfile_]").click(function(){
$("#"+$(this).attr("class")).html($(this).html());
var parts = $(this).attr("class").split("_"),
parameter = parts.slice(1, parts.length).join("_");
if ($(this).html() == "url") {
rules[parameter].remote.data.type = "urlfile";
} else if ($(this).html() == "server file") {
rules[parameter].remote.data.type = "localfile";
}
//$("#workflow_form").validate({ rules: rules });
});
$('.date').datepicker().on('changeDate', function(ev){ $('.date').datepicker('hide');} );
......@@ -295,12 +273,6 @@ jQuery.validator.addMethod("exclude_required", function(value, element, options)
crule["number"] = true;
} else if (param.type == "date") {
crule["date"] = true;
// if this is a urlfile type
} else if (param.type == "urlfile") {
crule["remote"] = {
url: server_url + '/validate_urlfile?callback=?',
type: "post"
};
// if it is not a known type, check from the custom ones, using the validate_field address
} else {
crule["remote"] = {
......@@ -368,11 +340,20 @@ jQuery.validator.addMethod("exclude_required", function(value, element, options)
' <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>',
// if param is an urlfile
'{{else param.type == "urlfile"}}',
' <div class="input-append">',
// if param is an inputfile
'{{else param.type == "inputfile"}}',
' <div class="input-prepend">',
' <div class="btn-group">',
' <button class="btn dropdown-toggle" data-toggle="dropdown">',
' <span id="inputfile_${param.name}">server file</span>',
' <span class="caret"></span>',
' </button>',
' <ul class="dropdown-menu">',
' <li><a class="inputfile_${param.name}" href="#">server file</a></li>',
' <li><a class="inputfile_${param.name}" href="#">url</a></li>',
' </ul>',
' </div>',
' <input id="${param.name}" name="${param.name}" class="${param.group} ${input_size} span2 upload" type="text" value="${param.default}">',
' <button id="urlfile_btn_${param.name}" class="btn ladda-button" data-style="zoom-in" data-spinner-color="#000000" type="button"><span class="ladda-label"><i class="icon-upload"></i></span></button>',
' </div>',
// if param is a boolean
'{{else param.type == "bool"}}',
......
......@@ -25,7 +25,7 @@ import logging
from urlparse import urlparse
from jflow.config_reader import JFlowConfigReader
# custom date type
def date(datestr):
return datetime.datetime.strptime(datestr, '%d/%m/%Y')
......@@ -35,27 +35,28 @@ def localfile(file):
else:
raise argparse.ArgumentTypeError("Local file '" + file + "' does not exists! Please provide a valid file path!")
def inputfile(ifile):
def urlfile(file):
uri_object = urlparse(file)
try:
opener = urllib2.urlopen(file)
except:
raise argparse.ArgumentTypeError("URL '" + file + "' is invalid!")
file_name = os.path.basename(uri_object.path)
if file_name is not None and file_name != "":
metadata = opener.info()
file_size = int(metadata.getheaders("Content-Length")[0])
if file_size == 0:
raise argparse.ArgumentTypeError("The URL file '" + file + "' is empty!")
return file
else:
raise argparse.ArgumentTypeError("URL '" + file + "' does not contain any file name!")
def inputfile(file):
# test the format
uri_object = urlparse(ifile)
uri_object = urlparse(file)
# check the file
if uri_object.scheme == '':
if os.path.isfile(ifile):
return os.path.abspath(ifile)
else:
raise argparse.ArgumentTypeError("Local file '" + ifile + "' does not exists! Please provide a valid file path!")
localfile(file)
else:
try:
opener = urllib2.urlopen(ifile)
except:
raise argparse.ArgumentTypeError("URL '" + ifile + "' is invalid!")
file_name = os.path.basename(uri_object.path)
if file_name is not None and file_name != "":
metadata = opener.info()
file_size = int(metadata.getheaders("Content-Length")[0])
if file_size == 0:
raise argparse.ArgumentTypeError("The URL file '" + ifile + "' is empty!")
return ifile
else:
raise argparse.ArgumentTypeError("URL '" + ifile + "' does not contain any file name!")
\ No newline at end of file
urlfile(file)
\ 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