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