Commit dfa0c56f authored by Franziska Koehn's avatar Franziska Koehn
Browse files

functions for downloading/saving query (includes constraints, fields, roottype) implemented

parent ae2179aa
......@@ -87,29 +87,30 @@ def download():
response.headers["Content-Disposition"] = "attachment; filename=test.zip"#TODO rename file
return response
def adjust_constraints(list, parent):
for item in list:
if isinstance(item, dict):
for key in item:
if key=='operator':
child = []
child.append(item['operator'])
adjust_constraints(item['children'], child)
parent.append(child)
elif key=='field':
parent.append((item['field'], item['predicate'], item['value']))
else:
pass
else:
pass
@app.route("/send_query", methods=["POST", "GET"])
def send_query():
import json
from xsa.queries import search_for
#TODO show message: sending query...
def func(list, parent):
for item in list:
if isinstance(item, dict):
for key in item:
if key=='operator':
child = []
child.append(item['operator'])
func(item['children'], child)
parent.append(child)
elif key=='field':
parent.append((item['field'], item['predicate'], item['value']))
else:
pass
else:
pass
constraints=[]
func(json.loads(request.form.get("model")), constraints)
adjust_constraints(json.loads(request.form.get("model")), constraints)
search_fields = json.loads(request.form.get("fields"))
state["host"] = json.loads(request.form.get("host"))
......@@ -159,6 +160,40 @@ def get_adddata():
return results(state["results"])
@app.route("/downl_query", methods=["POST", "GET"])
def downl_query():
from datetime import datetime
from xsa.datatypereader import get_field_label_by_field
import json
root = state["roottype"]
from urllib.parse import unquote
constraints = []
adjust_constraints(json.loads(unquote(request.form.get("model"))), constraints)
labels = []
for f in json.loads(unquote(request.form.get("fields"))):
labels.append(get_field_label_by_field(root, f))
# auslagern (selber code steht in xsagtk.main_controller):
root_key = 'root'
query_key = 'query'
labels_key = 'labels'
data = {}
data.update({root_key: root})
data.update({query_key: constraints})
data.update({labels_key: labels})
response = Response(json.dumps(data), mimetype='application/json')
filename= "query_%s" % str(datetime.now())
response.headers["Content-Disposition"] = "attachment; filename="+filename
return response
@app.route("/downl_csv", methods=["POST", "GET"])
def download_csv():
from datetime import datetime
......
......@@ -45,9 +45,6 @@
$("#roottypes button").html(rt + '<span class="caret"></span>');
};
</script>
</head>
......@@ -133,5 +130,24 @@ $(function() {
fileElem = document.getElementById("fileElem");
});
</script>
<script>
// http://www.filamentgroup.com/lab/jquery-plugin-for-requesting-ajax-like-file-downloads.html
jQuery.download = function(url, data, method){
//url and data options required
if( url && data ){
//data can be string of parameters or array/object
data = typeof data == 'string' ? data : jQuery.param(data);
//split params into form inputs
var inputs = '';
jQuery.each(data.split('&'), function(){
var pair = this.split('=');
inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />';
});
//send request
jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
.appendTo('body').submit().remove();
};
};
</script>
......@@ -88,9 +88,14 @@
el.append(build_tree(model, true));
};
var sendquery = function() {
var getselectedfields = function(){
var selecfields = [];
$('input[type="checkbox"]:checked').each(function(){ selecfields.push($(this).attr('name')) });
return selecfields;
};
var sendquery = function() {
var selecfields = getselectedfields();
var passw = $('input[id="inputPassw"]').val();
var user = $('input[id="inputUser"]').val();
var host = $('input[id="inputHost"]').val();
......@@ -103,39 +108,49 @@
data:{"passw":JSON.stringify(passw), "user":JSON.stringify(user), "host":JSON.stringify(host), "model":JSON.stringify(model), "fields":JSON.stringify(selecfields)}, method:"POST" });
};
var setquery = function(jsonobj){
var add_to_model = function(m) {
var i2;
console.log("add", m);
if (m[0] == "AND" || m[0] == "OR") { // conjunktion
var rest = [];
for (i2 = 1; i2 < m.length; i2++) // über rest der selben ebene
{
rest.push(add_to_model(m[i2]))
}
return {operator: m[0], children: rest}
}
else {// filterfield
return {name: get_label(m[0]), predicate: m[1], value: m[2], idx: get_idx(m[0]), field: m[0]}
}
};
var setquery = function(jsonobj){
var add_to_model = function(m) {
var i2;
console.log("add", m);
if (m[0] == "AND" || m[0] == "OR") { // conjunktion
var rest = [];
for (i2 = 1; i2 < m.length; i2++) // über rest der selben ebene
{
rest.push(add_to_model(m[i2]))
}
return {operator: m[0], children: rest}
}
else {// filterfield
return {name: get_label(m[0]), predicate: m[1], value: m[2], idx: get_idx(m[0]), field: m[0]}
}
};
var new_model = [], i;
var new_model = [], i;
for (i = 0; i < jsonobj.query.length; i++) {
new_model.push(add_to_model(jsonobj.query[i]));
}
console.log(new_model);
for (i = 0; i < jsonobj.query.length; i++) {
new_model.push(add_to_model(jsonobj.query[i]));
}
console.log(new_model);
model = new_model;
update_tree();
model = new_model;
update_tree();
for(i = 0; i < jsonobj.labels.length; i++) {
var field = get_field_from_label(jsonobj.labels[i]);
$("#check input[name='" + field + "']").attr("checked","checked");
}
}
for(i = 0; i < jsonobj.labels.length; i++) {
var field = get_field_from_label(jsonobj.labels[i]);
$("#check input[name='" + field + "']").attr("checked","checked");
}
};
var downloadquery = function(){
console.log("download query");
var selecfields = getselectedfields();
$.download("/downl_query", {"model":JSON.stringify(model), "fields":JSON.stringify(selecfields)}, "POST");
/*$.ajax("/downl_query", { success: function(data) {
console.log(data);
},
data:{"model":JSON.stringify(model), "fields":JSON.stringify(selecfields)}, method:"POST" });*/
};
</script>
......@@ -176,7 +191,7 @@ var setquery = function(jsonobj){
</div>
<div class="col-md-12 main">
<button type="button" class="btn btn-lg btn-primary">Download Query</button>
<button type="button" onclick= "downloadquery()" class="btn btn-lg btn-primary">Download Query</button>
<button type="button" class="btn btn-lg btn-primary" onclick="sendquery()">Send Query</button>
</div>
......
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