Commit 4c68da79025a5a0c33714b3c642e96945b972bde
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.
| | | | 11 | | 11 | |
---|
12 | @app.route('/',methods=['GET']) | 12 | @app.route('/',methods=['GET']) |
---|
13 | def index(): | 13 | def index(): |
---|
14 | if request.args.has_key('json'): | | if request.args.has_key('json'): |
---|
15 | return render_template('index.html',json=request.args['json']) | | return render_template('index.html',json=request.args['json']) |
---|
16 | else: | | else: |
---|
17 | return render_template('index.html') | | 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') |
---|
18 | | 18 | |
---|
19 | @app.route('/editor', methods=['GET']) | 19 | @app.route('/editor', methods=['GET']) |
---|
20 | def editor(): | 20 | def 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) | | filename = os.path.join('static', filename) |
---|
26 | try: | | try: |
---|
27 | f = open(filename, 'r') | | f = open(filename, 'r') |
---|
28 | except: | | except: |
---|
29 | f = open('static/test.json', 'r') | | f = open('static/test.json', 'r') |
---|
30 | buf = f.read() | | buf = f.read() |
---|
31 | f.close() | | f.close() |
---|
32 | return render_template('editor.html', json = buf) | | 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) |
---|
33 | | 33 | |
---|
34 | @app.route('/saveJSON', methods=['POST']) | 34 | @app.route('/saveJSON', methods=['POST']) |
---|
35 | def saveJSON(): | 35 | def saveJSON(): |
---|
36 | if request.form: | | if request.form: |
---|
37 | data = request.form | | data = request.form |
---|
38 | JSON = data['json'] | | JSON = data['json'] |
---|
39 | filename = os.path.join('static', data['filename']) | | filename = os.path.join('static', data['filename']) |
---|
40 | f = open(filename, 'w') | | f = open(filename, 'w') |
---|
41 | f.write(JSON) | | f.write(JSON) |
---|
42 | f.close() | | f.close() |
---|
43 | return (data, 200) | | return (data, 200) |
---|
44 | else: | | else: |
---|
45 | return (json.dumps(request.form), 200) # Bad Request | | 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 |
---|
46 | | 54 | |
---|
47 | @app.route('/history', methods=['GET']) | 55 | @app.route('/history', methods=['GET']) |
---|
48 | def listJSON(): | 56 | def 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) | | ls = map(sanitize, ls) |
---|
62 | return render_template('history.html', ls=ls) | | return render_template('history.html', ls=ls) |
---|
| | 61 | ls = map(sanitize, ls) | | | 62 | return render_template('history.html', ls=ls) |
---|
63 | | 63 | |
---|
64 | | 64 | |
---|
65 | if __name__ == "__main__": | 65 | if __name__ == "__main__": |
---|
66 | app.run(debug=True, host='0.0.0.0') | | app.run(debug=True, host='0.0.0.0') |
---|
| | 66 | app.run(debug=True, host='0.0.0.0') | 67 | | 67 | |
---|
68 | # #from bson.code import * | 68 | # #from bson.code import * |
---|
69 | # #from urllib import unquote_plus | | # #from urllib import unquote_plus |
---|
70 | # def application(environ, start_response): | | # def application(environ, start_response): |
---|
71 | # status = '200 OK' | | # status = '200 OK' |
---|
72 | # response_headers = [('Content-type', 'application/json'),('Access-Control-Allow-Origin', '*')] | | # response_headers = [('Content-type', 'application/json'),('Access-Control-Allow-Origin', '*')] |
---|
73 | # start_response(status, response_headers) | | # start_response(status, response_headers) |
---|
74 | # c = pymongo.Connection() | | # c = pymongo.Connection() |
---|
75 | # db = c['mural'] | | # db = c['mural'] |
---|
76 | # coll = db['data'] | | # coll = db['data'] |
---|
77 | # ret = {} | | # ret = {} |
---|
78 | # x = 0 | | # x = 0 |
---|
79 | # for i in coll.find(): | | # for i in coll.find(): |
---|
80 | # del(i['_id']) | | # del(i['_id']) |
---|
81 | # ret[x] = i | | # ret[x] = i |
---|
82 | # x = x + 1 | | # x = x + 1 |
---|
83 | # #return repr(recieved) | | # #return repr(recieved) |
---|
84 | # return json.dumps(ret) | | # 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) |
---|
| | | | 6764 | } | 6764 | } |
---|
6765 | this.plot() | 6765 | this.plot() |
---|
6766 | }, | 6766 | }, |
---|
6767 | reposition: function() { | | reposition: function() { |
---|
6768 | this.compute("end"); | | this.compute("end"); |
---|
6769 | var w=this.graph.getNode(this.root).pos.getc().scale(-1); | | var w=this.graph.getNode(this.root).pos.getc().scale(-1); |
---|
6770 | e.Util.moebiusTransformation(this.graph,[w],["end"],"end","ignore"); | | e.Util.moebiusTransformation(this.graph,[w],["end"],"end","ignore"); |
---|
6771 | this.graph.eachNode(function(x) { | | this.graph.eachNode(function(x) { |
---|
6772 | if(x.ignore){ | | if(x.ignore){ |
---|
6773 | x.endPos.rho=x.pos.rho; | | x.endPos.rho=x.pos.rho; |
---|
6774 | x.endPos.theta=x.pos.theta | | 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() { | | plot: function() { |
---|
6779 | this.fx.plot(); | | this.fx.plot(); |
---|
| | 6778 | plot:function(){ | | | 6779 | this.fx.plot() |
---|
6780 | }, | 6780 | }, |
---|
6781 | tree_info_template: _.template($('#tree-info-template').html()), | | tree_info_template: _.template($('#tree-info-template').html()), |
---|
6782 | onClick: function(y, w) { | | onClick: function(y, w) { |
---|
6783 | // Ajay - Visual target selection | | // Ajay - Visual target selection |
---|
6784 | /*if (this.graph.getNode(y)._angularWidth <= 0.5) { | | /*if (this.graph.getNode(y)._angularWidth <= 0.5) { |
---|
6785 | if (this.graph.getNode(y).data.band == "Character") { | | if (this.graph.getNode(y).data.band == "Character") { |
---|
6786 | $("#character-value").html(this.graph.getNode(y).name); | | $("#character-value").html(this.graph.getNode(y).name); |
---|
6787 | } else if (this.graph.getNode(y).data.band == "Jewellery") { | | } else if (this.graph.getNode(y).data.band == "Jewellery") { |
---|
6788 | $("#jewellery-value").html(this.graph.getNode(y).name); | | $("#jewellery-value").html(this.graph.getNode(y).name); |
---|
6789 | } else if (this.graph.getNode(y).data.band == "Material") { | | } else if (this.graph.getNode(y).data.band == "Material") { |
---|
6790 | $("#material-value").html(this.graph.getNode(y).name); | | $("#material-value").html(this.graph.getNode(y).name); |
---|
6791 | } | | } |
---|
6792 | }*/ | | }*/ |
---|
6793 | // Ajay - end of visual target selection | | // Ajay - end of visual target selection |
---|
6794 | var node = this.graph.getNode(y); | | var node = this.graph.getNode(y); |
---|
6795 | var children = $jit.json.getSubtree(tree_json, node.id).children; | | var children = $jit.json.getSubtree(tree_json, node.id).children; |
---|
6796 | if(_.isEmpty(children)) { | | if(_.isEmpty(children)) { |
---|
6797 | $('#tree-info ul').append(this.tree_info_template({ | | $('#tree-info ul').append(this.tree_info_template({ |
---|
6798 | band: node.data.band, | | band: node.data.band, |
---|
6799 | node: node.name | | node: node.name |
---|
6800 | })); | | })); |
---|
6801 | } | | } |
---|
6802 | var x = node.pos.getc(true); | | var x = node.pos.getc(true); |
---|
6803 | this.move(x, w); | | 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. |
---|
| | | | 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; | | return; |
---|
137 | if (data.length != 0) | | if (data.length != 0) |
---|
138 | { | | { |
---|
139 | ans.ans = JSON.parse(data); | | ans.ans = JSON.parse(data); |
---|
140 | for(var i in ans.ans) | | for(var i in ans.ans) |
---|
141 | { | | { |
---|
142 | ans.count+=1; | | ans.count+=1; |
---|
143 | makeBoxes(ans.ans[i]); | | 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 | } |
---|
145 | | 144 | |
---|
146 | } | | } |
---|
| | 145 | } | 147 | | 146 | |
---|
148 | }); | 147 | }); |
---|
149 | var layer = new OpenLayers.Layer.TMS( "TMS Layer","static/", | 148 | var layer = new OpenLayers.Layer.TMS( "TMS Layer","static/", |
---|
| | | | 41 | } | 41 | } |
---|
42 | try { | 42 | try { |
---|
43 | json = JSON.parse(json); | 43 | json = JSON.parse(json); |
---|
44 | console.log(json); | | console.log(json); |
---|
45 | console.log(filename); | | console.log(filename); |
---|
46 | $.post('/saveJSON', {"json": json, "filename": filename}, function(data) { | | $.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'); |
---|