def signup(): form = SignUpForm(csrf_enabled=False) choices = [(str(x),x) for x in reversed(range(1900,2004))] form.year_of_birth.choices = choices if request.method == 'POST': if form.validate_on_submit(): email = form.email.data password = form.password.data firstname = form.firstname.data lastname = form.lastname.data year_of_birth = form.year_of_birth.data try: cur = mysql.cursor() cur.execute('''insert into users values ('%s', '%s', '%s','%s', %d)''' % (email , password,firstname, lastname, int(year_of_birth))) mysql.commit() user = User(email, password, firstname, lastname, year_of_birth) login_user(user) return redirect(url_for('add_ingredient')) except Exception as e: flash(str(e)) return render_template('signup.html', form=form) else: flash('Error signing up') render_template('signup.html' , form=form) else: return render_template('signup.html', form=form)
def add_ingredient(): form = IngredientForm(csrf_enabled=False) cursor = mysql.cursor() cursor.execute('''select unit_name from units''') units = cursor.fetchall() choices = [(unit[0], unit[0].capitalize()) for unit in units] form.unit_name.choices = choices if request.method == 'POST': if form.validate_on_submit(): product_name = form.product_name.data calories_per_unit = form.calories_per_unit.data stock = form.stock.data cost = form.cost.data unit_name = form.unit_name.data email = current_user.email try: cursor.execute('''insert into ingredients (product_name, unit_name, email, calories_per_unit, stock, cost) values ("%s", "%s", "%s", %d, %d, %0.2f)''' % (product_name, unit_name, email, calories_per_unit, stock, cost)) mysql.commit() return 'Added' except Exception as e: flash(str(e)) return render_template('add_ingredient.html', form=form) else: flash('Error adding ingredient') return render_template('add_ingredient.html', form=form) else: return render_template('add_ingredient.html', form=form)
def insertingredients(name, ingredients): cursor = mysql.cursor() cursor.callproc("GetRecipeId", [str(name)]) result = cursor.fetchall() cursor.close() mysql.commit() recipes = [] for row in result: recipes.append(row) ''' for i in range (0,len(ingredients)-1): cursor= mysql.cursor() cursor.callproc("GetIngredientsId",[str(ingredients[i])]) result = cursor.fetchall() cursor.close() mysql.commit() ingredients = [] for row in result: ingredients.append(row) ''' cursor = mysql.cursor() for i in range(0, len(ingredients)): insert_stmt = ("INSERT INTO Contains(recipe_id,ingredients_id) " "VALUES (%s, %s)") data = (recipes[0], ingredients[i]) cursor.execute(insert_stmt, data) mysql.commit() cursor.close()
def add_recipie(): form = RecipeForm() if request.method == 'GET': cursor = mysql.cursor() cursor.execute('''select ingredients_id, product_name from ingredients where email="%s"''' % (current_user.email)) results = cursor.fetchall() choices = [(ingredient[0], ingredient[1].capitalize()) for ingredient in results] form.ingredients.choices = choices return render_template('add_recipe.html', form=form) else: cursor = mysql.cursor() recipe_name = form.recipe_name.data calories = form.calories.data image = form.image.data filename = image.filename image.save(os.path.join('app/static/images', filename)) url = 'app/static/images/%s' % (filename) steps = form.instructions.data instruction1 = steps[0]['instruction1'] instruction2 = steps[0]['instruction2'] instruction3 = steps[0]['instruction3'] instruction4 = steps[0]['instruction4'] cursor.execute('''insert into recipes (email, recipe_name, calories, image_url) values ("%s", "%s", %d, "%s")''' % (current_user.email, recipe_name, calories, url)) cursor.execute('''insert into instructions (recipe_id, order_of_action, action) values (LAST_INSERT_ID(), 1, "%s")''' % instruction1) cursor.execute('''insert into instructions (recipe_id, order_of_action, action) values (LAST_INSERT_ID(), 2, "%s")''' % instruction2) cursor.execute('''insert into instructions (recipe_id, order_of_action, action) values (LAST_INSERT_ID(), 3, "%s")''' % instruction3) cursor.execute('''insert into instructions (recipe_id, order_of_action, action) values (LAST_INSERT_ID(), 4, "%s")''' % instruction4) mysql.commit() return 'works'
def insert_kitchen(user_name): insert_stmt = ("INSERT INTO Kitchen(user_name) " "VALUES (%s)") data = (str(user_name)) cursor = mysql.cursor() cursor.execute(insert_stmt, data) mysql.commit() cursor.close()
def register(): error = None form = reg_Form() if request.method == 'POST': if form.validate_on_submit(): first_name = form.first_name.data last_name = form.last_name.data user_name = form.user_name.data email = form.email.data password = form.password.data conf_password = form.conf_password.data phone = form.phone.data diet = form.diet.data # health_info=form.health_info.data #diet="Normal" health_info = form.health_info.data try: insert_stmt = ("INSERT INTO User(user_name, hash_password) " "VALUES (%s, %s)") data = (user_name, password) cursor = mysql.cursor() cursor.execute(insert_stmt, data) mysql.commit() cursor.close() try: insert_profile(first_name, user_name, last_name, email, phone, diet, health_info) except Exception as e: print e db.session.rollback() flash(str(e)) return render_template('register.html', error=error, form=form) try: #insert_kitchen(user_name) pass except Exception as e: print e db.session.rollback() flash(str(e)) return render_template('register.html', error=error, form=form) user = User(user_name=user_name, hash_password=password) login_user(user) return redirect(url_for('index')) except Exception as e: print e db.session.rollback() flash(str(e)) return render_template('register.html', error=error, form=form) else: flash('Error signing up') render_template('register.html', error=error, form=form) else: return render_template('register.html', form=form)
def insertinstructions(name, instruction1, instruction2, instruction3, instruction4): # cursor = mysql.cursor() # cursor.callproc("GetInstructionsId",[str(instruction1)]) # result_1 = cursor.fetchall() # cursor.close() # cursor = mysql.cursor() # cursor.callproc("GetInstructionsId",[str(instruction2)]) # result_2 = cursor.fetchall() # cursor.close() # cursor = mysql.cursor() # cursor.callproc("GetInstructionsId",[str(instruction3)]) # result_3 = cursor.fetchall() # cursor.close() # cursor = mysql.cursor() # cursor.callproc("GetInstructionsId",[str(instruction4)]) # result_4 = cursor.fetchall() # cursor.close() cursor = mysql.cursor() cursor.callproc("GetRecipeId", [str(name)]) result = cursor.fetchall() cursor.close() mysql.commit() recipes = [] instructions = [] for row in result: recipes.append(row) # print (recipes[0]) cursor = mysql.cursor() insert_stmt = ( "INSERT INTO Instructions(recipe_id,task,instruction_order) " "VALUES (%s, %s, %s)") data = (recipes[0], instruction1, "1") cursor.execute(insert_stmt, data) insert_stmt = ( "INSERT INTO Instructions(recipe_id,task,instruction_order) " "VALUES (%s, %s, %s)") data = (recipes[0], instruction2, "2") cursor.execute(insert_stmt, data) insert_stmt = ( "INSERT INTO Instructions(recipe_id,task,instruction_order) " "VALUES (%s, %s, %s)") data = (recipes[0], instruction3, "3") cursor.execute(insert_stmt, data) insert_stmt = ( "INSERT INTO Instructions(recipe_id,task,instruction_order) " "VALUES (%s, %s, %s)") data = (recipes[0], instruction4, "4") cursor.execute(insert_stmt, data) mysql.commit() cursor.close()
def insert_profile(first_name, user_name, last_name, email, phone, diet, health_info): insert_stmt = ( "INSERT INTO Profile(first_name, user_name, last_name, email, phone, diet, health_info) " "VALUES (%s, %s, %s, %s, %s, %s, %s)") data = (first_name, user_name, last_name, email, phone, diet, health_info) cursor = mysql.cursor() cursor.execute(insert_stmt, data) mysql.commit() cursor.close()
def add_recipe(): error = None form = recipe_Form() if request.method == 'POST': if form.validate_on_submit(): name = form.name.data calorie = form.calorie.data servings = form.servings.data prep_time = form.prep_time.data cook_time = form.cook_time.data #diet_type="Normal" diet_type = form.diet_type.data instruction1 = form.instruction1.data instruction2 = form.instruction2.data instruction3 = form.instruction3.data instruction4 = form.instruction3.data ingredients = form.ingredients.data #flash(ingredients) image = request.files['photo'] if allowed_file(image.filename): filename = secure_filename(image.filename) image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) else: flash('Incorrect File Format', 'danger') return redirect(url_for('home')) insert_stmt = ( "INSERT INTO Recipe(name,calorie,servings,cook_time,prep_time,diet_type) " "VALUES (%s, %s, %s, %s, %s, %s)") data = (name, calorie, servings, cook_time, prep_time, diet_type) cursor = mysql.cursor() cursor.execute(insert_stmt, data) insert_date(current_user.user_name, name) mysql.commit() cursor.close() insertinstructions(name, instruction1, instruction2, instruction3, instruction4) insertingredients(name, ingredients) flash(ingredients) flash(len(ingredients)) flash('success') return redirect(url_for('recipes')) else: flash(form.errors) flash('Error entering') render_template('add_recipe.html', error=error, form=form) return render_template('add_recipe.html', form=form)
def insert(self, data): logger.debug("Inserting data to mysql") try: query = build_insert_query(data) cursor = mysql.cursor() cursor.execute(query) # if res == 0: # return False mysql.commit() return True except Exception as e: logger.exception("Exception in inserting data to the mysql server") return False
def insert_date(user, name): cursor = mysql.cursor() cursor.callproc("GetRecipeId", [str(name)]) result = cursor.fetchall() cursor.close() mysql.commit() recipes = [] for row in result: recipes.append(row) insert_stmt = ("INSERT INTO Uploads(user_name,recipe_id,upload_date) " "VALUES (%s, %s, NOW())") data = (user, recipes[0]) cursor = mysql.cursor() cursor.execute(insert_stmt, data)
def edit_task(self, id, Title, Status, Begin, End): cursor = mysql.cursor() if Title: cursor.execute("UPDATE task SET title='" + Title + "' WHERE task_id = " + id + ";") if Status: cursor.execute("UPDATE task SET status='" + Status + "' WHERE task_id = " + id + ";") if Begin: cursor.execute("UPDATE task SET begin='" + Begin + "' WHERE task_id = " + id + ";") if End: cursor.execute("UPDATE task SET end='" + End + "' WHERE task_id = " + id + ";") mysql.commit() cursor.close()
def recipes(): cursor = mysql.cursor() form = recipe_Form(request.form) cursor.callproc("GetRecipes", [str(form.name.data)]) result = cursor.fetchall() cursor.close() mysql.commit() recipes = [] for row in result: recipes.append(row) # print recipes if request.method == 'POST': if result is not None: return render_template("recipes.html", form=form, recipes=recipes) else: return render_template("recipes.html", form=form)
def create_task(self, Title, Status, Begin, End): cursor = mysql.cursor() if Title: cursor.execute("INSERT INTO task SET title = '" + Title + "';") else: return False if Begin: cursor.execute("UPDATE task SET begin = '" + Begin + "' WHERE title=Title ;") if End: cursor.execute("UPDATE task SET end = '" + End + "' WHERE title=Title ;") if Status: cursor.execute("UPDATE task SET status = '" + Status + "' WHERE title=Title ;") mysql.commit() cursor.close() return True
def delete_task(self, id): cursor = mysql.cursor() cursor.execute("DELETE FROM task WHERE task_id = " + str(id) + ";") mysql.commit() cursor.close()
def remove_user_from_task(self, task_id, user_id): cursor = mysql.cursor() cursor.execute("delete from user_has_task where fk_task_id = " + task_id + " and fk_user_id = " + user_id + ";") mysql.commit() cursor.close()
def create_user(self, username, password): cursor = mysql.cursor() cursor.execute("INSERT INTO user SET username = '******', password = '******';") mysql.commit() cursor.close()
def add_user_to_task(self, task_id, user_id): cursor = mysql.cursor() cursor.execute("insert into user_has_task values (" + user_id + ", " + task_id + ");") mysql.commit() cursor.close()