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, 'menuOrder': []}) |
21 |
|
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=conf.SITE_TITLE, footer=conf.SITE_FOOTER) |
48 |
|
49 |
|
50 |
@app.route('/edit', methods=['GET']) |
51 |
def edit(): |
52 |
if "logged_in" in flask.session: |
53 |
flask.session['key'] = conf.SECRET_KEY |
54 |
return flask.render_template('editor.html', content=getContent(), |
55 |
title=conf.SITE_TITLE) |
56 |
else: |
57 |
return flask.redirect(flask.url_for('login')) |
58 |
|
59 |
|
60 |
@app.route('/page', methods=['POST']) |
61 |
def insertPage(): |
62 |
newpage = flask.request.json |
63 |
print newpage |
64 |
res = siteContent.insert(newpage) |
65 |
_id = bson.ObjId(res) |
66 |
newpage['id'] = str(_id) |
67 |
del(newpage['_id']) |
68 |
print newpage |
69 |
# FIXME: handle errors |
70 |
return flask.jsonify(status='ok', page=newpage) |
71 |
|
72 |
|
73 |
@app.route('/page/<_id>', methods=['PUT', 'DELETE']) |
74 |
def updatePage(_id): |
75 |
if flask.request.method == 'PUT': |
76 |
changedPage = flask.request.json |
77 |
print changedPage |
78 |
print '=======' |
79 |
res = siteContent.update({'_id': bson.ObjId(_id)}, |
80 |
changedPage) |
81 |
print res |
82 |
if res['err'] == None: |
83 |
print changedPage |
84 |
return flask.jsonify(status='ok', page=changedPage) |
85 |
|
86 |
elif flask.request.method == 'DELETE': |
87 |
delPage = flask.request.url |
88 |
print delPage |
89 |
print _id |
90 |
res = siteContent.remove({'_id': bson.ObjId(_id)}) |
91 |
print res |
92 |
if res['err'] == None: |
93 |
return flask.jsonify(status='ok') |
94 |
else: |
95 |
return flask.jsonify(error=res['err'], status='error') |
96 |
|
97 |
|
98 |
@app.route('/menu', methods=['POST']) |
99 |
def insertMenu(): |
100 |
#newmenu = flask.request.json |
101 |
#print newmenu |
102 |
#res = siteMenu.insert(newmenu) |
103 |
#print res |
104 |
#return flask.jsonify(status='success')#, content=getContent()) |
105 |
return '200 OK' |
106 |
|
107 |
|
108 |
@app.route('/menu/<_id>', methods=['PUT']) |
109 |
def updateMenu(_id): |
110 |
if flask.request.method == 'PUT': |
111 |
changedMenu = flask.request.json |
112 |
print "changed menu:" |
113 |
print changedMenu |
114 |
res = siteMenu.update({'_id': bson.ObjId(_id)}, changedMenu) |
115 |
print res |
116 |
return flask.jsonify(status='ok', menu=changedMenu) |
117 |
|
118 |
#elif flask.request.method == 'DELETE': |
119 |
# delMenu = flask.request.url |
120 |
# print delMenu |
121 |
# print _id |
122 |
# res = siteMenu.remove({'_id': bson.ObjId(_id)}) |
123 |
# return flask.jsonify(status='deleted') |
124 |
|
125 |
|
126 |
# Basic login for one single admin user whose credentials are in conf.py |
127 |
@app.route('/login', methods=['GET', 'POST']) |
128 |
def login(): |
129 |
error = None |
130 |
if flask.request.method == 'POST': |
131 |
print flask.request.form |
132 |
if flask.request.form['username'] != conf.ADMIN_USERNAME: |
133 |
error = 'Invalid username' |
134 |
elif flask.request.form['password'] != conf.ADMIN_PASSWORD: |
135 |
error = 'Invaid password' |
136 |
else: |
137 |
flask.session['logged_in'] = True |
138 |
flask.session['key'] = conf.SECRET_KEY |
139 |
flask.flash('You were logged in') |
140 |
return flask.redirect(flask.url_for('edit')) |
141 |
return flask.render_template('login.html', error=error) |
142 |
|
143 |
@app.route('/logout') |
144 |
def logout(): |
145 |
flask.session.pop('logged_in', None) |
146 |
flask.flash('You were logged out') |
147 |
return flask.redirect(flask.url_for('login')) |
148 |
|
149 |
@app.route('/robots.txt') |
150 |
@app.route('/crossdomain.xml') |
151 |
def static_from_root(): |
152 |
return flask.send_from_directory(app.static_folder, request.path[1:]) |
153 |
|
154 |
|
155 |
app.config.from_object(conf) |
156 |
|
157 |
import logging,os |
158 |
from logging import FileHandler |
159 |
|
160 |
fil = FileHandler(os.path.join(os.path.dirname(__file__),'logme'),mode='a') |
161 |
fil.setLevel(logging.ERROR) |
162 |
app.logger.addHandler(fil) |
163 |
|
164 |
|
165 |
|
166 |
if __name__ == "__main__": |
167 |
app.run(debug=True, host=conf.HOST, port=conf.PORT) |