1 |
from flask import Flask |
2 |
from flask import request |
3 |
from flask import render_template |
4 |
from flask import make_response |
5 |
import json |
6 |
import pymongo |
7 |
import os |
8 |
import glob |
9 |
|
10 |
app = Flask(__name__) |
11 |
|
12 |
@app.route('/',methods=['GET']) |
13 |
def 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') |
18 |
|
19 |
@app.route('/editor', methods=['GET']) |
20 |
def editor(): |
21 |
if request.args.has_key('json'): |
22 |
filename = request.args['json'] |
23 |
else: |
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) |
33 |
|
34 |
@app.route('/saveJSON', methods=['POST']) |
35 |
def 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 |
46 |
|
47 |
@app.route('/history', methods=['GET']) |
48 |
def listJSON(): |
49 |
path = os.path.join('static', '*.json') |
50 |
ls = glob.glob(path) |
51 |
def sanitize(i): |
52 |
return i.split('/')[-1] |
53 |
ls = map(sanitize, ls) |
54 |
return render_template('history.html', ls=ls) |
55 |
|
56 |
|
57 |
if __name__ == "__main__": |
58 |
app.run(debug=True, host='0.0.0.0') |
59 |
|
60 |
# #from bson.code import * |
61 |
# #from urllib import unquote_plus |
62 |
# def application(environ, start_response): |
63 |
# status = '200 OK' |
64 |
# response_headers = [('Content-type', 'application/json'),('Access-Control-Allow-Origin', '*')] |
65 |
# start_response(status, response_headers) |
66 |
# c = pymongo.Connection() |
67 |
# db = c['mural'] |
68 |
# coll = db['data'] |
69 |
# ret = {} |
70 |
# x = 0 |
71 |
# for i in coll.find(): |
72 |
# del(i['_id']) |
73 |
# ret[x] = i |
74 |
# x = x + 1 |
75 |
# #return repr(recieved) |
76 |
# return json.dumps(ret) |