3579442 by Anon Ray at 2013-05-03 1
#!/usr/bin/python
63c7919 by Anon Ray at 2013-05-30 2
4a1fae7 by Anon Ray at 2013-05-09 3
# Mouchak Server -
4
# A Flask Application (http://flask.pocoo.org/)
5
3579442 by Anon Ray at 2013-05-03 6
import flask
4a1fae7 by Anon Ray at 2013-05-09 7
import pymongo
8
import bson
668b408 by Anon Ray at 2013-07-01 9
import conf
3579442 by Anon Ray at 2013-05-03 10
11
app = flask.Flask(__name__)
12
63c7919 by Anon Ray at 2013-05-30 13
14
4a1fae7 by Anon Ray at 2013-05-09 15
dbClient = pymongo.MongoClient()
668b408 by Anon Ray at 2013-07-01 16
db = dbClient[conf.DB]
63c7919 by Anon Ray at 2013-05-30 17
siteContent = db['content']
18
siteMenu = db['menu']
19
if siteMenu.find_one() == None:
20
    siteMenu.insert({'customMenu': False})
21
4a1fae7 by Anon Ray at 2013-05-09 22
# handy reference to otherwise long name
23
bson.ObjId = bson.objectid.ObjectId
24
63c7919 by Anon Ray at 2013-05-30 25
4a1fae7 by Anon Ray at 2013-05-09 26
def getContent():
27
    content = []
63c7919 by Anon Ray at 2013-05-30 28
    for i in siteContent.find():
4a1fae7 by Anon Ray at 2013-05-09 29
        objId = bson.ObjId(i['_id'])
30
        del(i['_id'])
31
        i['id'] = str(objId)
32
        content.append(i)
63c7919 by Anon Ray at 2013-05-30 33
34
    menu = siteMenu.find_one()
35
    objId = bson.ObjId(menu['_id'])
36
    del(menu['_id'])
37
    menu['id'] = str(objId)
38
39
    return {'content': content, 'menu': menu}
40
4a1fae7 by Anon Ray at 2013-05-09 41
42
3579442 by Anon Ray at 2013-05-03 43
@app.route('/', methods=['GET'])
44
def index():
63c7919 by Anon Ray at 2013-05-30 45
    return flask.render_template('index.html', content=getContent(),
668b408 by Anon Ray at 2013-07-01 46
                                 title=config.SITE_TITLE)
4a1fae7 by Anon Ray at 2013-05-09 47
48
63c7919 by Anon Ray at 2013-05-30 49
@app.route('/edit', methods=['GET'])
4a1fae7 by Anon Ray at 2013-05-09 50
def edit():
63c7919 by Anon Ray at 2013-05-30 51
    return flask.render_template('editor.html', content=getContent(),
668b408 by Anon Ray at 2013-07-01 52
                                 title=config)
4a1fae7 by Anon Ray at 2013-05-09 53
54
63c7919 by Anon Ray at 2013-05-30 55
@app.route('/page', methods=['POST'])
56
def insertPage():
57
    newpage = flask.request.json
58
    print newpage
59
    res = siteContent.insert(newpage)
60
    _id = bson.ObjId(res)
61
    newpage['id'] = str(_id)
62
    del(newpage['_id'])
63
    print newpage
64
    # FIXME: handle errors
65
    return flask.jsonify(status='ok', page=newpage)
66
67
68
@app.route('/page/<_id>', methods=['PUT', 'DELETE'])
69
def updatePage(_id):
4a1fae7 by Anon Ray at 2013-05-09 70
    if flask.request.method == 'PUT':
71
        changedPage = flask.request.json
72
        print changedPage
63c7919 by Anon Ray at 2013-05-30 73
        print '======='
74
        res = siteContent.update({'_id': bson.ObjId(_id)},
4a1fae7 by Anon Ray at 2013-05-09 75
                                changedPage)
76
        print res
63c7919 by Anon Ray at 2013-05-30 77
        if res['err'] == None:
78
            print changedPage
79
            return flask.jsonify(status='ok', page=changedPage)
4a1fae7 by Anon Ray at 2013-05-09 80
81
    elif flask.request.method == 'DELETE':
82
        delPage = flask.request.url
83
        print delPage
84
        print _id
63c7919 by Anon Ray at 2013-05-30 85
        res = siteContent.remove({'_id': bson.ObjId(_id)})
4a1fae7 by Anon Ray at 2013-05-09 86
        print res
63c7919 by Anon Ray at 2013-05-30 87
        if res['err'] == None:
88
            return flask.jsonify(status='ok')
89
        else:
90
            return flask.jsonify(error=res['err'], status='error')
4a1fae7 by Anon Ray at 2013-05-09 91
3579442 by Anon Ray at 2013-05-03 92
63c7919 by Anon Ray at 2013-05-30 93
#@app.route('/menu', methods=['POST'])
94
#def insertMenu():
95
#    newmenu = flask.request.json
96
#    print newmenu
97
#    res = siteMenu.insert(newmenu)
98
#    print res
99
#    return flask.jsonify(status='success')#, content=getContent())
100
#
4a1fae7 by Anon Ray at 2013-05-09 101
63c7919 by Anon Ray at 2013-05-30 102
@app.route('/menu/<_id>', methods=['PUT'])
103
def updateMenu(_id):
104
    if flask.request.method == 'PUT':
105
        changedMenu = flask.request.json
106
        print changedMenu
107
        res = siteMenu.update({'_id': bson.ObjId(_id)}, changedMenu)
108
        print res
109
        return flask.jsonify(status='ok',menu=changedMenu)
110
111
    #elif flask.request.method == 'DELETE':
112
    #    delMenu = flask.request.url
113
    #    print delMenu
114
    #    print _id
115
    #    res = siteMenu.remove({'_id': bson.ObjId(_id)})
116
    #    return flask.jsonify(status='deleted')
117
118
119
if __name__ == "__main__":
120
    print config
668b408 by Anon Ray at 2013-07-01 121
    app.run(debug=True, host=config.HOST, port=config.PORT)
122