Example #1
0
def del_comment(entry_id, comment_id):
    """
    删除 回复
    """
    # 普通用户的删除
    # 管理员的删除
    # 防止删除别人的帖子
    comment = CommentService.get_by_id(comment_id)

    if comment.author_id != g.user.id and not g.user.is_supervisor:
        abort(403)

    # 子回复个数
    sub_count = CommentService.count_by_parent_id(comment_id)

    # 删除回复
    if not sub_count:
        CommentService.delete(comment)
    else:
        # 有子回复的,不能数据删除,只能删除内容
        CommentService.softdel_by_id(comment)

    # 更新回复数
    if comment.body_type in [CommentBodyType.comment, CommentBodyType.appreciation]:
        EntryService.inc_num_comments(entry_id, -1)

    if comment.body_type == CommentBodyType.comment:
        # 文章最后回复内容
        latest_comment = CommentService.get_latest_comment_by_entey(entry_id)
        if latest_comment:
            EntryService.modify_last_comment(entry_id, latest_comment.id)
        else:
            EntryService.modify_last_comment(entry_id, 0)


    next = request.args.get('next', None)

    return jsonify(success=True,
        redirect_url=next,
        comment_id=comment_id)
Example #2
0
def _add_comment(entry_id, parent_id, comment_type=CommentBodyType.comment, chunk_id=0, is_auto_save_img=True):
    entry = EntryService.get_by_id(entry_id)

    if not entry:
        abort(404)

    form = CommentNewForm()

    url_base = ''

    if form.validate_on_submit():
        # 获取指定的表单数据
        comment = Comment()
        form.populate_obj(comment)

        # 评论没内容
        if not comment.comment:
            abort(404)

        if parent_id:
            parent = CommentService.get_by_id(parent_id)
            if not parent:
                abort(404)

            if comment_type == CommentBodyType.reply:
                if parent.body_type != CommentBodyType.comment:
                    abort(404)
            else:
                comment.comment =  '<blockquote><em>%s</em>%s</blockquote> %s' %\
                               (parent.nickname, parent.comment, comment.comment)

            if comment_type == CommentBodyType.reply:
                comment.parent_id = parent.id

        comment.entry_id = entry_id
        comment.chunk_id = chunk_id

        print "===---------------------------------------------------------------------"
        print comment.author_id,comment.email ,comment.nickname,comment.homepage
        print "===---------------------------------------------------------------------"

        if g.user.is_supervisor:
            user = UserService.get_by_id(comment.author_id)
            comment.email = user.email
            comment.nickname = user.nickname
            comment.homepage = user.homepage
            print "g.user.is_supervisor============================================"
            print comment.author_id,comment.email ,comment.nickname,comment.homepage


        elif g.user:
            print "g.user==========================================================="
            print g.user.role
            print g.user.is_supervisor
            comment.author_id = g.user.id
            comment.email = g.user.email
            comment.nickname = g.user.nickname
            comment.homepage = g.user.homepage
        else:
            comment.email = request.form['email']
            comment.nickname = request.form['nickname']
            comment.homepage = request.form['homepage']
            if comment.homepage == 'http://':
                comment.homepage = app.config['SITE_URL']

                #print 'made:', comment.email, comment.nickname, comment.homepage

            if not comment.email:
                comment.email = '*****@*****.**'
            if not comment.nickname:
                comment.nickname = u'游客'
            if not comment.homepage:
                comment.homepage = '#'

        if comment_type != CommentBodyType.reply:
            comment.parent_id = 0

        if g.user and g.user.is_editor:
            comment.comment_status = 0
        else:
            if not check_in_rank(app.config['SAFE_POST_START_TIME'], app.config['SAFE_POST_END_TIME']):
                # 需要审核
                comment.comment_status = 1

        if gfw.check(comment.comment):
            # 需要审核
            comment.comment_status = 1

        comment.body_type = comment_type
        comment.created_ip = get_remote_ip()
        comment.updated_time = comment.created_time

        # slug 有用吗?
        comment.slug = str(int_now())

        if is_auto_save_img:
            comment.content =  auto_save_img(comment.comment, app.config['SITE_DOMAIN'], PHOTOS_RELATIVE_PATH)

        # 保存数据
        if not CommentService.add(comment):
            flash(_("Internal error"), "failed")
            return render_template("%s/add_comment.html" % url_base, form=form, entries=entry)

        # 文章最后回复内容
        EntryService.modify_last_comment(entry_id, comment.id)

        if comment_type == CommentBodyType.reply:
            # 点评,更新主回复时间
            CommentService.update_time(parent_id)
        elif comment_type in [CommentBodyType.comment, CommentBodyType.appreciation]:
            # 更新回复数
            EntryService.inc_num_comments(entry_id, 1)

        flash(_("Thanks for your comment"), "success")

        return redirect(url_for('portal.entry', slug=entry.slug) + '#comment-%d' % comment.id)
    return redirect(url_for('portal.entry', slug=entry.slug))