1 |
#!/usr/bin/python |
2 |
|
3 |
# Mouchak Server - |
4 |
# A Flask Application (http://flask.pocoo.org/) |
5 |
|
6 |
import flask |
7 |
import pymongo |
8 |
import bson |
9 |
import readConfig |
10 |
|
11 |
app = flask.Flask(__name__) |
12 |
|
13 |
|
14 |
config = readConfig.readConfig() |
15 |
|
16 |
dbClient = pymongo.MongoClient() |
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 |
|
23 |
# handy reference to otherwise long name |
24 |
bson.ObjId = bson.objectid.ObjectId |
25 |
|
26 |
|
27 |
def getContent(): |
28 |
content = [] |
29 |
for i in siteContent.find(): |
30 |
objId = bson.ObjId(i['_id']) |
31 |
del(i['_id']) |
32 |
i['id'] = str(objId) |
33 |
content.append(i) |
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 |
|
42 |
|
43 |
|
44 |
@app.route('/', methods=['GET']) |
45 |
def index(): |
46 |
return flask.render_template('index.html', content=getContent(), |
47 |
title=config['site_title']) |
48 |
|
49 |
|
50 |
@app.route('/edit', methods=['GET']) |
51 |
def edit(): |
52 |
return flask.render_template('editor.html', content=getContent(), |
53 |
title=config['site_title']) |
54 |
|
55 |
|
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): |
71 |
if flask.request.method == 'PUT': |
72 |
changedPage = flask.request.json |
73 |
print changedPage |
74 |
print '=======' |
75 |
res = siteContent.update({'_id': bson.ObjId(_id)}, |
76 |
changedPage) |
77 |
print res |
78 |
if res['err'] == None: |
79 |
print changedPage |
80 |
return flask.jsonify(status='ok', page=changedPage) |
81 |
|
82 |
elif flask.request.method == 'DELETE': |
83 |
delPage = flask.request.url |
84 |
print delPage |
85 |
print _id |
86 |
res = siteContent.remove({'_id': bson.ObjId(_id)}) |
87 |
print res |
88 |
if res['err'] == None: |
89 |
return flask.jsonify(status='ok') |
90 |
else: |
91 |
return flask.jsonify(error=res['err'], status='error') |
92 |
|
93 |
|
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 |
# |
102 |
|
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 |
110 |
return flask.jsonify(status='ok',menu=changedMenu) |
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']) |