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