Exemple #1
0
def category_edit(id):
    ca = db.session.query(Category).filter(Category.id == id).one()
    form = EditCategoryForm(name=ca.name)
    if form.validate_on_submit():
        if form.delete.data == u"确认删除":
            dca = Category.query.get_or_404(id)
            try:
                db.session.delete(dca)
                db.session.commit()
                return redirect(url_for('admin.category'))
            except:
                flash(u'删除失败,请联系管理员。')
                return redirect(url_for('admin.category_edit', id=id))
        elif form.delete.data == "":
            dca = Category.query.get_or_404(id)
            dca.name = form.name.data
            try:
                db.session.add(dca)
                db.session.commit()
                return redirect(url_for('admin.category'))
            except:
                flash(u'提交失败')
                return redirect(url_for('admin.category_edit', id=id))
        else:
            flash(u'删除栏输入有误,请重新输入')
            return redirect(url_for('admin.category_edit', id=id))
    return render_template("admin/category_edit.html", form=form, id=ca.id)
Exemple #2
0
def edit_category(request):
    form = EditCategoryForm(request.POST)
    if form.is_valid():
        categoryId = form.cleaned_data.get('category_id')
        name = form.cleaned_data.get('name')
        categoryModel = CategoryModel.objects.filter(pk=categoryId).first()
        if categoryModel:
            categoryModel.name = name
            categoryModel.save(update_fields=['name'])
            return xtjson.json_result()
        else:
            return xtjson.json_params_error(message=u'该分类不存在')
    else:
        return form.get_error_response()
Exemple #3
0
def editCategory(category_id):

    form = EditCategoryForm()
    category = session.query(Category).filter_by(id=category_id).one()
    if form.validate_on_submit():
        category.name = form.name.data
        session.commit()
        flash('Category successfully edited.')
        return redirect(url_for('mainPage'))

    return render_template('editcategory.html',
                           form=form,
                           name=category.name,
                           username=get_logged_in_username())
Exemple #4
0
def edit_category():
    form = EditCategoryForm()
    if form.validate_on_submit():
        category = Category.query.get(form.category_id.data)
        if category:
            category.name = form.new_name.data
            db.session.add(category)
            db.session.commit()
            flash(u"Edit category %s successfull!" % category.name, 'success')
            return redirect(url_for('admin.index'))
        else:
            flash(
                u"Can't find category with id %s" % str(form.category_id.data),
                'error')
    return render_template('admin/edit_category.html', form=form)