3579442 by Anon Ray at 2013-05-03 |
1 |
#!/usr/bin/python |
4a1fae7 by Anon Ray at 2013-05-09 |
2 |
# Mouchak Server - |
|
3 |
# A Flask Application (http://flask.pocoo.org/) |
|
4 |
|
3579442 by Anon Ray at 2013-05-03 |
5 |
import flask |
4a1fae7 by Anon Ray at 2013-05-09 |
6 |
import pymongo |
|
7 |
import bson |
3579442 by Anon Ray at 2013-05-03 |
8 |
|
|
9 |
app = flask.Flask(__name__) |
|
10 |
|
4a1fae7 by Anon Ray at 2013-05-09 |
11 |
dbClient = pymongo.MongoClient() |
|
12 |
db = dbClient['mouchak'] |
|
13 |
collection = db['content'] |
|
14 |
# handy reference to otherwise long name |
|
15 |
bson.ObjId = bson.objectid.ObjectId |
|
16 |
|
|
17 |
def getContent(): |
|
18 |
content = [] |
|
19 |
for i in collection.find(): |
|
20 |
objId = bson.ObjId(i['_id']) |
|
21 |
del(i['_id']) |
|
22 |
i['id'] = str(objId) |
|
23 |
content.append(i) |
|
24 |
return content |
|
25 |
|
|
26 |
|
3579442 by Anon Ray at 2013-05-03 |
27 |
@app.route('/', methods=['GET']) |
|
28 |
def index(): |
4a1fae7 by Anon Ray at 2013-05-09 |
29 |
return flask.render_template('index.html', content=getContent()) |
|
30 |
|
|
31 |
|
|
32 |
@app.route('/edit', methods=['GET', 'POST']) |
|
33 |
def edit(): |
|
34 |
if flask.request.method == 'GET': |
|
35 |
return flask.render_template('editor.html', content=getContent()) |
|
36 |
|
|
37 |
elif flask.request.method == 'POST': |
|
38 |
newpage = flask.request.json |
|
39 |
print newpage |
|
40 |
res = collection.insert(newpage) |
|
41 |
print res |
|
42 |
return flask.jsonify(status='success')#, content=getContent()) |
|
43 |
|
|
44 |
|
|
45 |
@app.route('/edit/<_id>', methods=['PUT', 'DELETE']) |
|
46 |
def editPage(_id): |
|
47 |
if flask.request.method == 'PUT': |
|
48 |
changedPage = flask.request.json |
|
49 |
print changedPage |
|
50 |
res = collection.update({'_id' : bson.ObjId(_id)}, |
|
51 |
changedPage) |
|
52 |
print res |
|
53 |
#print collection.find({'name': changed['name']}) |
|
54 |
#for i in collection.find({'name': changed['name']}): |
|
55 |
#print i |
|
56 |
return flask.jsonify(status='success')#, content=getContent()) |
|
57 |
|
|
58 |
elif flask.request.method == 'DELETE': |
|
59 |
delPage = flask.request.url |
|
60 |
print delPage |
|
61 |
print _id |
|
62 |
res = collection.remove({'_id': bson.ObjId(_id)}) |
|
63 |
print res |
|
64 |
return flask.jsonify(status='success', msg='removed') |
|
65 |
|
3579442 by Anon Ray at 2013-05-03 |
66 |
|
|
67 |
if __name__ == "__main__": |
|
68 |
app.run(debug=True, host='0.0.0.0') |
4a1fae7 by Anon Ray at 2013-05-09 |
69 |
|