def update_product(product): if hasattr(product, 'price'): product.price = round(product.price, 2) data = to_dict(product) del data[db_product_id] data = {'$set': data} return update(products, {db_product_id: product.pid}, data)
def update_quantity(idx): if not request.json or pr.db_quantity not in request.json: abort(400) temp = request.json temp[pr.db_product_id] = idx product = Product(temp) output = pdb.change_quantity(product) if isinstance(output, bool): return jsonify({"error": "Quantity can't be negative"}), 406 return jsonify(pr.to_dict(output)), 204
def update_product(idx): if not request.json or pr.db_manufacturer not in request.json or pr.db_model not in request.json or pr.db_price not in request.json: abort(400) doc = pdb.get_product(idx) if doc is None: return jsonify({"error": "Product not found"}), 404 temp = request.json temp[pr.db_product_id] = idx product = Product(temp) pdb.update_product(product) return jsonify(pr.to_dict(product)), 201
def change_quantity(product): actual_product = get_product(product.pid) if actual_product[db_quantity] + product.quantity < 0: return False data = to_dict(product) del data[db_product_id] data = {'$inc': data} update(products, {db_product_id: product.pid}, data) actual_product[ db_quantity] = actual_product[db_quantity] - product.quantity return actual_product
def add_product(): if not request.json or pr.db_manufacturer not in request.json or pr.db_model not in request.json or pr.db_price not in request.json: abort(400) product = Product(request.json) product = pdb.add_product(product) return jsonify(pr.to_dict(product)), 201
def add_product(item): if hasattr(item, 'price'): item.price = round(item.price, 2) response = insert(products, to_dict(item)) item.pid = response.inserted_id return item
def get_products(): jsons = select(products) dicts = [] for json in jsons: dicts.append(to_dict(Product(json))) return dicts
def get_product(pid): json = select_one(products, constraints={db_product_id: ObjectId(pid)}) product = Product(json) return to_dict(product)