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.
fetch.py
(49 / 41)
  
1111
1212@app.route('/',methods=['GET'])
1313def 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
1919@app.route('/editor', methods=['GET'])
2020def editor():
2222 filename = request.args['json']
2323 else:
2424 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
3434@app.route('/saveJSON', methods=['POST'])
3535def 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
4755@app.route('/history', methods=['GET'])
4856def listJSON():
5858 ls = glob.glob(path)
5959 def sanitize(i):
6060 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
6565if __name__ == "__main__":
66 app.run(debug=True, host='0.0.0.0')
66 app.run(debug=True, host='0.0.0.0')
6767
6868# #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
(27 / 36)
  
67646764 }
67656765 this.plot()
67666766 },
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 })
67776777 },
6778 plot: function() {
6779 this.fx.plot();
6778 plot:function(){
6779 this.fx.plot()
67806780 },
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 },
68056796 move:function(A,y){
68066797 var x=r(A.x,A.y);
68076798 if(this.busy===false&&x.norm()<0.9){ // Ajay - make it clickable upto the depth of tree DIRECTLY. Increase in value = deeper the length.
  
133133 };
134134 map = new OpenLayers.Map('map', options);
135135 $.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
148147 });
149148 var layer = new OpenLayers.Layer.TMS( "TMS Layer","static/",
  
4141 }
4242 try {
4343 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) {
4745 console.log(data);
4846 $('#json-saved').show();
4947 }, 'json');