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)
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)
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)
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)
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)
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)
def products(): return jsonify(jsonpickle.encode(Product.get_all(), unpicklable=False))
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))
def get_products(self): return list( map(lambda op: Product.get(op.product_id), self.order_products))