Newer
Older
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
from flask import render_template, url_for, request, redirect, flash
from blog import app, db, resetDB
from blog.models import User, Post, Comment, Rate
from blog.forms import RegistrationForm, LoginForm, CommentForm,PostForm, RateForm
from flask_login import login_user, logout_user, login_required, current_user
import re
@app.route("/")
@app.route("/home")
def home():
posts=Post.query.all()
TAG_RE = re.compile(r'<[^>]+>')
for post in posts:
post.shortcontent = TAG_RE.sub('', post.content)
post.shortcontent = post.shortcontent[:100]
return render_template('home.html',posts=posts)
@app.route("/allposts")
def allposts():
sort = request.args.get('sorttype')
search = request.args.get('searchtext')
if not search :
search = ''
if (sort=="asc"):
posts=Post.query.filter(Post.content.contains(search),Post.title.contains(search)).order_by(Post.date.asc())
else :
posts=Post.query.filter(Post.content.contains(search),Post.title.contains(search)).order_by(Post.date.desc())
return render_template('allposts.html',posts=posts)
@app.route("/aboutus")
def aboutus():
return render_template('aboutus.html', title='About')
@app.route("/license")
def license():
return render_template('license.html', title='License')
@app.route("/search",methods=['GET','POST'])
def search():
searchtext = request.form.get('search');
return redirect(url_for('allposts', searchtext=searchtext))
@app.route("/post/<int:post_id>")
def post(post_id):
post = Post.query.get_or_404(post_id)
comments = Comment.query.filter(Comment.post_id==post.id)
form=CommentForm()
rates=Rate.query.filter(Rate.post_id==post.id)
sRate = 0
countRate = 0
for rate in rates :
sRate = sRate + rate.rateValue
countRate = countRate + 1
if countRate == 0 :
sRate = 0
else :
sRate = sRate / countRate
sRate = int(sRate)
return render_template('post.html',post=post,comments=comments,rate=sRate,form=form)
@app.route('/postcomment/<int:post_id>',methods=['GET','POST'])
@login_required
def post_comment(post_id):
post=Post.query.get_or_404(post_id)
form=CommentForm()
if form.validate_on_submit():
db.session.add(Comment(content=form.comment.data,post_id=post.id,author_id=current_user.id))
db.session.commit()
return redirect(f'/post/{post.id}')
comments=Comment.query.filter(Comment.post_id==post.id)
rates=Rate.query.filter(Rate.post_id==post.id)
sRate = 0
countRate = 0
for rate in rates :
sRate = sRate + rate.rateValue
countRate = countRate + 1
if countRate == 0 :
sRate = 0
else :
sRate = sRate / countRate
sRate = int(sRate)
return render_template('post.html',post=post,comments=comments,rate=sRate,form=form)
@app.route('/ratepost/<int:post_id>',methods=['GET','POST'])
@login_required
def post_rate(post_id):
post=Post.query.get_or_404(post_id)
rateValue = request.args.get('rate')
rateObj=Rate.query.filter(Rate.post_id==post_id,Rate.author_id==current_user.id).first()
if rateObj :
rateObj.rateValue=rateValue
else :
db.session.add(Rate(rateValue=rateValue,post_id=post.id,author_id=current_user.id))
db.session.commit()
return "done"
@app.route("/register",methods=['GET','POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
user = User(username=form.username.data,email=form.email.data,password=form.password.data)
db.session.add(user)
db.session.commit()
flash('Registration successful!')
return redirect(url_for('home'))
return render_template('register.html',title='Register',form=form)
@app.route("/login",methods=['GET','POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user is not None and user.verify_password(form.password.data):
login_user(user)
flash('Login successful!')
return redirect(url_for('home'))
flash('Invalid email address or password, please try again')
return render_template('login.html',form=form)
return render_template('login.html',title='Login',form=form)
@app.route("/logout")
def logout():
logout_user()
return redirect(url_for('home'))
@app.route("/resetdb")
def resetdb():
resetDB.resetDB(db)
return "done"