Пример #1
0
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)
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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
Пример #5
0
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
Пример #6
0
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
Пример #7
0
def get_products():
    jsons = select(products)
    dicts = []
    for json in jsons:
        dicts.append(to_dict(Product(json)))
    return dicts
Пример #8
0
def get_product(pid):
    json = select_one(products, constraints={db_product_id: ObjectId(pid)})
    product = Product(json)
    return to_dict(product)