Skip to content

Commit

Permalink
added submission of metadata changes (issue #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Aug 21, 2016
1 parent c57190a commit 6bbc5c9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
59 changes: 57 additions & 2 deletions flat/style/flat.metadata.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var newfields = 0;
var metadatafields = 0;

function metadata_ontimer() {
}
Expand All @@ -11,21 +13,74 @@ function metadata_oninit() {
s = s + "<tr><td class=\"key\"><input id=\"metakey" + i + "\" value=\"" + key + "\" /></td><td class=\"value\"><input id=\"metavalue" + i + "\" value=\"" + metadata[key] + "\" /></td></tr>\n";
i++;
});
metadatafields = i;
newfields = 0;
s = s + "<tr id=\"metadataplaceholder\"></tr>";
s = s + "</table>";
s = s + "<div class=\"buttons\"><button id=\"metadatasubmit\" onclick=\"metadata_submit\">Save changes</button> <button onclick=\"metadata_addinput()\">+</button></div>";
s = s + "<div class=\"buttons\"><button id=\"metadatasubmit\" onclick=\"metadata_submit()\">Save changes</button> <button onclick=\"metadata_addinput()\">+</button></div>";
$('#metadata').html(s);
metadata_addinput();
}

function metadata_addinput() {
var i = metadata.length + newfields;
var i = metadatafields + newfields;
newfields++;
var s = "<tr><td class=\"key\"><input id=\"metakey" + i + "\" value=\"\" /></td><td class=\"value\"><input id=\"metavalue" + i + "\" value=\"\" /></td></tr>\n";
s = s + "<tr id=\"metadataplaceholder\"></tr>";
$('#metadataplaceholder')[0].outerHTML = s;
}

function metadata_submit() {
var queries = [];
var i = 0;
Object.keys(metadata).forEach(function(key){
var newkey = $('#metakey' + i).val();
var newvalue = $('#metavalue' + i).val();
if (newkey != key) {
//key changed, delete this one
queries.push("USE " + namespace + "/" + docid + " META " + key + "=NONE");
//add a new one
if (newkey !== "") {
queries.push("USE " + namespace + "/" + docid + " META " + newkey + "=" + newvalue);
}
} else if (newvalue != metadata[key]) {
queries.push("USE " + namespace + "/" + docid + " META " + key + "=" + newvalue);
}
i++;
});
//add new fields
for (j = i; j < i+newfields; j++) {
var newkey = $('#metakey' + j).val();
var newvalue = $('#metavalue' + j).val();
if (newkey) {
queries.push("USE " + namespace + "/" + docid + " META " + newkey + "=" + newvalue);
}
}
if (queries.length > 0) {
$('#wait span.msg').val("Submitting changes");
$('#wait').show();
$.ajax({
type: 'POST',
url: "/" + namespace + "/"+ docid + "/query/",
contentType: "application/json",
//processData: false,
headers: {'X-sessionid': sid },
data: JSON.stringify( { 'queries': queries}),
success: function(data) {
if (data.error) {
$('#wait').hide();
alert("Received error from document server: " + data.error);
} else {
location.reload(true); //refresh the page
}
},
error: function(req,err,exception) {
$('#wait').hide();
alert("Editor submission failed: " + req.responseText);
},
dataType: "json"
});
} else {
alert("No changes to submit");
}
}
2 changes: 1 addition & 1 deletion flat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def query(request,namespace, docid):
elif docselector[0] != namespace:
return HttpResponseForbidden("Query would affect a different namespace than your current one, forbidden!")

if query != "GET" and query[:4] != "CQL ":
if query != "GET" and query[:4] != "CQL " and query[:4] != "META":
#parse query on this end to catch syntax errors prior to sending, should be fast enough anyway
try:
query = fql.Query(query)
Expand Down

0 comments on commit 6bbc5c9

Please sign in to comment.