-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.py
403 lines (326 loc) · 10.5 KB
/
blog.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
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
#!/usr/bin/python3
import html
import re
import bottle
from models.users import users as USERS
from models.posts import posts as POSTS
from models.sessions import sessions as SESSIONS
from models.media import media as MEDIA
from bottle import SimpleTemplate, response, HTTPResponse
from PIL import Image
import time
import mimetypes
PAGESIZE = 3
app = bottle.Bottle()
SimpleTemplate.defaults["get_url"] = app.get_url
@app.route(['/', '/:page#\d+#'])
def blogIndex(page=1):
'''
The main page for the blog.
'''
page = int(page) if int(page) >= 1 else 1
prevPage = None
nextPage = None
if page > 1:
prevPage = page - 1
cookie = bottle.request.get_cookie('session')
username = SESSIONS.getUsername(cookie)
posts = POSTS.getPosts((page-1)*PAGESIZE, PAGESIZE)
if POSTS.getPosts((page)*PAGESIZE, PAGESIZE):
nextPage = page + 1
else:
nextPage = 1
return bottle.template(
'index',
dict(posts=posts,
username=username,
page=page, tag='',
nextPage=nextPage,
prevPage=prevPage)
)
@app.route(['/tag/:tag', '/tag/:tag/:page#\d+#'])
def postByTag(tag='notfound',page=1):
'''
Posts filtered by tag.
'''
page = int(page) if int(page) >= 1 else 1
prevPage = None
nextPage = None
if page > 1:
prevPage = page - 1
cookie = bottle.request.get_cookie('session')
tag = html.escape(tag)
username = SESSIONS.getUsername(cookie)
posts = POSTS.getPostsByTag(tag, (page-1)*PAGESIZE, PAGESIZE)
if POSTS.getPostsByTag(tag, (page)*PAGESIZE, PAGESIZE):
nextPage = page + 1
else:
nextPage = 1
return bottle.template(
'index',
dict(posts=posts,
username=username,
page=page, tag=tag,
nextPage=nextPage,
prevPage=prevPage)
)
@app.get('/post/<permalink>')
def showPost(permalink):
'''
Specific post by permalink.
'''
cookie = bottle.request.get_cookie('session')
username = SESSIONS.getUsername(cookie)
post = POSTS.getPostByPermalink(html.escape(permalink))
comment = {
'name': '',
'body': '',
'email': ''
}
return bottle.template(
'post',
dict(
post=post,
username=username,
errors='',
comment=comment
)
)
@app.get('/:imageType#(image|thumb)#/:permalink')
def getImage(imageType, permalink):
'''
Gets the image from the datebase for the post with the given
permalink.
'''
image = MEDIA.getMediaByPermalink(permalink, imageType)
response.content_type = image.content_type
return HTTPResponse(image)
@app.post('/newcomment')
def postNewComment():
'''
Route for posting new comment for a given post.
'''
name = bottle.request.forms.get("name")
email = bottle.request.forms.get("email")
body = bottle.request.forms.get("message")
permalink = bottle.request.forms.get("permalink")
post = POSTS.getPostByPermalink(permalink)
# if post not found, redirect to post not found error
if not post:
bottle.redirect("/post_not_found")
return
# all fields should be present
POSTS.addComment(permalink, name, email, body)
bottle.redirect("/post/" + permalink)
@app.post('/like')
def postCommentLike():
'''
Used to process a like on a blog post
'''
permalink = html.escape(bottle.request.forms.get("permalink"))
commentOrdinal = int(bottle.request.forms.get("commentOrdinal"))
post = POSTS.getPostByPermalink(permalink)
POSTS.incrementLikes(permalink, commentOrdinal)
bottle.redirect("/post/" + permalink)
@app.get('/newpost')
def getNewPost():
'''
Gets the form to allow the user to post a new post. Only works for logged-
in users.
'''
username = SESSIONS.getUsername(
bottle.request.get_cookie("session")
)
# redirect to login if user is not logged in.
if not username:
bottle.redirect('/login')
return bottle.template(
'newpost',
{
"title": "",
"post": "",
"errors": "",
"tags": "",
"username": username
}
)
@app.post('/newpost')
def postNewPost():
'''
Proccesses the new post. Only works for logged-in users.
'''
title = bottle.request.forms.get("title")
post = bottle.request.forms.get("post")
tags = bottle.request.forms.get("tags")
file = bottle.request.files.get("image")
username = SESSIONS.getUsername(
bottle.request.get_cookie("session")
)
# redirect to login if user is not logged in.
if not username:
bottle.redirect('/login')
# don't proccess if title or post is missing
if not (title and post):
errors = "Post must contain a title and blog entry"
return bottle.template("newpost",
{
"title": html.escape(title),
"username": username,
"post": html.escape(post),
"tags": tags, "errors": errors
}
)
# extract tags
whiteSpace = re.compile('\s')
noWhiteSpace = whiteSpace.sub("", tags)
cleanTags = [tag
for tag in noWhiteSpace.split(',')
]
# substitute some <p> for the paragraph breaks
newline = re.compile('\r?\n')
post = newline.sub("<p>", html.escape(post))
permalink = POSTS.insert(title, post, cleanTags, username)
if not (file and file.filename.lower().endswith(
('.jpg', '.jpeg', '.png', '.bmp', '.gif'))):
errors = "Missing or incorrect image format"
return bottle.template("newpost",
{
"title": html.escape(title),
"username": username,
"post": html.escape(post),
"tags": tags, "errors": errors
}
)
filetype = mimetypes.guess_type(file.filename)[0]
MEDIA.insert(permalink, file.file, file.filename, filetype)
# send to the post
bottle.redirect("/post/" + permalink)
@app.get('/login')
def presentLogin():
'''
Displays the login page.
'''
return bottle.template(
'login',
{
'username': "",
'password': "",
'login_error': ""
}
)
@app.post('/login')
def processLogin():
'''
Processes the login requst.
'''
user = USERS.validate(
bottle.request.forms.get("username"),
bottle.request.forms.get("password")
)
if user:
sessionId = SESSIONS.startSession(user['_id'])
if not sessionId:
bottle.redirect('/internal_error')
bottle.response.set_cookie("session", sessionId)
bottle.redirect('/')
else:
return bottle.template(
'login',
{
'username': html.escape(bottle.request.forms.get("username")),
'password': "",
'login_error': "Invalid Error!"
}
)
@app.get('/signup')
def presentSignUp():
'''
Displays the initial blog signup form.
'''
return bottle.template("signup",
{
"username": "",
"password": "",
"password_error": "",
"email": "",
"username_error": "",
"email_error": "",
"verify_error": ""
}
)
@app.post('/signup')
def processSignUp():
'''
Processes the information entered in the signup form.
'''
email = bottle.request.forms.get("email")
username = bottle.request.forms.get("username")
password = bottle.request.forms.get("password")
verify = bottle.request.forms.get("verifypassword")
errors = {
'username': html.escape(username),
'email': html.escape(email),
"password_error": "",
"username_error": "",
"email_error": "",
"verify_error": ""
}
if validateSignUp(username, password, verify, email, errors):
if not USERS.addUser(username, password, email):
# this was a duplicate
errors['username_error'] = "Username already in use. \
Please choose another username."
return bottle.template("signup", errors)
sessionId = SESSIONS.startSession(username)
bottle.response.set_cookie("session", sessionId)
bottle.redirect("/")
else:
return bottle.template("signup", errors)
@app.get('/logout')
def processLogout():
'''
Logs out the user and redirects to the home page.
'''
SESSIONS.endSession(bottle.request.get_cookie("session"))
bottle.response.set_cookie("session", "")
bottle.redirect("/")
@app.route('/<filename:path>', name='static')
def server_static(filename):
'''
Routing for static files.
'''
return bottle.static_file(
filename,
root='/Users/faisalusmani/Documents/blog/blog/views/'
)
# helpers
def validateSignUp(username, password, verify, email, errors):
'''
Validates the signup fields:
- username length is 3-20 and contains only char, numbers, _, and -
- password length is 3-20 char
- password is the same as varify
- email (if present) is proper format
'''
verified = True
# setup the regexes
userRegex = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
passRegex = re.compile(r"^.{3,20}$")
emailRegex = re.compile(r"^[\S]+@[\S]+\.[\S]+$")
if not userRegex.match(username):
errors['username_error'] = "Invalid Username. \
\n Username can contain letters, numbers, _, and -"
verified = False
if not passRegex.match(password):
errors['password_error'] = "Invalid password. \
\n Password must be between 3-20 characters long."
verified = False
if password != verify:
errors['verify_error'] = "Passwords must match."
verified = False
if email and not emailRegex.mathc(email):
errors['email_error'] = "Invalid email address."
verified = False
return verified
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True, reloader=False)