Commit 4c68da79025a5a0c33714b3c642e96945b972bde

  • avatar
  • arvind
  • Fri Feb 08 10:36:59 IST 2013
Integrating saving of JSON on server
						- User can edit json and also load a different json.
						- The edited json can be saved now, if files have same name, API return 409 Conflict.
						- A faulty version of static/jit.js had creeped in. Adding the correct one.
  • Diff rendering mode:
  • inline
  • side by side

fetch.py

1111
12@app.route('/',methods=['GET'])12@app.route('/',methods=['GET'])
13def index():13def index():
14 if request.args.has_key('json'):
15 return render_template('index.html',json=request.args['json'])
16 else:
17 return render_template('index.html')
14 if request.args.has_key('json'):
15 return render_template('index.html',json=request.args['json'])
16 else:
17 return render_template('index.html')
1818
19@app.route('/editor', methods=['GET'])19@app.route('/editor', methods=['GET'])
20def editor():20def editor():
22 filename = request.args['json']22 filename = request.args['json']
23 else:23 else:
24 filename = 'test.json'24 filename = 'test.json'
25 filename = os.path.join('static', filename)
26 try:
27 f = open(filename, 'r')
28 except:
29 f = open('static/test.json', 'r')
30 buf = f.read()
31 f.close()
32 return render_template('editor.html', json = buf)
25 filename = os.path.join('static', filename)
26 try:
27 f = open(filename, 'r')
28 except:
29 f = open('static/test.json', 'r')
30 buf = f.read()
31 f.close()
32 return render_template('editor.html', json = buf)
3333
34@app.route('/saveJSON', methods=['POST'])34@app.route('/saveJSON', methods=['POST'])
35def saveJSON():35def saveJSON():
36 if request.form:
37 data = request.form
38 JSON = data['json']
39 filename = os.path.join('static', data['filename'])
40 f = open(filename, 'w')
41 f.write(JSON)
42 f.close()
43 return (data, 200)
44 else:
45 return (json.dumps(request.form), 200) # Bad Request
36 if request.method == 'POST':
37 response = make_response()
38 JSON = request.form['json']
39 filename = os.path.join('static', request.form['filename'])
40 ls = glob.glob(filename)
41 if len(ls) > 0:
42 response.status_code = 409
43 response.status ="409 Conflict"
44 response.data = "The file that you were trying to save already exits, please try a different name."
45 return response
46 f = open(filename, 'w')
47 f.write(JSON)
48 f.close()
49 return response
50 else:
51 response = make_response()
52 response.code = 400
53 return reponse
4654
47@app.route('/history', methods=['GET'])55@app.route('/history', methods=['GET'])
48def listJSON():56def listJSON():
58 ls = glob.glob(path)58 ls = glob.glob(path)
59 def sanitize(i):59 def sanitize(i):
60 return i.split('/')[-1]60 return i.split('/')[-1]
61 ls = map(sanitize, ls)
62 return render_template('history.html', ls=ls)
61 ls = map(sanitize, ls)
62 return render_template('history.html', ls=ls)
6363
6464
65if __name__ == "__main__":65if __name__ == "__main__":
66 app.run(debug=True, host='0.0.0.0')
66 app.run(debug=True, host='0.0.0.0')
6767
68# #from bson.code import *68# #from bson.code import *
69# #from urllib import unquote_plus
70# def application(environ, start_response):
71# status = '200 OK'
72# response_headers = [('Content-type', 'application/json'),('Access-Control-Allow-Origin', '*')]
73# start_response(status, response_headers)
74# c = pymongo.Connection()
75# db = c['mural']
76# coll = db['data']
77# ret = {}
78# x = 0
79# for i in coll.find():
80# del(i['_id'])
81# ret[x] = i
82# x = x + 1
83# #return repr(recieved)
84# return json.dumps(ret)
69 # #from urllib import unquote_plus
70 # def application(environ, start_response):
71 # status = '200 OK'
72 # response_headers = [('Content-type', 'application/json'),('Access-Control-Allow-Origin', '*')]
73 # start_response(status, response_headers)
74 # c = pymongo.Connection()
75 # db = c['mural']
76 # coll = db['data']
77 # ret = {}
78 # x = 0
79 # for i in coll.find():
80 # del(i['_id'])
81 # ret[x] = i
82 # x = x + 1
83 # #return repr(recieved)
84 # return json.dumps(ret)

static/jit.js

