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 import_sample_recipe_and_grocery(): """ Made into a function to avoid polluting global scope :return: """ connection = IGDBConnector.connect() grab_recipe_data(connection) generate_fake_grocery_data(connection) generate_recipe_prices(connection) connection.close()
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))
def recipe(): query = request.args.get('q', '') include_foods = request.args.get('include', None) exclude_foods = request.args.get('exclude', None) min_price = request.args.get('min', 0) max_price = request.args.get('max', 9001) sort = request.args.get('sort', 'ASC') # Sanitize inputs if include_foods == '': include_foods = None if exclude_foods == '': exclude_foods = None if min_price == '': min_price = 0 if max_price == '': max_price = 9001 if sort == '': sort = 'ASC' # Retrieve and displays data try: cur = conn.cursor() params = (query, IGDBConnector.format_proc_param(include_foods), IGDBConnector.format_proc_param(exclude_foods), min_price, max_price, sort) cur.callproc('get_recipes', params) for result in cur.stored_results(): recipes = [IGDBConnector.get_recipes_row_to_dict(row) for row in result] return render_template('recipes.html', data=recipes) except errors.Error as e: return render_template('recipes.html', error=str(e))
from flask import Flask, request, render_template, redirect, url_for from db.igdb_connector import IGDBConnector from mysql.connector import errors import flask_login from flask.ext.login import UserMixin app = Flask(__name__) login_manager = flask_login.LoginManager() login_manager.init_app(app) # Creates a global connection to the database conn = IGDBConnector.connect() class User(UserMixin): """ A simple User class with the username being the id. """ def __init__(self, name): self.name = name self.id = name @login_manager.user_loader def load_user(user_id): cur = conn.cursor() cur.execute("SELECT * FROM users WHERE username = %s", (user_id,)) r = cur.fetchone() return User(r[0]) if r is not None else None