def user_recipe(id_): try: cur = conn.cursor() ingredient_id = request.form.get('id', None) text = request.form.get('text', None) food = request.form.get('food', None) measure = request.form.get('measure', None) quantity = request.form.get('quantity', None) weight = request.form.get('weight', None) # Gotta make sure the food is there if request.method in ('POST', 'PUT'): cur.execute("SELECT * FROM foods WHERE food = %s", (food,)) if cur.fetchone() is None: cur.execute("INSERT INTO foods VALUES (%s)", (food,)) if request.method == 'POST': cur.execute(IGDBConnector.ingredient_insert, (id_, text, food, measure, quantity, weight)) if request.method == 'PUT': cur.execute("UPDATE ingredients SET text=%s, food=%s, measure=%s, quantity=%s, weight=%s" "WHERE id=%s", (text, food, measure, quantity, weight, ingredient_id)) if request.method == 'DELETE': cur.execute("DELETE FROM ingredients WHERE id=%s", (ingredient_id,)) if request.method in ('PUT', 'DELETE', 'POST'): cur.execute('SELECT generate_price_for_recipe(%s)', (id_,)) cur.fetchall() conn.commit() # Both put and delete requests are done via ajax so we don't need to return html if request.method in ('PUT', 'DELETE'): return 'success', 200 # Display new data cur.execute("SELECT * FROM recipes WHERE username = %s and id = %s", (flask_login.current_user.name, id_)) r = cur.fetchone() if r is None: return render_template('user_recipe.html', error='No recipe found') cur.execute("SELECT * FROM ingredients WHERE recipe = %s", (id_,)) ingredients = [IGDBConnector.ingredient_row_to_dict(row) for row in cur.fetchall()] return render_template('user_recipe.html', recipe=IGDBConnector.recipe_row_to_dict(r), ingredients=ingredients) except errors.Error as e: return render_template('user_recipe.html', error=str(e))
def account(): try: cur = conn.cursor() cur.callproc('get_recipes_for_user', (flask_login.current_user.name,)) for result in cur.stored_results(): recipes = [IGDBConnector.recipe_row_to_dict(row) for row in result] return render_template('account.html', data=recipes) except errors.Error as e: return render_template('account.html', error=str(e))