6764 }6764 }
6765 this.plot()6765 this.plot()
6766 },6766 },
6767 reposition: function() {
6768 this.compute("end");
6769 var w=this.graph.getNode(this.root).pos.getc().scale(-1);
6770 e.Util.moebiusTransformation(this.graph,[w],["end"],"end","ignore");
6771 this.graph.eachNode(function(x) {
6772 if(x.ignore){
6773 x.endPos.rho=x.pos.rho;
6774 x.endPos.theta=x.pos.theta
6775 }
6776 });
6767 reposition:function(){
6768 this.compute("end");
6769 var w=this.graph.getNode(this.root).pos.getc().scale(-1);
6770 e.Util.moebiusTransformation(this.graph,[w],["end"],"end","ignore");
6771 this.graph.eachNode(function(x){
6772 if(x.ignore){
6773 x.endPos.rho=x.pos.rho;
6774 x.endPos.theta=x.pos.theta
6775 }
6776 })
6777 },6777 },
6778 plot: function() {
6779 this.fx.plot();
6778 plot:function(){
6779 this.fx.plot()
6780 },6780 },
6781 tree_info_template: _.template($('#tree-info-template').html()),
6782 onClick: function(y, w) {
6783 // Ajay - Visual target selection
6784 /*if (this.graph.getNode(y)._angularWidth <= 0.5) {
6785 if (this.graph.getNode(y).data.band == "Character") {
6786 $("#character-value").html(this.graph.getNode(y).name);
6787 } else if (this.graph.getNode(y).data.band == "Jewellery") {
6788 $("#jewellery-value").html(this.graph.getNode(y).name);
6789 } else if (this.graph.getNode(y).data.band == "Material") {
6790 $("#material-value").html(this.graph.getNode(y).name);
6791 }
6792 }*/
6793 // Ajay - end of visual target selection
6794 var node = this.graph.getNode(y);
6795 var children = $jit.json.getSubtree(tree_json, node.id).children;
6796 if(_.isEmpty(children)) {
6797 $('#tree-info ul').append(this.tree_info_template({
6798 band: node.data.band,
6799 node: node.name
6800 }));
6801 }
6802 var x = node.pos.getc(true);
6803 this.move(x, w);
6804 },
6781 onClick:function(y,w){
6782// Ajay - Visual target selection
6783 if (this.graph.getNode(y)._angularWidth <= 0.5) {
6784 if (this.graph.getNode(y).data.band == "Character") {
6785 $("#character-value").html(this.graph.getNode(y).name);
6786 } else if (this.graph.getNode(y).data.band == "Jewellery") {
6787 $("#jewellery-value").html(this.graph.getNode(y).name);
6788 } else if (this.graph.getNode(y).data.band == "Material") {
6789 $("#material-value").html(this.graph.getNode(y).name);
6790 }
6791 }
6792// Ajay - end of visual target selection
6793 var x=this.graph.getNode(y).pos.getc(true);
6794 this.move(x,w)
6795 },
6805 move:function(A,y){6796 move:function(A,y){
6806 var x=r(A.x,A.y);6797 var x=r(A.x,A.y);
6807 if(this.busy===false&&x.norm()<0.9){ // Ajay - make it clickable upto the depth of tree DIRECTLY. Increase in value = deeper the length.6798 if(this.busy===false&&x.norm()<0.9){ // Ajay - make it clickable upto the depth of tree DIRECTLY. Increase in value = deeper the length.

static/myVector.js

133 };133 };
134 map = new OpenLayers.Map('map', options);134 map = new OpenLayers.Map('map', options);
135 $.get(config.indexer+"/fetch", function(data){135 $.get(config.indexer+"/fetch", function(data){
136 return;
137 if (data.length != 0)
138 {
139 ans.ans = JSON.parse(data);
140 for(var i in ans.ans)
141 {
142 ans.count+=1;
143 makeBoxes(ans.ans[i]);
144 }
136 if (data != undefined)
137 {
138 ans.ans = data;
139 for(var i in ans.ans)
140 {
141 ans.count+=1;
142 makeBoxes(ans.ans[i]);
143 }
145144
146 }
145 }
147146
148 });147 });
149 var layer = new OpenLayers.Layer.TMS( "TMS Layer","static/",148 var layer = new OpenLayers.Layer.TMS( "TMS Layer","static/",

templates/editor.html

41 }41 }
42 try {42 try {
43 json = JSON.parse(json);43 json = JSON.parse(json);
44 console.log(json);
45 console.log(filename);
46 $.post('/saveJSON', {"json": json, "filename": filename}, function(data) {
44 $.post('/saveJSON', {"json": JSON.stringify(json), "filename": filename}, function(data) {
47 console.log(data);45 console.log(data);
48 $('#json-saved').show();46 $('#json-saved').show();
49 }, 'json');47 }, 'json');