This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
350 lines (273 loc) · 9.8 KB
/
main.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
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
import os
import json
import time
import urllib
import logging
from abc import ABCMeta, abstractmethod
import webapp2
import jinja2
from model.user import User
from model.wiki import Wiki
from model.wiki_history import WikiHistory
from utils.secure import make_secure_val, check_secure_val
from utils.validate import valid_username, valid_password, valid_email
PATHS = {
'redirect': 'redirect_to'
}
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
JINJA_ENV = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_dir),
autoescape=True
)
def render_str(template, **params):
"""Render jinja template
"""
t = JINJA_ENV.get_template(template)
return t.render(params)
class BaseHandler(webapp2.RequestHandler):
"""This is Base Handler class
Now our handler will extends this class and gets some additional
functionality and configuration
"""
def write(self, *a, **kw):
"""It is simply going to write a response
"""
self.response.write(*a, **kw)
def render_str(self, template, extension='.html', **params):
"""Our helper render_str
"""
# These params is going on every render
params['title'] = 'Progrwiki'
params['user'] = self.user
return render_str(template + extension, **params)
def render(self, template, **kw):
"""It is going to render a template and
write it as response
"""
self.write(self.render_str(template, extension='.html', **kw))
def render_json(self, d):
"""It is going to write a json response
"""
json_txt = json.dumps(d)
self.response.content_type = 'application/json;charset=UTF-8'
self.write(json_txt)
def set_secure_cookie(self, name, val):
"""Method for setting private cookie
"""
cookie_val = make_secure_val(val)
# max_age=2.628e+6 (30 days)
self.response.set_cookie(name, cookie_val,
path='/', max_age=3600)
def read_secure_cookie(self, name):
"""Method for getting private cookie
"""
cookie_val = self.request.cookies.get(name)
return cookie_val and check_secure_val(cookie_val)
def login(self, user):
"""Set the cookie, so user can keep logged
"""
self.set_secure_cookie('user_id', str(user.key.id()))
def logout(self):
"""Remove the cookie, so user will logout
"""
self.response.set_cookie('user_id', '',
path='/')
def initialize(self, *a, **kw):
webapp2.RequestHandler.initialize(self, *a, **kw)
# set self.user to current user logged in
uid = self.read_secure_cookie('user_id')
self.user = uid and User.by_id(int(uid))
# if url ends with .json set selfat to json else html
class MainPage(BaseHandler):
def get(self):
self.render('main')
class SignUp(BaseHandler):
__metaclass__ = ABCMeta
def get(self):
if self.user:
self.redirect('/welcome')
path = self.request.get('redirect_to')
self.render('signup', path=path)
def post(self):
# currently no errors
have_error = False
# getting values
self.name = self.request.get('name')
self.password = self.request.get('password')
self.verify = self.request.get('verify')
self.email = self.request.get('email')
self.redirect_to = self.request.get('redirect_to')
params = dict(name=self.name,
email=self.email)
if not valid_username(self.name):
params['error_username'] = "This username is invalid"
have_error = True
if not valid_password(self.password):
params['error_password'] = "That's not a valid password."
have_error = True
elif self.password != self.verify:
params['password'] = self.password
params['error_verify'] = "Your password didn't match."
have_error = True
if not valid_email(self.email):
params['error_email'] = "This email is invalid."
have_error = True
if have_error:
self.render('signup', **params)
else:
self.done()
@abstractmethod
def done(self):
pass
class Register(SignUp):
def done(self):
# make sure that user doesn't already exist
u = User.by_name(self.name)
if u:
msg = 'That user already exist'
self.render('signup', error_username=msg)
else:
u = User.register(self.name, self.password, self.email)
u.put()
# set cookie
# and redirect to welcome
self.login(u)
self.redirect(self.redirect_to)
class Login(BaseHandler):
def get(self):
if self.user:
self.redirect('/welcome')
path = self.request.get('redirect_to')
self.render('login', path=path)
def post(self):
self.name = self.request.get('name')
self.password = self.request.get('password')
self.redirect_to = self.request.get('redirect_to')
u = User.login(self.name, self.password)
if u:
time.sleep(1)
self.login(u)
self.redirect(self.redirect_to)
else:
msg = 'Invalid login'
self.render('login', error=msg)
class Logout(BaseHandler):
def get(self):
self.logout()
path = self.request.get('redirect_to')
self.redirect('/' + path)
class EditPage(BaseHandler):
def get(self, url):
version = self.request.get('v')
# if user is not logged in redirect to login page
# if post is already exist add content and subject to template
if self.user:
wiki = Wiki.by_path(url)
if wiki:
wiki_history = WikiHistory.by_path(wiki.url)
# ?v=n
# changing versions
if version:
version = int(version)
if version > 0 and version <= len(wiki_history.history):
version -= 1
wiki = wiki_history.get_wiki(version)
else:
print('not valid')
subject = wiki.subject
content = wiki.content
else:
subject = url.replace('_', ' ') if url else ''
content = None
btntext = 'Save'
# if user access _edit page with no path
# then set path to '' instead of 'None'
# else normally url
path = url if url else ''
params = dict(wiki=wiki,
subject=subject,
content=content,
btntext=btntext,
path=path)
self.render('edit', **params)
else:
if url:
self.redirect('/login?redirect_to=/_edit/' + url)
else:
self.redirect('/login?redirect_to=/_edit/')
def post(self, url):
content = self.request.get('content')
subject = self.request.get('subject')
path = self.request.get('path')
# if path is empty create path from subject
if not path:
path = subject.replace(' ', '_')
wiki = Wiki.by_path(path)
print('subject', subject)
print('path', path)
if wiki:
wiki.update_wiki(subject, content, self.user)
wiki.put()
wiki_history = WikiHistory.by_path(wiki.url)
wiki_history.add_wiki_history(wiki)
wiki_history.put()
print(wiki_history.url)
for wiki in wiki_history.history:
print(wiki.last_contributor)
else:
wiki = Wiki.create_wiki(subject, content, self.user)
wiki.put()
wiki_history = WikiHistory.create_wiki_history(wiki)
wiki_history.put()
time.sleep(1)
self.redirect('/' + path)
# TODO: IMplemnet _history page
# TODO: /path?v=n history
class HistoryPage(BaseHandler):
def get(self, url):
wiki = Wiki.by_path(url)
if (wiki):
wiki_history = WikiHistory.by_path(wiki.url)
history = list(enumerate(wiki_history.history))
self.render('history', subject=wiki.subject,
history=reversed(history))
else:
self.redirect('/_edit/' + url)
class WikiPage(BaseHandler):
def get(self, url):
version = self.request.get('v')
wiki = Wiki.by_path(url)
# the wiki exist
if wiki:
wiki_history = WikiHistory.by_path(wiki.url)
# ?v=n
# changing versions
print(len(wiki_history.history))
print(version)
if version:
version = int(version)
if version >= len(wiki_history.history):
version = False
elif version > 0 and version < len(wiki_history.history):
wiki = wiki_history.get_wiki(version-1)
else:
print('not valid')
redirect = urllib.urlencode({PATHS['redirect']: wiki.url})
logging.info(redirect)
params = dict(wiki=wiki,
path=wiki.url,
redirect=redirect,
version=version)
self.render('wiki-page', **params)
else:
self.redirect('/_edit/' + url)
PAGE_RE = r'/([a-zA-Z0-9()_-]+)*'
app = webapp2.WSGIApplication([
('/', MainPage),
('/register', Register),
('/login', Login),
('/logout', Logout),
('/_edit' + PAGE_RE, EditPage),
('/_history' + PAGE_RE, HistoryPage),
(PAGE_RE, WikiPage)
], debug=True)