-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.py
62 lines (56 loc) · 1.92 KB
/
post.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
from flask import Blueprint, render_template, redirect, flash, url_for
from forms import WriteForm, EditForm
from models import Post
from exts import db
from translate import trans
from markupsafe import Markup
from flask_login import login_required, current_user
postbp = Blueprint('postbp', __name__)
@postbp.route('/<int:id>')
def getpost(id):
post = Post.query.filter(Post.id==id).first()
body = trans(post.body)
post = {
'title':post.title,
'body':Markup(body),
'time':post.create_time.strftime('%b %d, %Y')
}
return render_template('post.html', post=post)
@postbp.route('/new', methods=['GET', 'POST'])
@login_required
def newpost():
form = WriteForm()
if form.validate_on_submit():
post = Post(title=form.title.data, body=form.body.data,user=current_user)
db.session.add(post)
db.session.commit()
return redirect(url_for('home'))
else:
flash('can not')
return render_template('new.html', form=form)
@postbp.route('/<int:id>/edit', methods=['GET','POST'])
@login_required
def edit(id):
post = Post.query.filter(Post.id==id).first()
form = EditForm(title=post.title,body=post.body)
if current_user != post.user:
flash("You do not have this permission to edit article")
return redirect('/post/'+str(id))
if form.validate_on_submit():
post.title = form.title.data
post.body = form.body.data
db.session.commit()
return redirect('/post/'+str(id))
else:
flash("There is an error")
return render_template('edit.html', form=form, id=id)
@postbp.route('/<int:id>/delete')
@login_required
def delete(id):
post = Post.query.filter(Post.id==id).first()
if current_user != post.user:
flash("You do not have this permission to edit article")
return redirect('/post/'+str(id))
db.session.delete(post)
db.session.commit()
return redirect(url_for('manage'))