Esempio n. 1
0
def delete(id):
    """
    删除 entry
    返回 json
    """
    int_value_verify(id)

    old_entry = EntryService.get_by_id(id)
    if not old_entry:
        return jsonify(success=False,
            redirect_url='/',
            data_id=id)

    # 防止删除别人的帖子
    if old_entry.author_id != g.user.id and not g.user.is_supervisor:
        abort(403)

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

    if next is None:
        next = url_for('portal.category', category=old_entry.category.slug)

    flash(_("The %(name)s has been deleted", name = old_entry.title), "success")

    return jsonify(success=EntryService.delete(old_entry),
        redirect_url=next,
        data_id=id)
Esempio n. 2
0
def edit(id):
    int_value_verify(id)

    entry = EntryService.get_by_id(id)
    if not entry:
        abort(404)

    if entry.author.id != g.user.id and not g.user.is_supervisor:
        abort(404)

    if entry.entry_status != EntryStatus.published and not g.user.is_supervisor:
        abort(404)

    if entry.category.show_role:
        if not g.user or entry.category.show_role > g.user.role:
            abort(404)

    category = entry.category

    edit_template = category.edit_template

    if request.method == 'GET':
        form = PostEditForm(next=request.args.get('next', None), id=id, obj=entry)
    else:
        form = PostEditForm(next=request.args.get('next', None), id=id)
        if form.validate_on_submit():
            form.populate_obj(entry)

            is_draft = False

            if form.draft.data:
                is_draft = True

            if not EntryService.add_or_update(category, entry, is_draft, True):
                flash(_("Internal error"), "failed")
                return render_template(edit_template, form=form, form_id=id, category=category, entry_type=entry.entry_type, current_category=category)

            next_url = url_for('portal.entry', slug=entry.slug)

            return redirect(next_url)
        elif form.errors:
            for error_name, error_value in form.errors.iteritems():
                print "error: %s %s" % (error_name, error_value)
            flash(_("Cause an error"), "failed")

    print "====================================================================", category.id
    return render_template(edit_template, form=form, form_id=id, category=category, entry_type=entry.entry_type, current_category=category)
Esempio n. 3
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))
Esempio n. 4
0
    def entry(self):
        from infopub.models.content.entry import EntryService

        return EntryService.get_by_id(self.entry_id)
Esempio n. 5
0
    def entry(self):
        from infopub.models.content.entry import EntryService

        return EntryService.get_by_id(self.entry_id)