def category_edit(id=None): """ 菜系编辑 """ form = CategoryForm() form.submit.label.text = "修改" category = Category.query.get_or_404(id) if request.method == "GET": form.name.data = category.name form.order_num.data = category.order_num if form.validate_on_submit(): data = form.data category_count = Category.query.filter_by(name=data["name"]).count() if category.name != data["name"] and category_count == 1: flash("菜系已存在", "err") return redirect(url_for("admin.category_edit", id=category.id)) category.name = data["name"] category.is_recommended = int(data["order_num"]) db.session.add(category) db.session.commit() flash("菜系修改成功", "ok") return redirect(url_for("admin.category_edit", id=category.id)) return render_template("admin/category_edit.html", form=form, category=category)
def add_category(): check_admin() categories = Category.query.all() add_category = True form = CategoryForm() if form.validate_on_submit(): category = Category(name=form.name.data, description=form.description.data, category_id=form.category_id.data) try: # add category to the database db.session.add(category) db.session.commit() flash('You have successfully added a new category.') except: # in case category name already exists flash('Error: category name already exists.') # redirect to category page return redirect(url_for('admin.list_categories')) # load category template return render_template('admin/categories/category.html', action="Add", add_category=add_category, form=form, title="Add Categories", categories=categories)
def addCategory(): form = CategoryForm() if form.validate_on_submit(): data = form.data category = Category(name=data["name"]) db.session.add(category) db.session.commit() flash("添加成功", "ok") return redirect(url_for("admin.category", page=1)) return render_template("admin/categoryAdd.html", form=form)
def editCategory(id=None): form = CategoryForm() category = Category.query.get_or_404(id) if request.method == 'GET': form.name.data = category.name if form.validate_on_submit(): data = form.data category.name = data["name"] db.session.add(category) db.session.commit() flash("修改成功", "ok") return redirect(url_for("admin.category", page=1)) return render_template("admin/categoryAdd.html", form=form)
def edit_category(id): category = Category.query.filter_by(id=id).first() categoryform = CategoryForm(name=category.name,description=category.description) if categoryform.validate_on_submit(): category_name = request.form.get('name') category_description = request.form.get('description') try: Category.query.filter_by(id=id).update({Category.name:category_name,Category.description:category_description}) db.session.commit() except: flash("edit_category Database error!") return render_template("admin/editcategory.html",categoryform=categoryform,category=category) return redirect(url_for("list_category")) return render_template("admin/editcategory.html",categoryform=categoryform,category=category)
def category_add(): """ 添加菜系 """ form = CategoryForm() if form.validate_on_submit(): data = form.data # 接收数据 category = Category.query.filter_by(name=data["name"]).count() # 说明已经有这个菜系了 if category == 1: flash("菜系已存在", "err") return redirect(url_for("admin.category_add")) category = Category(name=data["name"], order_num=int(data['order_num'])) db.session.add(category) db.session.commit() flash("菜系添加成功", "ok") return redirect(url_for("admin.category_add")) return render_template("admin/category_add.html", form=form)
def edit_category(category_id=None): '''Add or Edit the category when a category id is passed, the category will be eidited. Ortherwise a new category will be created :param category_id: The id of the category ''' if category_id: category = Category.query.get(category_id) else: category = Category() form = CategoryForm() if form.validate_on_submit(): form.populate_obj(category) category.save() return redirect(url_for('admin.show_categorys')) print(form.errors) return render_template('admin/edit-category.html', form=form, category=category)
def add_category(): categoryform = CategoryForm() if categoryform.validate_on_submit(): category_name = request.form.get('name') category_description = request.form.get('description') if(category_name != None and category_description != None and category_name !="" and category_description != ""): category = Category.check_by_name(category_name) if(category is None): category = Category(name=category_name,description=category_description) else: flash("category:%s is already exit" % category) try: db.session.add(category) db.session.commit() except: flash("add_category Database error!") return render_template("admin/addcategory.html",categoryform=categoryform) return redirect(url_for('list_category')) return render_template("admin/addcategory.html",categoryform=categoryform)
def category_add(): """ 添加类别 """ form = CategoryForm() if form.validate_on_submit(): data = form.data # 接收数据 category = Category.query.filter_by(name=data["name"]).count() # 说明已经有这个类别了 if category == 1: flash("类别已存在", "err") return redirect(url_for("admin.category_add")) category = Category( name=data["name"], ) db.session.add(category) db.session.commit() addOplog("添加类别" + data["name"]) # 添加日志 flash("类别添加成功", "ok") return redirect(url_for("admin.category_add")) return render_template("admin/category_add.html", form=form)
def edit_category(id): check_admin() categories = Category.query.all() add_category = False category = Category.query.get_or_404(id) form = CategoryForm(obj=category) if form.validate_on_submit(): category.name = form.name.data category.description = form.description.data category.category_id = form.category_id.data db.session.commit() flash('You have successfully edited the category.') # redirect to the categories page return redirect(url_for('admin.list_categories')) form.description.data = category.description form.name.data = category.name form.category_id.data = category.id return render_template('admin/categories/category.html', action="Edit", add_category=add_category, form=form, categories=categories, title="Edit Category")