Ejemplo n.º 1
0
def create():
    """
    创建新的文章
    @param category_id:
    @return:
    """
    # int_value_verify(category_id)
    #
    # category = CategoryService.get_by_id(category_id)
    #
    # if not category:
    #     abort(404)
    #
    # if category.show_role:
    #     if not g.user or category.show_role > g.user.role:
    #         abort(404)
    #
    # new_templates = category.create_template
    new_templates = '/create.html'

    form = PostNewForm(next=request.args.get('next', None), entry_type=1)

    if form.validate_on_submit():
        # 获取指定的表单数据
        entry = Entry()
        form.populate_obj(entry)
        category = CategoryService.get_by_id(int(entry.category_id))

        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(new_templates, form=form, category=category, current_category=category)

        flash(_("Create success"), "success")

        next_url = form.next.data

        # todo wqq: 这里能否不做跳转,送回json数据返回ID号,由前端去做处理
        if not next_url or next_url == request.path:
            next_url = url_for('portal.entry', slug=entry.slug)

        return jsonify(success="true", next_url=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")

    return render_template(new_templates, form=form)
Ejemplo 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)