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 conf |
10 |
|
11 |
app = flask.Flask(__name__) |
12 |
|
13 |
|
14 |
|
15 |
dbClient = pymongo.MongoClient() |
16 |
db = dbClient[conf.DB] |
17 |
siteContent = db['content'] |
18 |
siteMenu = db['menu'] |
19 |
if siteMenu.find_one() == None: |
20 |
siteMenu.insert({'customMenu': False}) |
21 |
|
22 |
# handy reference to otherwise long name |
23 |
bson.ObjId = bson.objectid.ObjectId |
24 |
|
25 |
|
26 |
def getContent(): |
27 |
content = [] |
28 |
for i in siteContent.find(): |
29 |
objId = bson.ObjId(i['_id']) |
30 |
del(i['_id']) |
31 |
i['id'] = str(objId) |
32 |
content.append(i) |
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 |
|
41 |
|
42 |
|
43 |
@app.route('/', methods=['GET']) |
44 |
def index(): |
45 |
return flask.render_template('index.html', content=getContent(), |
46 |
title=config.SITE_TITLE) |
47 |
|
48 |
|
49 |
@app.route('/edit', methods=['GET']) |
50 |
def edit(): |
51 |
return flask.render_template('editor.html', content=getContent(), |
52 |
title=config) |
53 |
|
54 |
|
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): |
70 |
if flask.request.method == 'PUT': |
71 |
changedPage = flask.request.json |
72 |
print changedPage |
73 |
print '=======' |
74 |
res = siteContent.update({'_id': bson.ObjId(_id)}, |
75 |
changedPage) |
76 |
print res |
77 |
if res['err'] == None: |
78 |
print changedPage |
79 |
return flask.jsonify(status='ok', page=changedPage) |
80 |
|
81 |
elif flask.request.method == 'DELETE': |
82 |
delPage = flask.request.url |
83 |
print delPage |
84 |
print _id |
85 |
res = siteContent.remove({'_id': bson.ObjId(_id)}) |
86 |
print res |
87 |
if res['err'] == None: |
88 |
return flask.jsonify(status='ok') |
89 |
else: |
90 |
return flask.jsonify(error=res['err'], status='error') |
91 |
|
92 |
|
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 |
# |
101 |
|
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 |
121 |
app.run(debug=True, host=config.HOST, port=config.PORT) |