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