1 |
from flask import Flask |
2 |
from flask import request |
3 |
from flask import render_template |
4 |
from flask import make_response |
5 |
from logging import FileHandler |
6 |
import json |
7 |
import pymongo |
8 |
import os |
9 |
import glob |
10 |
|
11 |
app = Flask(__name__) |
12 |
|
13 |
@app.route('/',methods=['GET']) |
14 |
def index(): |
15 |
if request.args.has_key('json'): |
16 |
return render_template('index.html',json=request.args['json']) |
17 |
else: |
18 |
return render_template('index.html') |
19 |
|
20 |
@app.route('/editor', methods=['GET']) |
21 |
def editor(): |
22 |
if request.args.has_key('json'): |
23 |
filename = request.args['json'] |
24 |
else: |
25 |
filename = 'test.json' |
26 |
filename = os.path.join('static', filename) |
27 |
try: |
28 |
f = open(filename, 'r') |
29 |
except: |
30 |
f = open('static/test.json', 'r') |
31 |
buf = f.read() |
32 |
f.close() |
33 |
return render_template('editor.html', json = buf) |
34 |
|
35 |
@app.route('/saveJSON', methods=['POST']) |
36 |
def saveJSON(): |
37 |
if request.method == 'POST': |
38 |
response = make_response() |
39 |
JSON = request.form['json'] |
40 |
filename = os.path.join('static', request.form['filename']) |
41 |
ls = glob.glob(filename) |
42 |
if len(ls) > 0: |
43 |
response.status_code = 409 |
44 |
response.status ="409 Conflict" |
45 |
response.data = "The file that you were trying to save already exits, please try a different name." |
46 |
return response |
47 |
f = open(filename, 'w') |
48 |
f.write(JSON) |
49 |
f.close() |
50 |
return response |
51 |
else: |
52 |
response = make_response() |
53 |
response.code = 400 |
54 |
return reponse |
55 |
|
56 |
@app.route('/history', methods=['GET']) |
57 |
def listJSON(): |
58 |
path = os.path.join('static', '*.json') |
59 |
ls = glob.glob(path) |
60 |
def sanitize(i): |
61 |
return i.split('/')[-1] |
62 |
ls = map(sanitize, ls) |
63 |
return render_template('history.html', ls=ls) |
64 |
|
65 |
#Log the errors, don't depend on apache to log it for you. |
66 |
fil = FileHandler(os.path.join(os.path.dirname(__file__),'logme'),mode='a') |
67 |
fil.setLevel(logging.ERROR) |
68 |
app.logger.addHandler(fil) |
69 |
|
70 |
|
71 |
if __name__ == "__main__": |
72 |
app.run(debug=True, host='0.0.0.0') |
73 |
|
74 |
# #from bson.code import * |
75 |
# #from urllib import unquote_plus |
76 |
# def application(environ, start_response): |
77 |
# status = '200 OK' |
78 |
# response_headers = [('Content-type', 'application/json'),('Access-Control-Allow-Origin', '*')] |
79 |
# start_response(status, response_headers) |
80 |
# c = pymongo.Connection() |
81 |
# db = c['mural'] |
82 |
# coll = db['data'] |
83 |
# ret = {} |
84 |
# x = 0 |
85 |
# for i in coll.find(): |
86 |
# del(i['_id']) |
87 |
# ret[x] = i |
88 |
# x = x + 1 |
89 |
# #return repr(recieved) |
90 |
# return json.dumps(ret) |