def update_post(post_id): post = postss.query.get(post_id) formsearch = RecipeSearchForm() form = PostFormHungryFor() if form.validate_on_submit(): hungryFood = "I am hungry for " + form.content.data db.engine.execute("UPDATE postss SET content = %s WHERE ID = %s", (hungryFood, post_id)) db.session.commit() return redirect(url_for('profile')) form3 = PostForm() if form3.validate_on_submit(): db.engine.execute("UPDATE postss SET content = %s WHERE ID = %s", form3.contentNormal.data, post_id) db.session.commit() return redirect(url_for('profile')) form2 = PostFormCurrentlyEating() if form2.validate_on_submit(): db.engine.execute("UPDATE postss SET content_current = %s, link_current = %s WHERE ID = %s", form2.contentCurrent.data, form2.linkCurrent.data, post_id) db.session.commit() return redirect(url_for('profile')) if post.post_type == "hungryFor": return render_template('editPost.html', form=form, form5=formsearch, post=post) elif post.post_type == "currentlyEating": return render_template('editPost2.html', form=form2, form5=formsearch, post=post) else: return render_template('editPost3.html', form=form3, form5=formsearch, post=post)
def settings(): formsearch = RecipeSearchForm() form = UpdateProfileForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) print(picture_file) db.engine.execute("UPDATE users SET profilePic = %s WHERE id = %s", (picture_file, current_user.id)) if(is_filled(form.username.data)): db.engine.execute("UPDATE users SET username = %s WHERE id = %s", (form.username.data, current_user.id)) if(is_filled(form.firstname.data)): db.engine.execute("UPDATE users SET firstname = %s WHERE id = %s", (form.firstname.data, current_user.id)) if(is_filled(form.lastname.data)): db.engine.execute("UPDATE users SET lastname = %s WHERE id = %s", (form.lastname.data, current_user.id)) if(is_filled(form.email.data)): db.engine.execute("UPDATE users SET firstname = %s WHERE id = %s", (form.firstname.data, current_user.id)) if(is_filled(form.cooking_exp.data)): db.engine.execute("UPDATE users SET cookingExperience = %s WHERE id = %s", (form.cooking_exp.data, current_user.id)) return redirect(url_for('profile')) elif request.method == 'GET': form.username.data = current_user.username form.firstname.data = current_user.firstName form.lastname.data = current_user.lastName form.email.data = current_user.email form.cooking_exp.data = current_user.cookingExperience return render_template('usersettings.html', form=form, form5=formsearch) return render_template('usersettings.html', form5=formsearch)
def profile(): followers = db.engine.execute("SELECT * FROM followers where followedid = %s", current_user.id) following = db.engine.execute("SELECT * FROM followers where followerid = %s", current_user.id) formsearch = RecipeSearchForm() form = PostFormHungryFor() if form.validate_on_submit(): toSend = "I am hungry for " + form.content.data post = postss(content=toSend, user_id=current_user.id, post_type="hungryFor") db.session.add(post) db.session.commit() flash('Your post has created', 'success') return redirect(url_for('profile')) formNormalText = PostForm() if formNormalText.validate_on_submit(): post2 = postss(content=formNormalText.contentNormal.data, user_id=current_user.id, post_type="boringPost") db.session.add(post2) db.session.commit() flash('Your post has created', 'success') return redirect(url_for('profile')) formCurrent = PostFormCurrentlyEating() if formCurrent.validate_on_submit(): post3 = postss(content_current=formCurrent.contentCurrent.data, link_current=formCurrent.linkCurrent.data, user_id=current_user.id, post_type="currentlyEating") db.session.add(post3) db.session.commit() flash('Your post has created', 'success') return redirect(url_for('profile')) allposts = postss.query.filter_by(user_id=current_user.id) recipes = rec.query.filter_by(user_id=current_user.id) favRecipes = favs.query.filter_by(user_id=current_user.id) image_file = url_for('static', filename='Images/' + current_user.profilePic) count2 = 0 for x in recipes: count2 = count2 + 1 count = 0 for x in favRecipes: count = count + 1 if count == 0 and count2 != 0: return render_template('ProfilePage.html', title='Profile', form5=formsearch, followers = followers, following = following, recipes=recipes, image_file=image_file, allPosts=allposts, form=form, form2=formNormalText, form3=formCurrent) elif count == 0 and count2 == 0: return render_template('ProfilePage.html', title='Profile', form5=formsearch, followers = followers, following = following, image_file=image_file, allPosts=allposts, form=form, form2=formNormalText, form3=formCurrent) elif count != 0 and count2 == 0: return render_template('ProfilePage.html', title='Profile', form5=formsearch, followers = followers, following = following, image_file=image_file, allPosts=allposts, form=form, form2=formNormalText, form3=formCurrent, favRecipes=favRecipes) else: return render_template('ProfilePage.html', title='Profile', form5=formsearch, followers = followers, following = following, recipes=recipes, image_file=image_file, allPosts=allposts, form=form, form2=formNormalText, form3=formCurrent, favRecipes=favRecipes)
def discovery(): # recipes = rec.query.all(); # posts = postss.query.all(); obj = rec.query.count() recipes = rec.query.order_by(func.rand()).first() recuser = users.query.get(recipes.user_id) print(recipes.dateposted) formsearch = RecipeSearchForm() return render_template('discovery.html', rec = recipes, recuser = recuser, form5=formsearch)
def remove_follower(followedid, followerid): db.engine.execute("DELETE FROM followers WHERE followedid = %s AND followerid = %s", followedid, followerid) formsearch = RecipeSearchForm() form = PostFormHungryFor() formNormalText = PostForm() formCurrent = PostFormCurrentlyEating() return redirect(url_for('homepage'))
def remove_like(postid): db.engine.execute("DELETE FROM likers WHERE liked_post = %s AND userid = %s", postid, current_user.id) db.engine.execute("UPDATE postss SET nlikes = nlikes - 1 where id = %s", postid) formsearch = RecipeSearchForm() form = PostFormHungryFor() formNormalText = PostForm() formCurrent = PostFormCurrentlyEating() return redirect(url_for('homepage'))
def showprofile(hisid): # users = db.engine.execute("SELECT * FROM users WHERE id = %s", hisid) userss = users.query.filter_by(id = hisid).first() # followers = db.engine.execute("SELECT * FROM followers where followerid = %s", current_user.id) image_file = url_for('static', filename='Images/' + userss.profilePic) formsearch = RecipeSearchForm() form = PostFormHungryFor() formNormalText = PostForm() formCurrent = PostFormCurrentlyEating() recipes = rec.query.filter_by(user_id=hisid) favRecipes = favs.query.filter_by(user_id=hisid) followers = db.engine.execute("SELECT * FROM followers where followedid = %s", hisid) following = db.engine.execute("SELECT * FROM followers where followerid = %s", hisid) allposts = db.engine.execute("SELECT * from (select * from postss where user_id = %s) as a left join likers on a.id = likers.liked_post and likers.userid = %s ;", hisid, current_user.id ) flag = "hello" for x in followers: if x.followedid == hisid: flag = "hello2" count2 = 0 for x in recipes: count2 = count2 + 1 count = 0 for x in favRecipes: count = count + 1 if count == 0 and count2 != 0: return render_template('ProfilePageOthers.html',flag = flag, title='Profile', form5=formsearch, followers = followers, following = following, users = userss, image_file=image_file, recipes=recipes, allPosts=allposts, form=form, form2=formNormalText, form3=formCurrent) elif count == 0 and count2 == 0: return render_template('ProfilePageOthers.html',flag = flag, title='Profile', form5=formsearch, followers = followers, following = following, users = userss, image_file=image_file, allPosts=allposts, form=form, form2=formNormalText, form3=formCurrent) elif count != 0 and count2 == 0: return render_template('ProfilePageOthers.html',flag = flag, title='Profile', form5=formsearch, followers = followers, following = following, users = userss, image_file=image_file, allPosts=allposts, form=form, form2=formNormalText, form3=formCurrent, favRecipes=favRecipes) else: return render_template('ProfilePageOthers.html',flag = flag, title='Profile', form5=formsearch, followers = followers, following = following, users = userss, image_file=image_file,recipes=recipes, allPosts=allposts, form=form, form2=formNormalText, form3=formCurrent, favRecipes=favRecipes)
def homepage(): formsearch = RecipeSearchForm() # favRecipes = favs.query.filter_by(user_id=current_user.id) form = PostFormHungryFor() if current_user.is_authenticated: # allrecipes = db.engine.execute("SELECT rec_name, rec_description, user_id, recipePic, dateposted, username, followername, rating from (rec left join (select id, username from users) as a on rec.user_id = a.id) left join followers on (followers.followedid = rec.user_id and followerid = %s)", current_user.id) # allposts = db.engine.execute("SELECT content_current, content, user_id, link_current, post_date, username, followername, followerid from \ # postss left join (select id, username from users) as a on postss.user_id = a.id \ # left join followers on (followers.followedid = postss.user_id and followerid = %s)", current_user.id ) allrecipes = db.engine.execute("SELECT rec.id as id, rec_name, rec_description, user_id, recipePic, dateposted, username, followername, rating, number_of_ratings from (rec left join (select id, username from users) as a on rec.user_id = a.id) \ left join followers on (followers.followedid = rec.user_id and followerid = %s) \ UNION \ select b.id, content_current, content, user_id, link_current, post_date, username, followername, userid, nlikes from \ (select * from postss left join likers on postss.id = likers.liked_post and likers.userid = %s) as b left join (select id, username from users) as a on b.user_id = a.id \ left join followers on (followers.followedid = b.user_id and followerid = %s) \ ORDER BY dateposted desc;", current_user.id, current_user.id, current_user.id) else: allrecipes = db.engine.execute("SELECT rec.id as id, rec_name, rec_description, user_id, recipePic, dateposted, username, rating, number_of_ratings from (rec left join (select id, username from users) as a on rec.user_id = a.id) \ UNION \ select postss.id, content_current, content, user_id, link_current, post_date, username, a.id, nlikes from (postss left join (select id , username from users) as a on postss.user_id = a.id) \ ORDER BY dateposted desc;") if form.validate_on_submit(): toSend = "I am hungry for " + form.content.data post = postss(content=toSend, user_id=current_user.id, post_type="hungryFor") db.session.add(post) db.session.commit() flash('Your post has created', 'success') return redirect(url_for('homepage')) formNormalText = PostForm() if formNormalText.validate_on_submit(): post2 = postss(content=formNormalText.contentNormal.data, user_id=current_user.id, post_type="boringPost") db.session.add(post2) db.session.commit() flash('Your post has created', 'success') return redirect(url_for('homepage')) formCurrent = PostFormCurrentlyEating() if formCurrent.validate_on_submit(): post3 = postss(content_current=formCurrent.contentCurrent.data, link_current=formCurrent.linkCurrent.data, user_id=current_user.id, post_type="currentlyEating") db.session.add(post3) db.session.commit() flash('Your post has created', 'success') return redirect(url_for('homepage')) return render_template('homepage.html', title='Home', form5=formsearch, form=form, form2=formNormalText, form3=formCurrent, allrecipes=allrecipes)
def add_follower(followedid, followedname): follower = followers(followerid = current_user.id, followedid = followedid, followername = current_user.username, followedname = followedname) db.session.add(follower) db.session.commit() formsearch = RecipeSearchForm() form = PostFormHungryFor() formNormalText = PostForm() formCurrent = PostFormCurrentlyEating() return redirect(url_for('homepage'))
def findfriends(): formsearch = RecipeSearchForm() findfriends = FindFriends() allrecipes = db.engine.execute("SELECT rec.id as id, rec_name, rec_description, user_id, recipePic, dateposted, username, followername, rating, number_of_ratings from (rec left join (select id, username from users) as a on rec.user_id = a.id) \ left join followers on (followers.followedid = rec.user_id and followerid = %s) \ UNION \ select b.id, content_current, content, user_id, link_current, post_date, username, followername, userid, nlikes from \ (select * from postss left join likers on postss.id = likers.liked_post and likers.userid = %s) as b left join (select id, username from users) as a on b.user_id = a.id \ left join followers on (followers.followedid = b.user_id and followerid = %s) \ ORDER BY dateposted desc;", current_user.id, current_user.id, current_user.id) if findfriends.validate_on_submit: friends = db.engine.execute("SELECT * from (users left join followers on followers.followedid = users.id and followerid = %s) where username = %s", current_user.id, findfriends.friend.data) return render_template('findfriends.html', form5=formsearch, findfriends = findfriends, friends = friends, allrecipes = allrecipes)
def searchthings(thing): formsearch = RecipeSearchForm() form = PostFormHungryFor() formNormalText = PostForm() formCurrent = PostFormCurrentlyEating() if thing is "": return render_template('homepage.html', form5=formsearch, form=form, form2=formNormalText, form3=formCurrent) else: recipes = db.engine.execute("SELECT * FROM (SELECT * FROM rec WHERE ((MATCH (rec_name, ings, tags) \ AGAINST (%s IN BOOLEAN MODE)))) as b left join (select id as useridd, username from users) as a on b.user_id = a.useridd;", thing) return render_template('homepage.html', form5=formsearch, form=form, form2=formNormalText, form3=formCurrent, recipes=recipes)
def showrecipe(recipe_id): formsearch = RecipeSearchForm() recc = db.engine.execute("SELECT * from (select * from rec where id = %s) as a left join raters on raters.userid = %s and raters.rated_recipe = %s LIMIT 1", recipe_id, current_user.id, recipe_id).first() if recc.number_of_ratings is 0: totalRating = 0 else: totalRating = float(recc.rating)/float(recc.number_of_ratings) # checkRating = raters.query.filter_by(rated_recipe = recipe_id, userid = current_user.id) return render_template('recipespage.html', title=recc.rec_name, rec=recc, form5=formsearch,totalRating=round(totalRating, 1))
def add_like(postid): print("Hello : ", postid) like = likers(liked_post = postid, userid = current_user.id) # luike = postss.query.filter_by(id=postid).first() db.engine.execute("UPDATE postss SET nlikes = nlikes + 1 where id = %s", postid) db.session.add(like) db.session.commit() formsearch = RecipeSearchForm() form = PostFormHungryFor() formNormalText = PostForm() formCurrent = PostFormCurrentlyEating() return redirect(url_for('homepage'))
def comment_recipe(recipe_id): commentForm = CommentForm() comments = recipe_comments.query.filter_by(recipe_id=recipe_id) post = rec.query.filter_by(id=recipe_id).first() formsearch = RecipeSearchForm() if commentForm.validate_on_submit(): comm = recipe_comments(recipe_id = recipe_id, commentContent=commentForm.commentBox.data, userid = current_user.id, username = current_user.username) db.session.add(comm) db.session.commit() return redirect(url_for('comment_recipe', recipe_id = recipe_id)) return render_template('recipeComment.html', commentForm=commentForm, form5=formsearch, post=post,post_id = recipe_id, comments=comments)
def search(): formsearch = RecipeSearchForm() form = PostFormHungryFor() formNormalText = PostForm() formCurrent = PostFormCurrentlyEating() if request.method == 'POST': minmax = request.form['minmax'] minmax = minmax.replace('-', '') minmax = minmax.replace('$', '').split() calories = request.form['calories'] calories = calories.replace('-', '') calories = calories.replace('$', '').split() if formsearch.validate_on_submit(): if is_filled(formsearch.keyWord.data): keywords = parser_first_round(formsearch.keyWord.data) keywords_sufix = parser_search_sufix(formsearch.keyWord.data) print(keywords_sufix) print(keywords) recipes = db.engine.execute("SELECT * FROM (SELECT * FROM rec WHERE (minPrice <= %s AND maxprice >= %s) AND ( calories >= %s AND calories <= %s ) AND \ ((MATCH (rec_name, rec_description, rec_instruction, ings, tags) \ AGAINST (%s IN BOOLEAN MODE)))) as b left join (select id as useridd, username from users) as a on b.user_id = a.useridd \ UNION \ SELECT * FROM (SELECT * FROM rec where rec_name LIKE %s) as b left join (select id as useridd, username from users) as a on b.user_id = a.useridd \ ", minmax[1], minmax[0], calories[0], calories[1], keywords, keywords_sufix ) return render_template('homepage.html', form5=formsearch, form=form, form2=formNormalText, form3=formCurrent, recipes=recipes) else: recipes = db.engine.execute("SELECT * FROM rec WHERE (minPrice <= %s AND maxprice >= %s) AND ( calories >= %s AND calories <= %s )", minmax[1], minmax[0], calories[0], calories[1]) return render_template('homepage.html', form5=formsearch, form=form, form2=formNormalText, form3=formCurrent, recipes=recipes) return render_template('homepage.html', form5=formsearch, form=form, form2=formNormalText, form3=formCurrent)
def create_recipe(): formsearch = RecipeSearchForm() form = RecipeForm() if form.validate_on_submit(): if form.recipePic.data: recipe_file = save_picture(form.recipePic.data) recipe = rec(rec_name=form.rec_name.data, prep_time=form.prep_time.data, cook_time=form.cook_time.data, rec_description=form.rec_description.data, rec_instruction=form.rec_instruction.data, ings=form.ings.data, tags=form.tags.data, calories=form.calories.data, fat=form.fat.data, cholesterol=form.cholesterol.data, sodium=form.sodium.data, user_id=current_user.id, minPrice=form.minPrice.data, maxPrice=form.maxPrice.data, recipePic=recipe_file, rating = 0, number_of_ratings = 0) else: print("RECIPE NAME: " + form.rec_name.data) recipe = rec(rec_name=form.rec_name.data, prep_time=form.prep_time.data, cook_time=form.cook_time.data, rec_description=form.rec_description.data, rec_instruction=form.rec_instruction.data, ings=form.ings.data, tags=form.tags.data, calories=form.calories.data, fat=form.fat.data, cholesterol=form.cholesterol.data, sodium=form.sodium.data, user_id=current_user.id, minPrice=form.minPrice.data, maxPrice=form.maxPrice.data, rating = 0, number_of_ratings = 0) print("add") db.session.add(recipe) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('profile')) return render_template('createrecipe.html', title='New Recipe', form=form, form5=formsearch)
def update_recipe(recipe_id): recipee = rec.query.get(recipe_id) formsearch = RecipeSearchForm() form = RecipeForm() if form.validate_on_submit(): if form.recipePic.data: recipe_file = save_picture(form.recipePic.data) print(recipe_file) db.engine.execute("UPDATE rec SET recipePic = %s WHERE id = %s", (recipe_file, recipe_id)) reRec_name = form.rec_name.data rePrep_time = form.prep_time.data reCook_time = form.cook_time.data if len(form.rec_description.data)==0: reRec_description= recipee.rec_description else: reRec_description = form.rec_description.data if len(form.rec_instruction.data)==0: reRec_instruction= recipee.rec_instruction else: reRec_instruction = form.rec_instruction.data ings = form.ings.data tags = form.tags.data reCalories = form.calories.data reFat = form.fat.data reCholesterol = form.cholesterol.data reSodium = form.sodium.data reMinPrice = form.minPrice.data reMaxPrice = form.maxPrice.data print("add recipe") db.engine.execute("UPDATE rec SET rec_name = %s, prep_time = %s, cook_time = %s, rec_description = %s, rec_instruction = %s, ings = %s, tags = %s, minPrice = %s, maxPrice = %s,calories = %s,fat = %s, cholesterol = %s, sodium = %s WHERE ID = %s", (reRec_name, rePrep_time, reCook_time, reRec_description,reRec_instruction, ings, tags,reMinPrice,reMaxPrice, reCalories, reFat, reCholesterol, reSodium, recipe_id)) db.session.commit() return redirect(url_for('profile')) return render_template('editRecipe.html',form = form, form5 = formsearch, rec=recipee)
def rate_recipe(rec_id): if request.method=='POST': ratings = request.form['rate'] rat = 0 if ratings == "5": rat = 1 elif ratings == "4": rat = 2 elif ratings == "3": rat = 3 elif ratings == "2": rat = 4 elif ratings == "1": rat = 5 addRate = raters(rated_recipe = rec_id,userid=current_user.id) db.session.add(addRate) db.session.commit() rate = rec.query.filter_by(id=rec_id).first() rat = rat + rate.rating numRatings = rate.number_of_ratings + 1 db.engine.execute("UPDATE rec SET rating = %s, number_of_ratings = %s where id = %s",str(rat),str(numRatings),rec_id) formsearch = RecipeSearchForm("SELECT COUNT(id) FROM raters WHERE rated_recipe = = 1") recc = db.engine.execute("SELECT * from (select * from rec where id = %s) as a left join raters on raters.userid = %s and raters.rated_recipe = %s LIMIT 1", rec_id, current_user.id, rec_id).first() if recc.number_of_ratings is 0: totalRating = 0 else: totalRating = recc.rating/recc.number_of_ratings return render_template('recipespage.html', title=recc.rec_name, rec=recc, form5=formsearch,totalRating=round(totalRating, 1))
def searchadvanced(things): print("things") print(things) formsearch = RecipeSearchForm() form = PostFormHungryFor() formNormalText = PostForm() formCurrent = PostFormCurrentlyEating() if things is "": return render_template('homepage.html', form5=formsearch, form=form, form2=formNormalText, form3=formCurrent) else: words = things.split(';') words = list(filter(None, words)) print(words) words = " ".join(words) recipes = db.engine.execute("SELECT * FROM (SELECT * FROM rec WHERE ((MATCH (rec_name, ings, tags) \ AGAINST (%s IN BOOLEAN MODE)))) as b left join (select id as useridd, username from users) as a on b.user_id = a.useridd;", words) return render_template('homepage.html', form5=formsearch, form=form, form2=formNormalText, form3=formCurrent, recipes=recipes) return render_template('homepage.html', form5=formsearch, form=form, form2=formNormalText, form3=formCurrent)
def advancedsearch(): formsearch = RecipeSearchForm() print(formsearch.keyWord.data) if formsearch.validate_on_submit(): print(formsearch.keyWord.data) return render_template("advancedsearchpage.html", form5=formsearch)
def favorites(): formsearch = RecipeSearchForm() favsss = favs.query.filter_by(user_id=current_user.id) return render_template('favoritesPage.html',title='Favorites Page', favorites=favsss, form5=formsearch)
def checklist(): formsearch = RecipeSearchForm() return render_template('checklist.html', form5=formsearch)
def map(): formsearch = RecipeSearchForm() return render_template('map.html', form5=formsearch)
def all_comments(): formsearch = RecipeSearchForm() allComments = post_comments.query.filter_by(post_id=65) return render_template('testComment2.html', allComments = allComments, form5=formsearch)