-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
508 lines (479 loc) · 15.7 KB
/
views.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : views.py
# @Author: lvconl
# @Date : 18-2-10
#@Software : PyCharm
from flask import render_template,make_response,request,redirect,session
from sqlalchemy import and_
from main import app
from models import db,Users,Blogs,Comments,Likes
from forms import LoginForm,RegisterForm,BlogTextForm,UserInfoForm,CommentForm,UpdatePasswdForm,SearchForm
from uuid import uuid1
from config import POSTS_PER_PAGE
import base64
import hashlib
import re
import datetime
import markdown2
COOKIE_NAME = 'lvconl'
def md5(args):
pwd = hashlib.md5(bytes('admin',encoding='utf-8'))
pwd.update(bytes(args,encoding='utf-8'))
return pwd.hexdigest()
def checkUser():
id = ''
user = ''
if 'id' in session:
id = session['id']
if id == '':
id = request.cookies.get(COOKIE_NAME)
users = Users.query.filter_by(id=id).all()
if len(users) == 0:
user = ''
else:
user = users[0]
return user
@app.route('/',methods = ['GET','POST'])
def index(page = 1):
user = checkUser()
page = request.args.get('page',1,type = int)
pagination = Blogs.query.order_by(Blogs.created_at.desc()).paginate(page,per_page = POSTS_PER_PAGE,error_out = False)
blogs = pagination.items
return render_template(
"index.html",
blogs = blogs,
user = user,
base64=base64,
pagination = pagination
)
@app.route('/blog/<id>',methods=['GET','POST'])
def read_blog(id):
user = checkUser()
blogs = Blogs.query.filter_by(id = id).all()
if len(blogs) == 0:
blog = ''
else:
blog = blogs[0]
commentResult = Comments.query.filter_by(blog_id= id).order_by(Comments.created_at.desc()).all()
comments = []
for c in commentResult:
like = Likes.query.filter(and_(Likes.user_id.like(user.id),Likes.comment_id.like(c.id))).all()
if len(like):
c.canLike = True
else:
c.canLike = False
comments.append(c)
blog.htmlcontent = markdown2.markdown(blog.content)
form = CommentForm()
if request.method == 'GET':
return render_template(
"blog.html",
blog = blog,
user = user,
comments = comments,
base64=base64,
form = form,
)
if request.method == 'POST':
content = form.comment.data
comment = Comments(id = str(uuid1()),blog_id = blog.id,blog_name = blog.name,user_id = user.id,user_name = user.name,user_image = user.image,content = content)
db.session.add(comment)
db.session.commit()
return redirect('/blog/'+blog.id)
@app.route('/login',methods = ["GET","POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
email = form.name.data
password = form.password.data
remember = form.remember.data
users = Users.query.filter_by(email=email).all()
if len(users) == 0:
error = '*用户不存在'
else:
user = users[0]
if user.passwd == md5(password):
response = make_response('''<script>location.href='/';</script>''')
if remember is True:
outdate = datetime.datetime.today() + datetime.timedelta(days=30)
response.set_cookie(COOKIE_NAME,user.id,expires = outdate)
return response
else:
session['id'] = user.id
return redirect('/')
else:
error = '*密码错误'
return render_template(
'login.html',
error = error,
form = form
)
else:
return render_template(
"login.html",
form = form
)
@app.route('/signout')
def signout():
if not 'id' in session:
response = make_response('''<script>location.href='/';</script>''')
response.delete_cookie(COOKIE_NAME)
return response
else:
session['id'] = ''
return redirect('/')
@app.route('/register',methods = ['GET','POST'])
def register():
form = RegisterForm()
if request.method == 'GET':
return render_template(
'register.html',
form = form
)
name = form.name.data
email = form.email.data
password = form.password.data
password1 = form.password1.data
if name == '':
error = '*用户名不能为空'
return render_template(
'register.html',
form = form,
error = error
)
if not re.match(r'^[0-9a-zA-Z_]{0,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$',email):
error = '*暂不支持此格式的邮箱'
return render_template(
'register.html',
form=form,
error=error
)
if len(password) < 6:
error = '*密码长度过短'
return render_template(
'register.html',
form=form,
error=error
)
if password != password1:
error = '*两次输入密码不一致'
return render_template(
'register.html',
form=form,
error=error
)
users = Users.query.filter_by(email = email).all()
if len(users) > 0:
error = '*账号已存在'
return render_template(
'register.html',
form=form,
error=error
)
passwd = md5(password)
user = Users(id = str(uuid1()),email = email,passwd = passwd,name = name)
db.session.add(user)
db.session.commit()
response = make_response('''<script>location.href='/';</script>''')
response.set_cookie(COOKIE_NAME,user.id)
return response
@app.route('/blogs/create',methods = ['GET','POST'])
def blog_create():
user = checkUser()
if user is None:
return redirect('/')
form = BlogTextForm()
if request.method == 'GET':
return render_template(
'blog_edit.html',
form = form,
user = user,
base64=base64
)
if request.method == 'POST':
name = form.name.data
summary = form.summary.data
content = form.content.data
tag = form.tag.data
blog = Blogs(id = str(uuid1()),user_id = user.id,user_name = user.name,name = name,summary = summary,content = content,tag = tag)
db.session.add(blog)
db.session.commit()
return redirect('/myblogs')
@app.route('/blogs')
def manage_blogs():
user = checkUser()
if user == '' or user.admin == 0:
return redirect('/')
page = request.args.get('page', 1, type=int)
pagination = Blogs.query.order_by(Blogs.created_at.desc()).paginate(page, per_page=7, error_out=False)
blogs = pagination.items
return render_template(
'manage_blogs.html',
blogs = blogs,
user = user,
base64=base64,
pagination = pagination
)
@app.route('/blogs_edit/<id>',methods = ['GET','POST'])
def blog_edit(id):
user = checkUser()
if user == '':
return redirect('/')
form = BlogTextForm()
blog = Blogs.query.filter_by(id=id).all()[0]
if request.method == 'GET':
form.name.data = blog.name
form.summary.data = blog.summary
form.content.data = blog.content
form.tag.data = blog.tag
return render_template(
'blog_edit.html',
form = form,
base64 = base64
)
if request.method == 'POST':
name = form.name.data
summary = form.summary.data
content = form.content.data
tag = form.tag.data
Blogs.query.filter_by(id = id).update({'name':name,'summary':summary,'content':content,'tag':tag})
db.session.commit()
return redirect('/')
@app.route('/user/edit',methods = ['GET','POST'])
def user_edit():
user = checkUser()
if user == '':
return redirect('/')
form = UserInfoForm()
return render_template(
'user_edit.html',
form=form,
user=user,
base64=base64
)
@app.route('/user/editImage',methods = ['GET','POST'])
def user_editImage():
user = checkUser()
if request.method == 'POST':
image = request.files['image'].read()
Users.query.filter_by(id = user.id).update({'image':image})
Comments.query.filter_by(user_id = user.id).update({'user_image':image})
Blogs.query.filter_by(user_id = user.id).update({'user_image':image})
db.session.commit()
return redirect('/')
@app.route('/user/<id>')
def user(id):
user = checkUser()
user_info = Users.query.filter_by(id = id).all()[0]
user_blog = Blogs.query.filter_by(user_id = id).order_by(Blogs.created_at.desc()).limit(2).all()
user_comment = Comments.query.filter_by(user_id = id).order_by(Comments.created_at.desc()).limit(2).all()
form = UserInfoForm()
return render_template(
'user_index.html',
user = user,
user_info = user_info,
user_blog = user_blog,
user_comment = user_comment,
base64 = base64,
form = form
)
@app.route('/updatepasswd',methods = ['GET','POST'])
def updatepasswd():
user = checkUser()
if user == '':
return redirect('/')
form = UpdatePasswdForm()
if request.method == 'GET':
return render_template(
'updatepasswd.html',
user = user,
base64 = base64,
form = form
)
if request.method == 'POST':
email = form.email.data
originalPasswd = form.originalPasswd.data
alterPasswd = form.alterPasswd.data
reAlterPasswd = form.reAlterPasswd.data
if not email == user.email:
error = '邮箱错误'
return render_template(
'updatepasswd.html',
user=user,
base64=base64,
form=form,
error = error
)
if not md5(originalPasswd) == user.passwd:
error = '原密码错误'
return render_template(
'updatepasswd.html',
user=user,
base64=base64,
form=form,
error = error
)
if alterPasswd != reAlterPasswd:
error = '两次输入密码不一致'
return render_template(
'updatepasswd.html',
user=user,
base64=base64,
form=form,
error = error
)
Users.query.filter_by(id = user.id).update({'passwd':md5(alterPasswd)})
db.session.commit()
signout()
return redirect('/')
@app.route('/myblogs',methods = ['GET','POST'])
def myblogs(page = 1):
user = checkUser()
if user == '':
return redirect('/')
page = request.args.get('page', 1, type=int)
pagination = Blogs.query.filter_by(user_id = user.id).order_by(Blogs.created_at.desc()).paginate(page, per_page=5, error_out=False)
blogs = pagination.items
return render_template(
'person_blog.html',
user=user,
base64 = base64,
blogs = blogs,
pagination = pagination
)
@app.route('/mycomments',methods = ['GET','POST'])
def mycomments(page = 1):
user = checkUser()
if user == '':
return redirect('/')
page = request.args.get('page',1,type = int)
pagination = Comments.query.filter_by(user_id = user.id).order_by(Comments.created_at.desc()).paginate(page,per_page = 6,error_out = False)
comments = pagination.items
return render_template(
'person_comment.html',
base64 = base64,
comments = comments,
user = user,
pagination = pagination
)
@app.route('/comment/<id>',methods = ['GET','POST'])
def delete_comment(id):
user = checkUser()
if user == '':
return redirect('/')
comment = Comments.query.filter_by(id = id).all()[0]
blod_id = comment.blog_id
db.session.delete(comment)
db.session.commit()
if request.method == 'POST':
return redirect('/mycomments')
else:
return redirect('/blog/'+blod_id)
@app.route('/search',methods = ['GET','POST'])
def search():
user = checkUser()
form = SearchForm()
if request.method == 'GET' or form.searchText.data is '':
return render_template(
'search.html',
user = user,
base64 = base64,
form = form
)
else:
text = form.searchText.data
searchText = '%' + text + '%'
blogsBaseName = Blogs.query.filter(Blogs.name.ilike(searchText)).all()
blogsBaseSummary = Blogs.query.filter(Blogs.summary.ilike(searchText)).all()
blogs = blogsBaseName + blogsBaseSummary
return render_template(
'search.html',
user=user,
base64=base64,
form=form,
blogs = blogs,
text = text
)
@app.route('/users')
def manage_users(page = 1):
user = checkUser()
if user is '' or user.admin == 0:
return redirect('/')
page = request.args.get('page',1,type = int)
pagination = Users.query.paginate(page,per_page = 6,error_out = False)
manaUser = pagination.items
return render_template(
'manage_users.html',
user = user,
manaUser = manaUser,
base64 = base64,
pagination = pagination
)
@app.route('/delete/user/<id>')
def delete_user(id):
user = checkUser()
if user.admin == 0:
return redirect('/')
u = Users.query.filter_by(id = id).all()[0]
blogs = Blogs.query.filter_by(user_id = u.id).all()
comments = Comments.query.filter_by(user_id = u.id).all()
for blog in blogs:
db.session.delete(blog)
for comment in comments:
db.session.delete(comment)
db.session.delete(u)
db.session.commit()
return redirect('/users')
@app.route('/updateToAdmin/<user_id>')
def updateToAdmin(user_id):
user = checkUser()
if user is None or user.admin == 0:
return redirect('/')
Users.query.filter_by(id = user_id).update({'admin':1})
db.session.commit()
r = make_response('''<script>alert('设置成功!');location.href = '/users';</script>''')
return r
@app.route('/tag/movie')
def movie(page = 1):
user = checkUser()
page = request.args.get('page', 1, type=int)
pagination = Blogs.query.filter_by(tag = 'movie').order_by(Blogs.created_at.desc()).paginate(page, per_page=POSTS_PER_PAGE, error_out=False)
blogs = pagination.items
return render_template(
"index.html",
blogs=blogs,
user=user,
base64=base64,
pagination=pagination
)
@app.route('/tag/technology')
def technology(page = 1):
user = checkUser()
page = request.args.get('page', 1, type=int)
pagination = Blogs.query.filter_by(tag = 'technology').order_by(Blogs.created_at.desc()).paginate(page,
per_page=POSTS_PER_PAGE,
error_out=False)
blogs = pagination.items
return render_template(
"index.html",
blogs=blogs,
user=user,
base64=base64,
pagination=pagination
)
@app.route('/tag/diary')
def diary(page = 1):
user = checkUser()
page = request.args.get('page', 1, type=int)
pagination = Blogs.query.filter_by(tag = 'diary').order_by(Blogs.created_at.desc()).paginate(page,
per_page=POSTS_PER_PAGE,
error_out=False)
blogs = pagination.items
return render_template(
"index.html",
blogs=blogs,
user=user,
base64=base64,
pagination=pagination
)