首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python flask web 博客实例 评论模块 4

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

作者头像
用户5760343
发布2019-07-05 11:09:12
4800
发布2019-07-05 11:09:12
举报
文章被收录于专栏:sktjsktjsktj

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/<int:id>', 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/<int:id>') @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/<int:id>') @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) %} <li><a href="{{ url_for('main.moderate') }}">Moderate Comments</a></li> {% endif %}

6 app/templates/moderate.html {% extends "base.html" %} {% import "_macros.html" as macros %} {% block title %}Flasky - Comment Moderation{% endblock %} {% block page_content %} <div class="page-header"> <h1>Comment Moderation</h1> </div> {% set moderate = True %} {% include '_comments.html' %} {% if pagination %} <div class="pagination"> {{ macros.pagination_widget(pagination, '.moderate') }} </div> {% endif %} {% endblock %}

7 app/templates/_comments.html <div class="comment-body"> {% if comment.disabled %} <p></p><i>This comment has been disabled by a moderator.</i></p> {% endif %} {% if moderate or not comment.disabled %} {% if comment.body_html %} {{ comment.body_html | safe }} {% else %} {{ comment.body }} {% endif %} {% endif %} </div> {% if moderate %} {% if comment.disabled %} <a class="btn btn-default btn-xs" href="{{ url_for('.moderate_enable', id=comment.id, page=page) }}">Enable</a> {% else %} <a class="btn btn-danger btn-xs" href="{{ url_for('.moderate_disable', id=comment.id, page=page) }}">Disable</a> {% endif %} {% endif %}

image.png

image.png

image.png

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档