04640f7 by Arvind at 2013-02-07 1
from flask import Flask
2
from flask import request
3
from flask import render_template
265ce28 by Anon Ray at 2013-02-08 4
from flask import make_response
5c78a27 by Arvind at 2012-06-17 5
import json
6
import pymongo
265ce28 by Anon Ray at 2013-02-08 7
import os
8
import glob
04640f7 by Arvind at 2013-02-07 9
10
app = Flask(__name__)
11
12
@app.route('/',methods=['GET'])
13
def index():
4c68da7 by Arvind at 2013-02-08 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')
04640f7 by Arvind at 2013-02-07 18
265ce28 by Anon Ray at 2013-02-08 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'
4c68da7 by Arvind at 2013-02-08 25
    filename = os.path.join('static', filename)
26
    try:
27
      f = open(filename, 'r')
28
    except:
29
      f = open('static/test.json', 'r')
2a9c18f by Arvind at 2013-02-08 30
    buf = f.read()
31
    f.close()
32
    return render_template('editor.html', json = buf)
265ce28 by Anon Ray at 2013-02-08 33
34
@app.route('/saveJSON', methods=['POST'])
35
def saveJSON():
4c68da7 by Arvind at 2013-02-08 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
265ce28 by Anon Ray at 2013-02-08 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]
2a9c18f by Arvind at 2013-02-08 61
  ls = map(sanitize, ls)
62
  return render_template('history.html', ls=ls)
265ce28 by Anon Ray at 2013-02-08 63
64
04640f7 by Arvind at 2013-02-07 65
if __name__ == "__main__":
4c68da7 by Arvind at 2013-02-08 66
  app.run(debug=True, host='0.0.0.0')
04640f7 by Arvind at 2013-02-07 67
68
# #from bson.code import *
4c68da7 by Arvind at 2013-02-08 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)