def team_leave(id, username): """ Allows team member to leave team """ team_id = request.view_args.get('id') username_arg = request.view_args.get('username') team = Team.query.get_or_404(team_id) user = User.query.filter_by(username=username_arg).first() form = EmptyForm(value="Delete") if request.method == "POST": if form.validate_on_submit(): user_team_member_object = user.get_team_member_object(id) db.session.delete(user_team_member_object) db.session.commit() flash('Successfully left team') return redirect(url_for('main.index'), 307) flash('something went wrong') return redirect(url_for('main.team', id=team.id), 307) text = """Are you sure you want to do this? If you carry this out, you can't access the team pages nor team tasks again without being invited back and you'll lose your team role """ return render_template('_confirm.html', id=team.id, username=username, form=form, value="Leave team", endpoint='main.team_leave', title="Are you sure?", text=text)
def team_task_delete(id, task_id): """ Method for deleting team tasks """ team = Team.query.get_or_404(id) task = Task.query.filter_by(id=task_id).first() if task is None: abort(404) form = EmptyForm() if form.validate_on_submit(): team_task = TeamTask.query.filter_by(task_id=task.id).first() db.session.delete(task) db.session.delete(team_task) db.session.commit() flash('The task was removed!') return redirect(url_for('main.team_tasks_uusi', id=team.id)) text = """Are you sure you want to do this? If you carry this out, the task and its state will be deleted permanently. """ return render_template( '_confirm.html', id=team.id, task_id=task.id, form=form, value="Delete team task", endpoint='main.team_task_delete', title="Are you sure?", text=text )
def team_remove_member_self(): """ For removing a member from team """ team = Team.query.get_or_404(request.view_args.get('id')) user = current_user form = EmptyForm(value="Delete") if form.validate_on_submit(): user_team_member_object = user.get_team_member_object(id) db.session.delete(user_team_member_object) db.session.commit() flash('Successfully deleted') return redirect(url_for('.index')) text = """Are you sure you want to do this? If you carry this out, your team member can't finish the tasks assigned to him/her and they can no longer access the team pages. """ return render_template( '_confirm.html', id=team.id, username=current_user.username, form=form, value="leave team", endpoint='main.team_remove_member_self', title="Are you sure?", text=text )
def team_invite(id): """ Route for sending team invites """ team = Team.query.get_or_404(id) users = User.query.all() form = EmptyForm() # Filters out the users already in the team us = [] for i in users: if team in i.teams: continue else: us.append(i) if form.validate_on_submit(): username = request.args.get('username') user = User.query.filter_by(username=username).first() tm = team.invite_user(user.username) # If invite was successful, carry out # database operation. if tm: db.session.add(tm) db.session.commit() else: flash('Did you try to invite someone already in your team?') return redirect(url_for('main.team_invite', id=team.id, team=team, users=us, form=form, team_id=team.id)) flash(message=('Invited user ' + user.username)) return redirect(url_for('main.team_invite', id=team.id, team=team, users=us, form=form, team_id=team.id)) return render_template('team_members.html', id=team.id, team=team, users=us, form=form, team_id=team.id)
def delete_profile(username): user = User.query.filter_by(username=username).first() if user is None: return abort(404) form = EmptyForm(value="Delete") if form.validate_on_submit(): db.session.delete(user) db.session.commit() flash('Your profile was deleted. Hope to see you again sometime!') return redirect(url_for('main.index')) text = """Are you sure you want to do this? If you carry this out, you will be removed from all your teams, all your tasks will be removed and none of these will be recoverable.""" return render_template( '_confirm.html', username=user.username, form=form, value="Delete profile", endpoint='main.delete_profile', title="Are you sure?", text=text )
def unfollow(username): form = EmptyForm() if form.validate_on_submit(): user = User.query.filter_by(username=username).first() if user is None: flash('User {} not found.'.format(username)) return redirect(url_for('main.index')) if user == current_user: flash('You cannot unfollow yourself!') return redirect(url_for('main.user', username=username)) current_user.unfollow(user) db.session.commit() flash('You are not following {}.'.format(username)) return redirect(url_for('main.user', username=username)) else: return redirect(url_for('main.index'))
def get_team_admin(id): """ Adds admin rights to admin, if moderation is needed for some reason """ team = Team.query.get_or_404(id) form = EmptyForm() if request.method == "POST": tm_admin = team.add_admin_role() db.session.add(tm_admin) db.session.commit() flash('You can now admin this team') return redirect(url_for('main.team', id=team.id))
def team_delete(id): team = Team.query.get_or_404(id) form = EmptyForm(value="Delete") if form.validate_on_submit(): flash('Successfully deleted') db.session.delete(team) db.session.commit() return redirect(url_for('main.index')) text = """Are you sure you want to do this? If you carry this out, all your team members will be removed, all the tasks they've done or are planning to do will disappear without a trace.""" return render_template( '_confirm.html', id=team.id, form=form, value="Delete team", endpoint='main.team_delete', title="Are you sure?", text=text )
def user(username): user = User.query.filter_by(username=username).first_or_404() page = request.args.get('page', 1, type=int) posts = user.posts.order_by(Post.timestamp.desc()).paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.user', username=user.username, page=posts.next_num) \ if posts.has_next else None prev_url = url_for('main.user', username=user.username, page=posts.prev_num) \ if posts.has_prev else None form = EmptyForm() return render_template('main/user.html', user=user, posts=posts.items, next_url=next_url, prev_url=prev_url, form=form)
def search_results(search_term): session["search_query"] = search_term like_form = EmptyForm() search_form = SearchForm() # exact match? Suggest alternatives! if sq.exact_recipe_match(search_term): return redirect( url_for("main.compare_recipes", search_term=search_term)) # fuzzy search results = sq.search_recipes(search_term) if len(results) > 0: # Retrieve or predict user ratings # TODO: Currently user_ratings isn't used at all # - put in helper fun, used in compare_recipes and search_results if current_user.is_authenticated: urls = list(results["url"].values) # Include user ratings in results user_results = sq.query_user_ratings(current_user.userID, urls) results = results.merge(user_results[["user_rating", "recipesID"]], how="left", on="recipesID") results["user_rating"].fillna(3, inplace=True) # Include bookmarks in results df_bookmarks = sq.query_bookmarks(current_user.userID, urls) results = results.merge(df_bookmarks, how="left", on="recipesID") results["bookmarked"].fillna(False, inplace=True) # ratings and emissions need to be passed separately for JS ratings = list(results["perc_rating"].values) emissions = [v for v in results["perc_sustainability"].values] return render_template( "explore.html", search_form=search_form, like_form=like_form, results=results, ratings=ratings, emissions=emissions, )
def compare_recipes(search_term, Np=20): # No exact match found if sq.exact_recipe_match(search_term) is False: return redirect(url_for("main.search_results", search_term=search_term)) # Forms search_form = SearchForm() like_form = EmptyForm() # Sorting results sort_by = request.args.get("sort_by") if not sort_by: sort_by = "Similarity" # Pagination page = request.args.get("page") page = int(page) if page else 0 # Get top 199 most similar recipes results = sq.content_based_search(search_term) # Retrieve bookmarks and likes if current_user.is_authenticated: urls = list(results["url"].values) # Include user ratings in results user_results = sq.query_user_ratings(current_user.userID, urls) results = results.merge(user_results[["user_rating", "recipesID"]], how="left", on="recipesID") results["user_rating"].fillna(3, inplace=True) # Include bookmarks in results df_bookmarks = sq.query_bookmarks(current_user.userID, urls) results = results.merge(df_bookmarks, how="left", on="recipesID") results["bookmarked"].fillna(False, inplace=True) # Disentangle reference recipe and similar recipes ref_recipe = results.iloc[0] results = results.iloc[1::, :] # Select only the top Np recipes for one page results = results[(0 + page * Np):((page + 1) * Np)] # Sort by similarity, sustainability or rating results = hf.sort_search_results(results, sort_by) # Pass ratings & emissions jointly for ref recipe and results ratings = list(results["perc_rating"].values) ratings = [ref_recipe["perc_rating"]] + ratings emissions = [v for v in results["perc_sustainability"].values] emissions = [ref_recipe["perc_sustainability"]] + emissions similarity = [round(v * 100) for v in results["similarity"].values] # make figures bp = ap.bar_compare_emissions(ref_recipe, results, sort_by=sort_by) return render_template( "compare_recipes.html", reference_recipe=ref_recipe, results=results, ratings=ratings, emissions=emissions, similarity=similarity, like_form=like_form, search_form=search_form, search_term=search_term, page=page, bp=bp, )
def cookbook(Np=20): # navbar-search search_form = SearchForm() # Like/Unlike form and bookmark form like_form = EmptyForm() bookmark_form = EmptyForm() # Pagination page = request.args.get("page") page = int(page) if page else 0 # TODO cookbook search # Get bookmarked recipes cookbook = sq.query_cookbook(current_user.userID) # Descriptive statistics to show to user Nrecipes = cookbook.shape[0] Nliked = sum(cookbook["user_rating"] == 5) Ndisliked = sum(cookbook["user_rating"] == 1) fav_recipes = hf.get_favorite_recipes(cookbook, 3) fav_categ, fav_categ_cnt = hf.get_favorite_categories(cookbook, 7) mean_cookbook_emissions = round( sum(cookbook["emissions"]) / (cookbook.shape[0] + 0.00001), 2) recommendations = [""] # TODO write helper func # Prepare figure data df_hist = sq.query_all_recipe_emissions() df_hist["reference"] = df_hist.url.isin(cookbook["url"]) df_hist.rename( columns={ "emissions_log10": "log10(Emissions)", "emissions": "Emissions", "title": "Title", }, inplace=True, ) # Sort by similarity, sustainability or rating sort_by = request.args.get("sort_by") if not sort_by: sort_by = "Sustainability" cookbook = hf.sort_search_results(cookbook, sort_by) Npages = cookbook.shape[0] // Np + 1 # Select only the top Np recipes for one page cookbook = cookbook[(0 + page * Np):((page + 1) * Np)] # TODO create separate route for personalized recommendations, see # https://github.com/sbuergers/sustainable-recipe-recommender-website/issues/3#issuecomment-717503064 # user_ratings = hf.predict_user_ratings(cookbook) # Variables to sort by avg_ratings = list(cookbook["perc_rating"].values) emissions = [v for v in cookbook["perc_sustainability"].values] # Make figures hist_title = "Emissions distribution of cookbook recipes" hist_emissions = ap.histogram_emissions(df_hist, hist_title) return render_template( "cookbook.html", search_form=search_form, cookbook=cookbook, Nrecipes=Nrecipes, Nliked=Nliked, Ndisliked=Ndisliked, fav_recipes=fav_recipes, fav_categ=fav_categ, fav_categ_cnt=fav_categ_cnt, mean_cookbook_emissions=mean_cookbook_emissions, recommendations=recommendations, avg_ratings=avg_ratings, emissions=emissions, like_form=like_form, bookmark_form=bookmark_form, page=page, Npages=Npages, hist_emissions=hist_emissions, )