-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
407 lines (330 loc) · 13 KB
/
main.py
File metadata and controls
407 lines (330 loc) · 13 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# --coding:utf-8--
import re
# Third-party imports
import webapp2
# Project-specific imports
from forms import EditForm, LoginForm, SignupForm
from hashutils import encrypt, make_hash, make_salt
from jinjacfg import jinja_environment, resolve_msg_from_errtype
from model import GLOBAL_PARENT, User, Session, Article, Version
# Setup logging
import logging
logging.getLogger().setLevel(logging.DEBUG)
# Handlers
class BaseHandler(webapp2.RequestHandler):
template = ""
def __init__(self, request=None, response=None):
self.session = None
self.user = None
self.context = {}
super(BaseHandler, self).__init__(request, response)
# Auth methods
def sid_is_valid(self):
sid = self.request.cookies.get("sid")
if sid:
self.session = Session.by_prop('sid', sid)
if self.session and not self.session.has_expired():
return True
def dispatch(self):
super(BaseHandler, self).dispatch()
# logout_url must be initially set in overriding class; it may change
# inside handling method.
try:
self.set_logout_url(self.context['logout_url'])
except KeyError: # no logout_url was defined, will use the default
pass
def get(self, *args, **kwargs):
self.auth_wrapper(self._get, *args, **kwargs)
def post(self, *args, **kwargs):
self.auth_wrapper(self._post, *args, **kwargs)
def handle_exception(self, exception, debug):
# Correctly handle all internal server errors.
if not isinstance(exception, webapp2.HTTPException):
super(BaseHandler, self).handle_exception(exception, debug)
self.auth_wrapper(self._handle_exception, exception, debug)
def auth_wrapper(self, method, *args, **kwargs):
if self.sid_is_valid():
self.user = self.session.user
method(*args, **kwargs)
# Actual handlers
def _get(self, *args, **kwargs):
raise NotImplementedError
def _post(self, *args, **kwargs):
raise NotImplementedError
def _handle_exception(self, exception, debug):
super(BaseHandler, self).handle_exception(exception, debug)
# Render & write methods
def set_title(self):
mode_to_title = {
'new': 'New Article', 'signup': 'Sign Up', 'login': 'Login',
'view': lambda c: c['article'].head,
'edit': lambda c: '{} (edit)'.format(c['article'].head),
'history': lambda c: '{} (history)'.format(c['article'].head),
'error': lambda c: resolve_msg_from_errtype(c['error_type'])
}
mode = self.context['mode']
sep = u'—' if mode in ['view', 'edit', 'history'] else '***'
title_handler = mode_to_title[mode]
if callable(title_handler):
self.context['title'] = u'MyWiki {0} {1}'.format(
sep, title_handler(self.context))
else:
self.context['title'] = u'MyWiki {0} {1}'.format(sep, title_handler)
def render_regular_not_found(self):
self.template = 'wiki/error.html'
self.context.update(
{'mode': 'error', 'error_type': 'not_found', 'logout_url': '/'})
self.response.set_status(404)
self.render()
@staticmethod
def render_str(template, **context):
t = jinja_environment.get_template(template)
return t.render(context)
def render(self):
self.set_title()
self.write(self.render_str(self.template, **self.context))
def write(self, *args, **kwargs):
self.response.out.write(*args, **kwargs)
def redirect_with_cookie(self, path, new_cookies):
for k in self.request.cookies:
if k not in new_cookies:
self.response.delete_cookie(k)
for key, value in new_cookies.items():
self.response.set_cookie(key, value)
self.redirect(str(path))
def set_logout_url(self, url):
try:
self.session.logout_url = url
self.session.put()
except AttributeError:
pass
# Base handler for signup & login pages
class AuthPageHandler(BaseHandler):
def auth_wrapper(self, method, *args, **kwargs):
method(*args, **kwargs)
# It won't set 'self.session', 'cause it is used exclusively by signup
# and login handlers, that die after user has been authenticated
@staticmethod
def get_new_session_cookie(user):
sid = encrypt(user.name + user.password_hash + make_salt())
session = Session(sid=sid, user=user, parent=GLOBAL_PARENT)
session.put()
return {'sid': sid}
# Signup form regexps
USERNAME_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
PASSWORD_RE = re.compile(r"^.{3,20}$")
EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$")
class SignupPage(AuthPageHandler):
template = "auth/signup.html"
def dispatch(self):
self.context['mode'] = 'signup'
super(SignupPage, self).dispatch()
def _get(self):
return_to = self.request.get('return_to')
if return_to:
self.response.set_cookie('referrer', return_to)
self.context['form'] = SignupForm()
self.render()
def _post(self):
form = SignupForm(self.request.params)
redirect_path = self.request.cookies.get('referrer', '/')
if form.validate():
username = form.username.data
pwd = form.password.data
pwd_hash = make_hash(username + pwd)
user = User(name=username, password_hash=pwd_hash,
parent=GLOBAL_PARENT)
if form.email.data:
user.email = form.email.data
user.put()
self.redirect_with_cookie(
redirect_path, self.get_new_session_cookie(user))
else:
form.password.data = ''
form.verify.data = ''
self.context['form'] = form
self.render()
class LoginPage(AuthPageHandler):
template = "auth/login.html"
def dispatch(self):
self.context['mode'] = 'login'
super(LoginPage, self).dispatch()
def _get(self):
return_to = self.request.get('return_to')
if return_to:
self.response.set_cookie('referrer', return_to)
self.context['form'] = LoginForm()
self.render()
def _post(self):
form = LoginForm(self.request.params)
redirect_path = self.request.cookies.get('referrer', '/')
if form.validate():
self.redirect_with_cookie(
redirect_path, self.get_new_session_cookie(form._user))
else:
form.password.data = ''
self.context['form'] = form
self.render()
class Logout(BaseHandler):
def _get(self):
logout_url = self.session.logout_url
if self.session:
self.session.delete()
self.redirect_with_cookie(logout_url, {})
class ViewPage(BaseHandler):
template = "wiki/view_page.html"
def dispatch(self):
self.context.update({'mode': 'view', 'logout_url': self.request.url})
super(ViewPage, self).dispatch()
def _handle_exception(self, exception, debug):
if exception.status_int == 404:
self.render_regular_not_found()
else:
super(ViewPage, self)._handle_exception(exception, debug)
def _get(self, url, version=None):
if version is None:
article = Article.by_url(url)
else:
article = Article.by_url(url, version)
if article is None:
if url == '/':
default_body = (
'<p>You are free to create new articles and edit existing '
'ones.</p>')
article = Article.new(
url='/', body=default_body, head='Welcome to MyWiki!')
elif self.user is None:
self.context['url'] = url
self.abort(404)
else:
self.redirect('/_edit' + url, abort=True)
self.context.update({'article': article, 'user': self.user})
self.render()
class HistoryPage(BaseHandler):
template = "wiki/history.html"
def dispatch(self):
self.context.update(
{'mode': 'history', 'logout_url': self.request.url})
super(HistoryPage, self).dispatch()
def _handle_exception(self, exception, debug):
if exception.status_int == 404:
self.render_regular_not_found()
else:
super(HistoryPage, self)._handle_exception(exception, debug)
def _get(self, url):
article = Article.by_url(url)
if article is None:
if self.user is None:
self.abort(404)
else:
self.redirect('/_edit' + url, abort=True)
self.context.update({'article': article, 'user': self.user})
self.render()
class EditPage(BaseHandler):
template = "wiki/edit_page.html"
def dispatch(self):
self.context.update(
{'mode': 'edit',
'logout_url': self.request.url.split('_edit')[-1]})
super(EditPage, self).dispatch()
@staticmethod
def form_head_from_path(path):
words = path.strip('/').split('_')
return ' '.join([w.capitalize() for w in words])
def _get(self, url, version=None):
if self.user is None:
self.redirect_with_cookie('/login', {'referrer': self.request.url})
form = EditForm()
if version is None:
article = Article.by_url(url)
else:
article = Article.by_url(url, version)
if article is None:
if url == '/':
default_body = (
'<p>You are free to create new articles and edit existing '
'ones.</p>')
article = Article.new(
url='/', body=default_body, head='Welcome to MyWiki!')
else:
self.context.update({'mode': 'new', 'logout_url': '/'})
form.head.data = self.form_head_from_path(url)
else:
form.head.data = article.head
form.body.data = article.body
self.context.update(
{'form': form, 'article': article, 'user': self.user})
self.render()
def _post(self, url, version=None):
if self.user is None:
self.redirect('/login', abort=True)
form = EditForm(self.request.params)
if version is None:
article = Article.by_url(url)
else:
article = Article.by_url(url, version)
if article is None:
self.context['mode'] = 'new'
if form.validate():
if self.context['mode'] == 'new':
Article.new(url, form.head.data, form.body.data)
else:
article.new_version(form.head.data, form.body.data)
self.redirect(url)
else:
self.context.update(
{'user': self.user, 'form': form, 'article': article})
self.render()
class DeleteVersion(BaseHandler):
def _handle_exception(self, exception, debug):
self.template = "wiki/error.html"
self.response.set_status(exception.status_int)
self.context.update({
'mode': 'error', 'user': self.user, 'logout_url': '/'})
if exception.status_int == 403:
self.context['error_type'] = 'unauthorized_del_attempt'
if self.user is not None:
self.context.update({
'error_type': 'sole_version_del_attempt',
'logout_url': self.context['url']
})
elif exception.status_int == 404:
self.context['error_type'] = 'not_found_dv'
if self.context.pop('article_exists', False):
self.context.update({
'error_type': 'version_not_found',
'logout_url': self.context['url']
})
else:
super(DeleteVersion, self)._handle_exception(exception, debug)
self.render()
def _get(self, url, version_id):
self.context['url'] = url
if not self.user:
self.abort(403)
else:
article = Article.by_url(url, project_with_version=False)
if article is None:
self.abort(404)
version = Version.by_id(int(version_id))
if version is None or not version.belongs_to_article(article):
self.context['article_exists'] = True
self.abort(404)
# If it is article's sole version.
if version.is_first() and version.is_latest():
self.abort(403)
version.delete()
self.redirect(url)
ARTICLE_RE = r'((?:/[a-zA-Z0-9_-]*)+?)/?'
handlers = [
(r'/signup', SignupPage),
(r'/login', LoginPage),
(r'/logout', Logout),
(r'/_delete' + ARTICLE_RE + r'_version/' + r'(\d+)', DeleteVersion),
(r'/_edit' + ARTICLE_RE + r'_version/' + r'(\d+)', EditPage),
(r'/_edit' + ARTICLE_RE, EditPage),
(r'/_history' + ARTICLE_RE, HistoryPage),
(ARTICLE_RE + r'_version/' + r'(\d+)', ViewPage),
(ARTICLE_RE, ViewPage)
]
app = webapp2.WSGIApplication(handlers, debug=True)