python flask web 博客实例 评论模块 4

1  app/models.py
class Comment(db.Model):
tablename = 'comments'
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text)
body_html = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
disabled = db.Column(db.Boolean)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
@staticmethod
def on_changed_body(target, value, oldvalue, initiator):
allowed_tags = ['a', 'abbr', 'acronym', 'b', 'code', 'em', 'i','strong']
target.body_html = bleach.linkify(bleach.clean(markdown(value, output_format='html'),tags=allowed_tags, strip=True))

db.event.listen(Comment.body, 'set', Comment.on_changed_body)

2 app/models.py
class User(db.Model):
# ...
comments = db.relationship('Comment', backref='author', lazy='dynamic')
class Post(db.Model):
# ...
comments = db.relationship('Comment', backref='post', lazy='dynamic')

3 app/main/forms.py
class CommentForm(Form):
body = StringField('', validators=[Required()])
submit = SubmitField('Submit')

4 app/main/views.py
@main.route('/post/', methods=['GET', 'POST'])
def post(id):
post = Post.query.get_or_404(id)
form = CommentForm()
if form.validate_on_submit():
comment = Comment(body=form.body.data,post=post,author=current_user._get_current_object())
db.session.add(comment)
flash('Your comment has been published.')
return redirect(url_for('.post', id=post.id, page=-1))
page = request.args.get('page', 1, type=int)
if page == -1:
page = (post.comments.count() - 1) / current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1
pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],error_out=False)
comments = pagination.items
return render_template('post.html', posts=[post], form=form,comments=comments, pagination=pagination)
@main.route('/moderate')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate():
page = request.args.get('page', 1, type=int)
pagination = Comment.query.order_by(Comment.timestamp.desc()).paginate(page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],error_out=False)
comments = pagination.items
return render_template('moderate.html', comments=comments,pagination=pagination, page=page)
@main.route('/moderate/enable/')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate_enable(id):
comment = Comment.query.get_or_404(id)
comment.disabled = False
db.session.add(comment)
return redirect(url_for('.moderate',page=request.args.get('page', 1, type=int)))
@main.route('/moderate/disable/')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate_disable(id):
comment = Comment.query.get_or_404(id)
comment.disabled = True
db.session.add(comment)
return redirect(url_for('.moderate',page=request.args.get('page', 1, type=int)))

5  app/templates/base.html
{% if current_user.can(Permission.MODERATE_COMMENTS) %}

  • Moderate Comments

  • {% endif %}

    6 app/templates/moderate.html
    {% extends "base.html" %}
    {% import "_macros.html" as macros %}
    {% block title %}Flasky - Comment Moderation{% endblock %}
    {% block page_content %}


    {% set moderate = True %}
    {% include '_comments.html' %}
    {% if pagination %}

    {% endif %}
    {% endblock %}

    7 app/templates/_comments.html


    {% if comment.disabled %}

    This comment has been disabled by a moderator.


    {% endif %}
    {% if moderate or not comment.disabled %}
    {% if comment.body_html %}
    {{ comment.body_html | safe }}
    {% else %}
    {{ comment.body }}
    {% endif %}
    {% endif %}

    {% if moderate %}


    {% if comment.disabled %}
    Enable
    {% else %}
    Disable
    {% endif %}
    {% endif %}

    python flask web 博客实例 评论模块 4_第1张图片
    image.png
    python flask web 博客实例 评论模块 4_第2张图片
    image.png
    python flask web 博客实例 评论模块 4_第3张图片
    image.png

    你可能感兴趣的:(python flask web 博客实例 评论模块 4)