def update_product(id): if Product.check_id(id) : _name = request.form.get('name') _amount = request.form.get('amount') _category = request.form.get('category') _description = request.form.get('description') file = request.files['productImage'] _productImage = file.filename target = os.path.join(APP_ROOT, 'static/images/') if not os.path.isdir(target): os.mkdir(target) file = request.files['productImage'] _productImage = file.filename ext = os.path.splitext(_productImage)[1] if (ext == ".jpg") or (ext == ".png") or (ext == ".bmp"): print("File accepted") else: return render_template("error.html", message="The selected file is not supported"), 400 destination = "/".join([target, _productImage]) print("File saved to to:", destination) file.save(destination) Product.update_product(id, _name, _amount, _category, _description, _productImage) response = Response("Product Updated", status=200, mimetype='application/json') return response res = make_response(jsonify({"error": "Incorrect Product Id"}), 404) return res
def add_product(): _name = request.form.get('name') _amount = request.form.get('amount') _category = request.form.get('category') _description = request.form.get('description') target = os.path.join(APP_ROOT, 'static/images/') if not os.path.isdir(target): os.mkdir(target) file = request.files['productImage'] _productImage = file.filename ext = os.path.splitext(_productImage)[1] if (ext == ".jpg") or (ext == ".png") or (ext == ".bmp"): print("File accepted") else: return render_template("error.html", message="The selected file is not supported"), 400 destination = "/".join([target, _productImage]) print("File saved to to:", destination) file.save(destination) Product.add_product(_name, _amount, _category, _description, _productImage) response = Response("Product added", 201, mimetype='application/json') return response
def get_product_by_id(id): if Product.check_id(id) : return_value = Product.get_product(id) return jsonify(return_value) res = make_response(jsonify({"error": "Incorrect Product Id"}), 404) return res
def post(self, request): productform = CreatProductForm(request.POST) next_url = request.GET.get("next") if request.GET.get( "next", "") else "product_manage" if productform.is_valid(): product_obj = Product(**productform.cleaned_data) try: product_obj.save() return redirect( reverse("success", kwargs={"next": urlquote_plus(next_url)})) except Exception as e: return redirect( reverse("error", kwargs={ "next": urlquote_plus(next_url), "msgs": e.args })) else: return redirect( reverse("error", kwargs={ "next": urlquote_plus(next_url), "msgs": json.dumps(json.loads( productform.errors.as_json()), ensure_ascii=False) }))
def remove_product(id): if Product.check_id(id) : Product.delete_product(id) response = Response("Product Deleted", status=200, mimetype='application/json') return response res = make_response(jsonify({"error": "Incorrect Product Id"}), 404) return res
def post(self, request): productform = ProductForm(request.POST) if productform.is_valid(): product = Product(**productform.cleaned_data) try: product.save() return redirect("success", next="server_list") except Exception as e: return redirect("error", next="server_add", msg=e.args) else: return redirect("error", next="server_add", msg=json.dumps(json.loads(productform.errors.as_json()), ensure_ascii=False))
def post(self, request): ''' 获取添加业务线表单的数据 :param request: :return: ''' print(request.POST) product_form = ProductForm(request.POST) # 验证提交的数据 # 测试: # product_form.is_valid() # return JsonResponse({'status': 'success'}) if product_form.is_valid(): # 验证数据 pro = Product(**product_form.cleaned_data) # cleaned_data 获取数据 try: pro.save() return redirect('success', next='product_manage') except Exception as e: return redirect('error', next='product_manage', msg=e.args) else: error_msg = json.dumps(json.loads(product_form.errors.as_json()), ensure_ascii=False) return redirect('error', next='product_manage', msg=error_msg)
def get_products(): return jsonify({'Products': Product.get_all_product()})