def setUp(self): """Create test client, add sample data.""" db.drop_all() db.create_all() self.uid = 94566 u = User.signup("testing", "*****@*****.**", "password", None) u.id = self.uid db.session.commit() self.u = User.query.get(self.uid) self.client = app.test_client() self.d1id = 5555555 d1 = Drink(name="TestDrink", ingredients="[gin, tonic water]", instructions="Mix ingredients", image=None) d1.id = self.d1id db.session.add(d1) db.session.commit() self.d1 = Drink.query.get(self.d1id)
def create_drink(jwt): body = request.get_json() if body is None: abort(400) drink_title = body.get('title') drink = Drink(title=drink_title) drink.insert() return jsonify({'success': True, 'drinks': [drink.format()]})
def add_drink(): payload = request.get_json() title = payload["title"] if drink := Drink.find_by(title): abort( Response( response=f"Drink '{title}' already exists!", status=405, ))
def create_detail(jwt): ''' Add a new drink in the drinks table ''' body = request.get_json() new_title = body['title'] recipe_json = body['recipe'] new_drink = Drink(title=new_title, recipe=json.dumps(recipe_json)) new_drink.insert() return jsonify({'success': True, 'drinks': [new_drink.long()]})
def add_drinks(payload): body = request.get_json() new_name = body.get('name') new_price = body.get('price') new_quantity = body.get('quantity') if new_name is None or new_price is None or new_quantity is None: abort(400) try: drink = Drink(new_name, new_price, new_quantity) drink.insert() return jsonify({"success": True}) except BaseException: abort(422)
def drink_post(jwt): body = dict(request.form or request.json or request.data) new_drink_title = body.get("title", None) new_recipe_drink = body.get("recipe", None) if new_drink_title is '': abort(422) try: drink = Drink(title=new_drink_title, recipe=json.dumps([new_recipe_drink])) drink.insert() return( json.dumps({"success": True, "newly_created_drink": drink.long()}), 200) except Exception: abort(422)
def test_drink_remove(self): new_drink = Drink(name='Delete Drink', content="Alcoholic", instructions='Delete instructions') new_drink.id = 10001 db.session.add(new_drink) new = Drink.query.get(10001) db.session.delete(new) check_delete = Drink.query.filter(Drink.name == 'Delete Drink').first() self.assertEqual(check_delete, None)
def add_drink(token): body = request.get_json() drink_title = body.get('drink_title', None) if drink_title is None: abort(422) try: drink = Drink(title=drink_title) drink.insert() return jsonify({ 'status': 200, 'success': True, }) except Exception: abort(422)
def create_new_drink(self): body = request.get_json() new_title = body.get('title', None) new_recipe = body.get('recipe', None) new_drink = Drink(title=new_title, recipe=json.dumps(new_recipe)) new_drink.insert() drinks_all = Drink.query.all() drinks = [drink.long() for drink in drinks_all] return jsonify({ "success": True, "drinks": drinks, })
def save_recipe(): """Adds the drink to the db, if not there currently and then saves the recipe for the user""" if not g.user: flash('You must be logged into an account to save a recipe', 'danger') return redirect('/') name=request.form["name"] ingredients=request.form["ingredients"] instructions=request.form["instructions"] image=request.form["image"] #if the drink is already in the db if Drink.query.filter_by(name=name).first(): drink=Drink.query.filter_by(name=name).first() #if the drink is not saved as an instance of Drink in the db else: drink=Drink(name=name, ingredients=ingredients, instructions=instructions, image=image) db.session.add(drink) db.session.commit() if Saved_recipe.query.filter_by(user_id=g.user.id, drink_id=drink.id).all(): flash(f"You have already saved the recipe for {drink.name}", "danger") return redirect('/') #now make the drink a saved recipe for the user saved=Saved_recipe(drink_id=drink.id, user_id=g.user.id) db.session.add(saved) db.session.commit() #on the saved page, we want ALL of the particular user's saved recipes to return redirect(f"/users/{g.user.id}/saved") #might not want to jump right to the page
def add_favorite(drink_id): """Favorite drink with current user.""" if not g.user: flash("Access unauthorized.", "danger") return redirect("/") user = g.user new_drink = Drink(id=drink_id) db.session.add(new_drink) db.session.commit() favorited = [f.id for f in user.favorites] favorited_drinks = Drink.query.filter(Drink.id.in_(favorited)).all() if drink_id in favorited: favorite = Favorite.query.filter( Favorite.user_id == user.id, Favorite.drink_id == drink_id).first() db.session.delete(favorite) db.session.commit() return redirect("/") new_favorite = Favorite(user_id=g.user.id, drink_id=drink_id) db.session.add(new_favorite) db.session.commit() return redirect(f"/users/{user.id}")
def test_drink_add(self): new_drink = Drink(name='Test Drink', content="Alcoholic", instructions='Test instructions') new_drink.id = 10000 db.session.add(new_drink) new = Drink.query.get(10000) self.assertEqual(new.id, 10000) self.assertEqual(new.name, 'Test Drink') self.assertEqual(new.content, 'Alcoholic') self.assertEqual(new.instructions, 'Test instructions')
def create_registry(self): reg = DrinkRegistry() for a in config.alcohols: reg.add(Alcohol(a)) for m in config.mixers: reg.add(Mixer(m)) for d in config.drinks: drink = Drink(d["name"], d["description"]) for i in d["ingredients"]: ingredient = reg.get_ingredient(i["name"]) drink.add(ingredient, i["amount"]) reg.add(drink) self.reg = reg
def test_change_drink_info(self): change_drink = Drink(name='Change Drink', content="Alcoholic", instructions='Change instructions') change_drink.id = 10002 db.session.add(change_drink) db.session.commit() change = Drink.query.get(10002) change.content = 'Non-alcoholic' db.session.commit() change_check = Drink.query.get(10002) self.assertEqual(change_check.content, 'Non-alcoholic') db.session.delete(change_check) db.session.commit()
def add_drink(): if request.method == 'POST': print(request.json) date = request.json['date'] btype = request.json['btype'] name = request.json['name'] place = request.json['place'] try: drink = Drink(date=date, btype=btype, name=name, place=place) print(date) db.session.add(drink) db.session.commit() return "Drink added. Drink id={}".format(drink.id) except Exception as e: return (str(e))
def add_to_favorite(drink_id): """Adds drink to users profile""" username1 = session['user_id'] user = User.query.filter_by(username=username1).first() if user: user_num = user.id else: print("didnt find user") print(user) new_drink = [Drink(id=drink_id, user_id=user_num)] db.session.add_all(new_drink) db.session.commit() return redirect("/search")
def parse_input_file(filename): with open(filename, 'r') as fh: data = json.load(fh) machine = data['machine'] n_outlets = machine['outlets']['count_n'] initial_ingredients = machine["total_items_quantity"] coffee_machine = CoffeeMachine(n_outlets, initial_ingredients) beverages = machine['beverages'] beverages_list = [] for beverage_name in beverages: ingredients = beverages[beverage_name] beverages_list.append(Drink(beverage_name, ingredients)) return coffee_machine, beverages_list
def add_new_drink(): if request.method == 'POST': date = request.form.get('date') btype = request.form.get('btype') name = request.form.get('name') try: drink = Drink( date=date, btype=btype, name=name, ) db.session.add(drink) db.session.commit() return "Drink added. Drink id={}".format(drink.id) except Exception as e: return (str(e)) return render_template("getData.html")
def init(request): drink1 = Drink(name='Mojito', description='Yum!', image='mojito.jpg') drink1.save() drink2 = Drink(name='Cuba Libre', description='Yuck!', image='cuba.jpg') drink2.save() i1 = Ingredient(name='Juice', description='So svalk', unit='kg') i1.save() i2 = Ingredient(name='Vatten', description='Such wet', unit='doge') i2.save() i3 = Ingredient(name='Vindruvor', description='Many rund', unit='pi') i3.save() i4 = Ingredient(name='Milk', description='Very ko', unit='mil-k') i4.save() Recipe(drink=drink1, ingredient=i1, amount=20).save() Recipe(drink=drink1, ingredient=i2, amount=20).save() Recipe(drink=drink1, ingredient=i4, amount=20, note="smaskens!").save() Recipe(drink=drink2, ingredient=i2, amount=20).save() Recipe(drink=drink2, ingredient=i4, amount=20, note="katt").save() return main(request)
def update_drink(id): payload = request.get_json() if drink := Drink.find(id): if title := payload["title"]: drink.title = title
def drinks_detail(): if drinks := Drink.all(True): return jsonify({"success": True, "drinks": drinks})
def remove_drink(id): if drink := Drink.find(id): drink.delete() return jsonify({"success": True, "delete": id})
""" @app.route("/drinks", methods=["POST"]) @requires_auth("post:drinks") def add_drink(): payload = request.get_json() title = payload["title"] if drink := Drink.find_by(title): abort( Response( response=f"Drink '{title}' already exists!", status=405, )) else: recipe = json.dumps(payload["recipe"]) drink = Drink(title=title, recipe=recipe) drink.insert() return jsonify({"success": True, "drinks": drink.long()}) """ TODO-DONE: implement endpoint PATCH /drinks/<id> where <id> is the existing model id it should respond with a 404 error if <id> is not found it should update the corresponding row for <id> it should require the 'patch:drinks' permission it should contain the drink.long() data representation returns status code 200 and json {"success": True, "drinks": drink} where drink an array containing only the updated drink or appropriate status code indicating reason for failure """