views.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:MagicPress 作者: huang-zp 项目源码 文件源码
def article(article_id):
    comment_form = CommentForm()
    if comment_form.validate_on_submit():
        from MagicPress.utils.tasks import send_async_email
        new_comment = Comment(username=comment_form.name.data)
        new_comment.text = comment_form.text.data
        new_comment.create_time = datetime.utcnow()
        new_comment.site = comment_form.site.data
        new_comment.email = comment_form.email.data
        new_comment.ip = request.remote_addr
        new_comment.language = request.accept_languages.best
        new_comment.os = request.user_agent.platform
        new_comment.browser = request.user_agent.browser
        new_comment.article_id = str(request.base_url).split('/')[-1]
        info = get_ip_info(request.remote_addr)
        new_comment.location = info['country']+info['region']+info['city']
        new_comment.network = info['isp']
        if gfw.filter(comment_form.text.data) or gfw.filter(comment_form.name.data):
            new_comment.hidden = False
            flash(u'????????????')
        else:
            new_comment.hidden = True

        message_details = {}
        message_details['subject'] = 'New Comment'
        message_details['recipients'] = [current_app.config['ADMIN_EMAIL']]
        message_details['body'] = "Name:     %s\nEmail:     %s\nSite:    %s\nLocation:    %s\n" \
                                  "Hihhen:    %s\nText:\n\n             %s" % (
            comment_form.name.data, current_app.config['ADMIN_EMAIL'], comment_form.site.data,
            info['country']+info['region']+info['city'], str(new_comment.hidden), comment_form.text.data)
        send_async_email.delay(message_details)
        db.session.add(new_comment)
        db.session.commit()
    the_article = Article.query.filter_by(id=article_id).first()
    next_article = db.session.query(Article).filter(Article.id < article_id, Article.state == True).order_by(Article.id.desc()).first()
    pre_article = db.session.query(Article).filter(Article.id > article_id, Article.state == True).order_by(Article.id.asc()).first()
    comments = Comment.query.filter_by(article_id=article_id, hidden=True).all()
    return render_template(get_theme() + '/article.html', article=the_article, next_article=next_article,
                           pre_article=pre_article, comment_form=comment_form, comments=comments)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号