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.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
54
55
@app.route('/history', methods=['GET'])
56
def listJSON():
57
  path = os.path.join('static', '*.json')
58
  ls = glob.glob(path)
59
  def sanitize(i):
60
    return i.split('/')[-1]
61
  ls = map(sanitize, ls)
62
  return render_template('history.html', ls=ls)
63
64
65
if __name__ == "__main__":
66
  app.run(debug=True, host='0.0.0.0')
67
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)