コード例 #1
0
def create():
    name = request.form['name']
    if Product.exist(name):
        abort(400, "A product with the same name already exists")
    product = Product.create(name)
    db.session.commit()
    return jsonify(product.id)
コード例 #2
0
def remove_product():
    product = Product.get(request.form['productId'])
    beer_pub = BeerPub.get(request.form['beerPubId'])
    if product is not None and beer_pub is not None and beer_pub.has_product(product):
        beer_pub.remove_product(product)
        db.session.commit()
    return ("", http.HTTPStatus.NO_CONTENT)
コード例 #3
0
def edit_product():
    beer_pub = BeerPub.get(request.form['beerPubId'])
    product = Product.get(request.form['productId'])
    if beer_pub is not None and product is not None:
        beer_pub.change_price(product, float(request.form['price']))
        db.session.commit()
    return ("", http.HTTPStatus.NO_CONTENT)
コード例 #4
0
def add_product():
    beer_pub = BeerPub.get(request.form['beerPubId'])
    product = Product.get(request.form['productId'])

    if not beer_pub.has_product(product):
        beer_pub.add_product(product, float(request.form['price']))
        db.session.commit()
    return ("", http.HTTPStatus.NO_CONTENT)
コード例 #5
0
def delete():
    product = Product.get(request.form['id'])
    if product is None:
        return ("", http.HTTPStatus.NO_CONTENT)    
    if len(BeerPub.get_from_product(product)) != 0:
        abort(400, description="Cannot remove a product which is being used in a beer pub.") 
    product.delete()
    db.session.commit()
    return ("", http.HTTPStatus.NO_CONTENT)
コード例 #6
0
def edit():
    name = request.form['name']
    product = Product.get(request.form['id'])
    if product is None:
        abort(400, "Invalid product was given!")
    if not product.can_change_name(name):
        abort(400, "A product with the same name already exists")
    
    product.name = request.form['name']
    db.session.commit()
    return ("", http.HTTPStatus.NO_CONTENT)
コード例 #7
0
def products():
    return jsonify(jsonpickle.encode(Product.get_all(), unpicklable=False))
コード例 #8
0
def possible_new_products(beer_pub_id):
    beer_pub = BeerPub.get(beer_pub_id)
    possible_products = Product.get_all()
    if beer_pub is not None:
        possible_products = list(possible_products - set(beer_pub.get_products()))
    return jsonify(jsonpickle.encode(possible_products, unpicklable=True))
コード例 #9
0
 def get_products(self):
     return list(
         map(lambda op: Product.get(op.product_id), self.order_products))