-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-server.py
277 lines (241 loc) · 8.9 KB
/
web-server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
Wiki webserver with bottlepy, markdown and sqlalchemy
"""
import os, datetime, re
import bottle
from sqlalchemy import create_engine
from bottle.ext import sqlalchemy
from beaker.middleware import SessionMiddleware
import markdown
from modeldb import Base, WikiPages, WikiVersions, Users, addUsers, addPages
#some useful links for wiki
internal_links = ('/edit', '/login', '/logout', '/info')
#cookie session
SESSION_NAME = os.getenv('SESSION_NAME','bottleserver.session')
COOKIE_CRYPT_KEY = os.getenv("COOKIE_CRYPT_KEY","123-456-789")
session_opts = {
'session.cookie_expires': 24*60*60, #in seconds -> 24 hours
'session.auto': True, # When set, calling the save() method is no longer required, and the session will be saved automatically anytime its accessed during a request.
'session.key':SESSION_NAME,
}
current_dir = os.path.dirname(__file__)
static_folder = os.path.join(current_dir,'static')
template_folder = os.path.join(current_dir,'templates')
bottle.TEMPLATE_PATH.insert(0,template_folder)
#database connect string
#DSN = 'sqlite:///:memory:'
#DSN = 'sqlite:///%s/wiki.db' % (current_dir)
#DSN = 'postgresql://ioan:***@localhost:5432/wiki'
#DSN = 'mssql://ioan:123@localhost/wiki'
#DSN = 'mssql+pymssql://ioan:123@localhost\sqlexpress/wiki'
DSN = os.getenv('DSN','sqlite:///wiki.db')
app = bottle.Bottle()
plugin = sqlalchemy.Plugin(
create_engine(DSN, echo=False), # SQLAlchemy engine created with create_engine function.
Base.metadata, # SQLAlchemy metadata, required only if create=True.
keyword='db', # Keyword used to inject session database in a route (default 'db').
create=True, # If it is true, execute `metadata.create_all(engine)` when plugin is applied (default False).
commit=True, # If it is true, plugin commit changes after route is executed (default True).
use_kwargs=True # If it is true and keyword is not defined, plugin uses **kwargs argument to inject session database (default False).
)
app.install(plugin)
def js_redirect(location):
return '<html><script>location.href="%s";</script></html>' % (location)
def auth(fn):
"""
On every call the user send a cookie
If it is the right cookie, then is authenticated, else redirect to login
"""
def check_uid(**kwargs):
#print "Authentication:",kwargs
db = kwargs.get('db')
path = kwargs.get('path')
if not path.startswith('/'):
path = '/'+path
user = bottle.request.get_cookie('account', secret=COOKIE_CRYPT_KEY)
#Password is not inside the cookie. The cookie is valid if password is correct in login form.
if db.query(Users).filter(Users.username == user).count() > 0:
return fn(**kwargs)
else:
return js_redirect("/login?back="+path)
return check_uid
def matchLink(match, db):
'''
Add class "nf" to dead links or non existent wiki pages
nf=not found
'''
group = match.group(0)
link = group[6:]
PageExists = False
if link.startswith('http'):
PageExists = True
else:
if not link.startswith('/'):
link = '/'+link
if link == '/':
PageExists = True
for i in internal_links:
if link.startswith(i):
PageExists = True
break
if 0 < db.query(WikiPages).filter(WikiPages.path == link).count():
PageExists = True
if PageExists:
return group
else:
return group + '" class="nf'
#
#webserver methods
#
@app.route('/')
def callback():
return js_redirect("/FrontPage")
@app.route('/info')
def callback():
return 'Info wiki page - <a href="/">Home</a>'
@app.route('/static/<path:path>')
def callback(path):
return bottle.static_file(path,root=static_folder)
@app.route('/add/<path:path>',method=['GET','POST'])
@bottle.view('editpage')
@auth
def callback(db, path):
'''
Add a wiki page
'''
pagepath = u'/'+path
action = bottle.request.forms.action
title = bottle.request.forms.title
body = bottle.request.forms.body
user = bottle.request.get_cookie('account', secret=COOKIE_CRYPT_KEY)
obuser = db.query(Users).filter(Users.username == user).one()
if action:
if action == "Save":
#create page + version
page = WikiPages(pagepath, 0)
db.add(page)
db.commit()
version = WikiVersions(title, body, obuser.id, page.id)
db.add(version)
db.commit()
page.version_id = version.id
db.add(page)
db.commit()
return js_redirect(pagepath)
else:
#action is cancel
return js_redirect("/")
else:
body = u"#New wiki page\n* Test\n* Hello World"
title = u'New wiki page'
return dict(user=user, title=title, body=body, path=path)
@app.route('/edit/<path:path>',method=['GET','POST'])
@bottle.view('editpage')
@auth
def callback(db, path):
'''
Edit a wiki page
'''
pagepath = u'/'+path
action = bottle.request.forms.action
title = bottle.request.forms.title
body = bottle.request.forms.body
user = bottle.request.get_cookie('account', secret=COOKIE_CRYPT_KEY)
page = db.query(WikiPages).filter(WikiPages.path == pagepath).one()
version = db.query(WikiVersions).filter(WikiVersions.id == page.version_id).one()
obuser = db.query(Users).filter(Users.username == user).one()
if action:
if action == "Save":
#update page with new version
version = WikiVersions(title, body, obuser.id, page.id)
db.add(version)
db.commit()
page.version_id = version.id
db.add(page)
db.commit()
#redirect on any action
return js_redirect(pagepath)
else:
body = version.body
title = version.title
return dict(user=user, title=title, body=body, path=path)
@app.route('/<path:path>')
@bottle.view('wikipage')
def callback(db, path):
'''
Return any wiki page, any version
'''
ver = int(bottle.request.query.get('v',0))
user = bottle.request.get_cookie('account', secret=COOKIE_CRYPT_KEY)
d_users = {}
if 0 == db.query(Users).count():
#populate table with some users
addUsers(db)
for i in db.query(Users):
d_users[i.id] = i.username
if 0 == db.query(WikiPages).count():
addPages(db)
pagepath = u'/'+path
q = db.query(WikiPages).filter(WikiPages.path == pagepath)
if 0 == q.count():
return js_redirect("/add/"+path)
else:
page = q.one()
allversions = db.query(WikiVersions).filter(WikiVersions.page_id == page.id).order_by(WikiVersions.created.desc()).all()
#try to get desired version
version = allversions[ver]
extensions = ['markdown.extensions.codehilite','markdown.extensions.extra']
body = markdown.markdown(version.body)
body = re.sub(r'href=[\'"]?([^\'" >]+)', lambda ma: matchLink(ma, db), body)
title = version.title
pageuser = d_users.get(version.user_id,'-unknown-')
created = version.created
return dict(title=title, user=user, body=body, path=path, pageuser=pageuser, created=created, allversions=allversions, d_users=d_users)
@app.route('/logout')
def callback():
bottle.response.delete_cookie("account")
return 'Logout! <a href="/">Home</a>'
@app.route('/login', method=['GET', 'POST'])
@bottle.view('login')
def callback(db):
title = "Login page"
msg = ""
back = bottle.request.forms.back #from HTML form with POST
if not back:
back = bottle.request.query.get('back','/') #from URL with GET
users = []
for i in db.query(Users):
users.append(i.username)
authenticated = False
user = bottle.request.forms.user
password = bottle.request.forms.password
if user:
q = db.query(Users).filter(Users.username == user)
if q.count() > 0:
if q.one().password == password:
#when password is correct, set the cookie
bottle.response.set_cookie("account", user, secret=COOKIE_CRYPT_KEY)
authenticated = True
else:
msg = "Invalid password"
else:
msg = "Invalid user"
if authenticated:
return js_redirect(back)
else:
return dict(title=title, users=users, msg=msg, back=back)
def main():
sess_root = SessionMiddleware(app, session_opts)
PORT = os.environ.get('PORT',8080)
HOST = os.environ.get('HOST','localhost')
if os.environ.get('BOTTLE_CHILD',None):
print "Start/Restart Server."
else:
print "Wiki Server: %s:%s" % (HOST,PORT)
#bottle.run(app=sess_root, server='paste', host=HOST, port=PORT, debug=True, reloader=True)
bottle.run(app=sess_root, server='cherrypy', host=HOST, port=PORT, debug=True, reloader=True)
if __name__ == "__main__":
try:
main()
except Exception, e:
print e