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