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 |
# |
106 |
|
107 |
@app.route('/menu/<_id>', methods=['PUT']) |
108 |
def updateMenu(_id): |
109 |
if flask.request.method == 'PUT': |
110 |
changedMenu = flask.request.json |
111 |
print "changed menu:" |
112 |
print changedMenu |
113 |
res = siteMenu.update({'_id': bson.ObjId(_id)}, changedMenu) |
114 |
print res |
115 |
return flask.jsonify(status='ok', menu=changedMenu) |
116 |
|
117 |
#elif flask.request.method == 'DELETE': |
118 |
# delMenu = flask.request.url |
119 |
# print delMenu |
120 |
# print _id |
121 |
# res = siteMenu.remove({'_id': bson.ObjId(_id)}) |
122 |
# return flask.jsonify(status='deleted') |
123 |
|
124 |
|
125 |
# Basic login for one single admin user whose credentials are in conf.py |
126 |
@app.route('/login', methods=['GET', 'POST']) |
127 |
def login(): |
128 |
error = None |
129 |
if flask.request.method == 'POST': |
130 |
print flask.request.form |
131 |
if flask.request.form['username'] != conf.ADMIN_USERNAME: |
132 |
error = 'Invalid username' |
133 |
elif flask.request.form['password'] != conf.ADMIN_PASSWORD: |
134 |
error = 'Invaid password' |
135 |
else: |
136 |
flask.session['logged_in'] = True |
137 |
flask.session['key'] = conf.SECRET_KEY |
138 |
flask.flash('You were logged in') |
139 |
return flask.redirect(flask.url_for('edit')) |
140 |
return flask.render_template('login.html', error=error) |
141 |
|
142 |
@app.route('/logout') |
143 |
def logout(): |
144 |
flask.session.pop('logged_in', None) |
145 |
flask.flash('You were logged out') |
146 |
return flask.redirect(flask.url_for('login')) |
147 |
|
148 |
@app.route('/robots.txt') |
149 |
@app.route('/crossdomain.xml') |
150 |
def static_from_root(): |
151 |
return flask.send_from_directory(app.static_folder, request.path[1:]) |
152 |
|
153 |
|
154 |
app.config.from_object(conf) |
155 |
|
156 |
import logging,os |
157 |
from logging import FileHandler |
158 |
|
159 |
fil = FileHandler(os.path.join(os.path.dirname(__file__),'logme'),mode='a') |
160 |
fil.setLevel(logging.ERROR) |
161 |
app.logger.addHandler(fil) |
162 |
|
163 |
|
164 |
|
165 |
if __name__ == "__main__": |
166 |
app.run(debug=True, host=conf.HOST, port=conf.PORT) |