def product_add(): form = ProductForm() tags = Tag.query.all() form.tag_id.choices = [(v.id, v.name) for v in tags] if form.validate_on_submit(): data = form.data # secure_filename 变成安全的名称 file_logo = secure_filename(form.logo.data.filename) if not os.path.exists(config.UP_DIR): os.makedirs(config.UP_DIR) # win系统不需要增加权限 # os.chmod(config.UP_DIR, "rw") logo = change_filename(file_logo) form.logo.data.save(config.UP_DIR + logo) product = Product( title=data['title'], title_en=data['title_en'], info=data['info'], info_en=data['info_en'], logo=logo, playnum=0, tag_id=int(data['tag_id']), release_time=data['release_time'], ) db.session.add(product) db.session.commit() flash('添加产品成功!', 'ok') oplog = Oplog(admin_id=session["admin_id"], ip=request.remote_addr, reason="添加产品:{}".format(data["title"], )) db.session.add(oplog) db.session.commit() return redirect(url_for('admin.product_add')) return render_template("admin/product_add.html", form=form)
def add_product(): check_admin() add_product = True form = ProductForm(request.form) if form.validate_on_submit(): product = Product(code=form.code.data, name=form.name.data, price=form.price.data, category=form.category.data) try: db.session.add(product) db.session.commit() flash('You have successfully added a new product.') except: # in case product name already exists flash('Error: product name already exists.') # redirect to departments page return redirect(url_for('admin.list_products')) # load product template return render_template('admin/products/product.html', action="Add", add_product=add_product, form=form, title="Add Product")
def product_add(): form = ProductForm() form.category_id.choices = Category.get_all_choices() form.supplier_id.choices = Supplier.get_all_choices() if form.validate_on_submit(): Product.save_product(form.supplier_id.data, form.category_id.data, form.product_name.data, form.sku.data, form.unit_price.data, form.discount.data, form.units_in_stock.data, form.description.data) Product.save_images(request.files.getlist('images'), form.sku.data) if form.save.data: return redirect(url_for('admin.panel')) else: return redirect(url_for('admin.product_add')) return render_template('admin/product_add.html', form=form)
def editProduct(id=None): form = ProductForm() product = Product.query.get_or_404(id) if request.method == 'POST': data = form.data if not os.path.exists(app.config["UP_DIR"]): os.makedirs(app.config["UP_DIR"]) os.chmod(app.config["UP_DIR"], "rw") if form.img.data != "": file_img = secure_filename(form.img.data.filename) product.img = change_filename(file_img) form.img.data.save(app.config["UP_DIR"] + product.img) product.name = data["name"] product.category_id = data["category_id"] product.price = data["price"] product.content = data["content"] db.session.add(product) db.session.commit() flash("修改成功!") return redirect(url_for("admin.editProduct", id=id)) if request.method == "GET": form.content.data = product.content return render_template("admin/productEdit.html", form=form, product=product)
def product_edit(id=None): form = ProductForm() form.logo.validators = [] product = Product.query.get_or_404(int(id)) if request.method == "GET": form.info.data = product.info form.info_en.data = product.info_en form.tag_id.data = product.tag_id if form.validate_on_submit(): data = form.data product_count = Product.query.filter_by(title=data["title"]).count() product_count_en = Product.query.filter_by( title=data["title_en"]).count() if (product_count == 1 or product_count_en == 1) and product.title != data["title"]: flash('名称已经存在!', 'err') return redirect(url_for('admin.product_edit', id=product.id)) if not os.path.exists(config.UP_DIR): os.makedirs(config.UP_DIR) # win系统不需要增加权限 # os.chmod(config.UP_DIR, "rw") if data["logo"] != "": file_logo = secure_filename(form.logo.data.filename) product.logo = change_filename(file_logo) form.logo.data.save(config.UP_DIR + product.logo) product.tag_id = data["tag_id"] product.info = data["info"] product.info_en = data["info_en"] product.title = data["title"] product.title_en = data["title_en"] product.release_time = data["release_time"] db.session.add(product) db.session.commit() flash('修改产品成功!', 'ok') oplog = Oplog(admin_id=session["admin_id"], ip=request.remote_addr, reason="修改产品{}".format(product.title)) db.session.add(oplog) db.session.commit() return redirect(url_for('admin.product_edit', id=product.id)) return render_template("admin/product_edit.html", form=form, product=product)
def product_add(): data = request.form form = ProductForm(data) if form.validate(): # 先添加产品信息 product_data = Crud.add(Product, data) if product_data: # 如果有标签信息,根据产品的id和标签ID保存关联的标签数据 if data['tags']: tags = data['tags'].split(',') tag_data = [ TagRelation(tag_type=1, relation_id=product_data.id, tag_id=v) for v in tags ] Crud.add_all(tag_data) op_log("添加产品-%s" % product_data.title) return {"code": 1, "msg": "添加成功"} return {"code": 0, "msg": '添加失败,系统错误'} return {"code": 0, "msg": form.get_errors()}
def product_edit(): if request.method == 'GET': getdata = request.args data = Crud.get_data_by_id(Product, getdata["id"]) product_data = object_to_dict(data) #替换掉产品数据里面的TAGS tags = Crud.search_data( TagRelation, and_(TagRelation.relation_id == getdata["id"], TagRelation.tag_type == 1), 'create_time') if tags: product_data['tags'] = [v.tag_id for v in tags] return {"code": 1, "data": product_data} elif request.method == "PUT": data = request.form form = ProductForm(data) if form.validate(): #移除已保存的tag tags = Crud.search_data( TagRelation, and_(TagRelation.relation_id == data["id"], TagRelation.tag_type == 1), 'create_time') if tags: del_tags = Crud.clean_all(tags) #保存修改后的信息 result = Crud.update(Product, data) # 如果有标签信息,根据产品的id和标签ID保存关联的标签数据 if data['tags']: tags = data['tags'].split(',') tag_data = [ TagRelation(tag_type=1, relation_id=data["id"], tag_id=v) for v in tags ] Crud.add_all(tag_data) if result: op_log("修改产品 #%s" % data["id"]) return {"code": 1, "msg": '修改成功'} return {"code": 0, "msg": '修改失败,系统错误'} return {"code": 0, "msg": form.get_errors()}
def edit_product(id): check_admin() add_product = False product = Product.query.get_or_404(id) form = ProductForm(obj=product) if form.validate_on_submit(): product.code = form.code.data product.name = form.name.data product.price = form.price.data product.category = form.category.data db.session.commit() flash('You have successfully edited the products.') return redirect(url_for('admin.list_products')) form.code.data = product.code form.name.data = product.name form.price.data = product.price form.category.data = product.category return render_template('admin/products/product.html', action="Edit", add_product=add_product, form=form, product=product, title="Edit Product")
def addHistory(): form = ProductForm() if request.method == 'POST': data = form.data file_img = secure_filename(form.img.data.filename) if not os.path.exists(app.config["UP_DIR"]): os.makedirs(app.config["UP_DIR"]) os.chmod(app.config["UP_DIR"], "rw") img = change_filename(file_img) form.img.data.save(app.config["UP_DIR"] + img) product = History(name=data["name"], img=img, content=data["content"]) db.session.add(product) db.session.commit() flash("添加成功!") return redirect(url_for("admin.history", page=1)) return render_template("admin/historyAdd.html", form=form)