def get_authed_reddit_for_cubersio_acct(): """ Returns a PRAW instance for the Reddit account to post the competition under. """ if __IS_DEVO: token = get_user_by_username(__DEVO_CUBERSIO_ACCT).reddit_token else: token = get_user_by_username(__PROD_CUBERSIO_ACCT).reddit_token return Reddit(client_id=__CLIENT_ID, client_secret=__CLIENT_SECRET, refresh_token=token, user_agent=__USER_AGENT)
def get_user_profile_info(username): if not current_user: return "", 401 user = get_user_by_username(username) user_dict = { 'id': user.id, 'name': user.username, 'wcaId': user.wca_id, 'admin': user.is_admin if current_user.is_admin or current_user.is_results_mod else "none", 'alwaysBlacklist': user.always_blacklist if current_user.is_admin or current_user.is_results_mod else "none", 'resultsMod': user.is_results_mod if current_user.is_admin or current_user.is_results_mod else "none", 'verified': user.is_verified if current_user.is_admin or current_user.is_results_mod else "none", } return jsonify(user_dict)
def edit_settings(): """ A route for showing a editing a user's personal settings. """ if not current_user.is_authenticated: return redirect(url_for('index')) user = get_user_by_username(current_user.username) return __handle_post( user, request.form) if request.method == 'POST' else __handle_get(user)
def user_site_stats(username): """ A route for retrieving data related to a user's usage of the site. Returns a JSON-serialized dictionary of the form: { event_id: [single, single_site_ranking, average, average_site_ranking] } """ user = get_user_by_username(username) if not user: return NO_SUCH_USER_ERR_MSG.format(username), HTTPStatus.NOT_FOUND return json.dumps(__get_user_site_rankings(user.id))
def user_versus_user(username1, username2, errors=None): """ A route for displaying user results head-to-head with the current user. """ errors = list() usernames = None user1 = get_user_by_username(username1) if not user1: usernames = get_all_active_usernames() user1 = get_user_by_username(choice(usernames)) errors.append(NO_SUCH_USER_ERR_MSG.format(username1, user1.username)) user2 = get_user_by_username(username2) if not user2: if not usernames: usernames = get_all_active_usernames() user2 = get_user_by_username(choice(usernames)) errors.append(NO_SUCH_USER_ERR_MSG.format(username2, user2.username)) return __render_versus_page_for_users(user1, user2, errors)
def __get_result(res): user = get_user_by_username(res[0]) result = { 'user': { 'id': user.id, 'name': user.username, 'verified': user.is_verified }, 'points': res[1] } return result
def reprocess_results_for_user_and_comp_event(username, comp_event_id): """ Reprocesses the event results for the specified user and competition event. """ user = get_user_by_username(username) if not user: print("Oops, that user doesn't exist.") comp_event = get_comp_event_by_id(comp_event_id) if not comp_event: print("Oops, that comp event doesn't exist.") results = get_event_results_for_user(comp_event_id, user) results = process_event_results(results, comp_event, user) save_event_results(results)
def get_user_rankings(username): if not current_user: return "", 401 user = get_user_by_username(username) # Accumulate a count of medals this user has for podiuming gold_count, silver_count, bronze_count = get_user_medals_count(user.id) # Get some other interesting stats solve_count = get_user_completed_solves_count(user.id) comps_count = get_user_participated_competitions_count(user.id) # Get a dictionary of event ID to names, to facilitate rendering some stuff in the template event_id_name_map = get_events_id_name_mapping() site_rankings_record = get_site_rankings_for_user(user.id) # Get sum of ranks sor_all = site_rankings_record.get_combined_sum_of_ranks() sor_wca = site_rankings_record.get_WCA_sum_of_ranks() sor_non_wca = site_rankings_record.get_non_WCA_sum_of_ranks() # Get kinch ranks kinch_all = site_rankings_record.get_combined_kinchrank() kinch_wca = site_rankings_record.get_WCA_kinchrank() kinch_non_wca = site_rankings_record.get_non_WCA_kinchrank() rankings = { 'medals': { 'gold': gold_count, 'silver': silver_count, 'bronze': bronze_count }, 'sumOfRanks': { 'all': clean_ranks(sor_all), 'wca': clean_ranks(sor_wca), 'non_wca': clean_ranks(sor_non_wca) }, 'kinchRanks': { 'all': kinch_all[2], 'wca': kinch_wca[2], 'non_wca': kinch_non_wca[2] }, 'solves': solve_count, 'competitions': comps_count } return jsonify(rankings)
def get_user_history(username): if not current_user: return "", 401 include_blacklisted = __should_show_blacklisted_results( username, current_user.is_admin) user = get_user_by_username(username) history = get_user_competition_history( user, include_blacklisted=include_blacklisted) history_dictionary = map(history_to_dictionary, history.items()) return jsonify(list(history_dictionary))
def do_verify_user(username): """ Sets the verified flag for the specified user. """ if not (current_user.is_authenticated and (current_user.is_admin or current_user.is_results_mod)): return ("Hey, you're not allowed to do that.", 403) user = get_user_by_username(username) if not user: return "", 404 if user.is_verified: unverify_user(user.id) else: verify_user(user.id) return "", 204
def blacklist_user(username): """ Sets the perma-blacklist flag for the specified user. """ if not (current_user.is_authenticated and (current_user.is_admin or current_user.is_results_mod)): return ("Hey, you're not allowed to do that.", 403) user = get_user_by_username(username) if not user: return "", 403 if user.always_blacklist: unset_perma_blacklist_for_user(user.id) else: set_perma_blacklist_for_user(user.id) return "", 204
def user_site_rankings(username): """ A route for retrieving data related to a user's PBs for all events. Returns a JSON-serialized dictionary of the form: { event_id: [single, single_site_ranking, average, average_site_ranking] } """ # TODO: format the values at indices 0 and 3 to be user-friendly representations of the results. # # Right now they are just the raw centiseconds, encoded MBLD, or "centi-moves" FMC values # # Further thinking... should I just format those for user-friendly display when I save them # to the database? I don't think we use the raw results times anywhere, and we're just wasting time # converting all of them each time we display them. user = get_user_by_username(username) if not user: return NO_SUCH_USER_ERR_MSG.format(username), HTTPStatus.NOT_FOUND return json.dumps(__get_user_site_rankings(user.id))
def get_user_records(username): if not current_user: return "", 401 user = get_user_by_username(username) site_rankings_record = get_site_rankings_for_user(user.id) # Get a dictionary of event ID to names, to facilitate rendering some stuff in the template event_id_name_map = get_events_id_name_mapping() site_rankings = site_rankings_record.get_site_rankings_and_pbs( event_id_name_map) rankings = [ x for x in map( lambda key: { 'puzzle': event_id_name_map[key], 'puzzleSlug': slugify(event_id_name_map[key]), 'single': "DNF" if site_rankings[key][0] == "DNF" else "" if site_rankings[key][0] == '' else int(site_rankings[key][0]), 'singleRank': site_rankings[key][1], 'average': "DNF" if site_rankings[key][2] == "DNF" else "" if site_rankings[key][2] == '' else int(site_rankings[key][2]), 'averageRank': site_rankings[key][3], 'kinchRank': site_rankings[key][4] } if site_rankings[key][0] else None, site_rankings.keys()) if x is not None ] return jsonify(list(rankings))
def profile(username): """ A route for showing a user's profile. """ user = get_user_by_username(username) if not user: no_user_msg = LOG_NO_SUCH_USER.format(username) app.logger.warning(no_user_msg) return (no_user_msg, 404) # Determine whether we're showing blacklisted results include_blacklisted = __should_show_blacklisted_results( username, current_user.is_admin) app.logger.info(LOG_PROFILE_VIEW.format(current_user.username, username), extra=__create_profile_view_log_context( current_user.is_admin, include_blacklisted)) # Get the user's competition history history = get_user_competition_history( user, include_blacklisted=include_blacklisted) # Accumulate a count of medals this user has for podiuming gold_count, silver_count, bronze_count = get_user_medals_count(user.id) # Get some other interesting stats solve_count = get_user_completed_solves_count(user.id) comps_count = get_user_participated_competitions_count(user.id) # Get a dictionary of event ID to names, to facilitate rendering some stuff in the template event_id_name_map = get_events_id_name_mapping() # See if the user has any recorded site rankings. If they do, extract the data as a dict so we # can build their site ranking table site_rankings_record = get_site_rankings_for_user(user.id) if site_rankings_record: site_rankings = site_rankings_record.get_site_rankings_and_pbs( event_id_name_map) # Get sum of ranks sor_all = site_rankings_record.get_combined_sum_of_ranks() sor_wca = site_rankings_record.get_WCA_sum_of_ranks() sor_non_wca = site_rankings_record.get_non_WCA_sum_of_ranks() # Get Kinchranks kinch_all = site_rankings_record.get_combined_kinchrank() kinch_wca = site_rankings_record.get_WCA_kinchrank() kinch_non_wca = site_rankings_record.get_non_WCA_kinchrank() # If it exists, get the timestamp formatted like "2019 Jan 11" if site_rankings_record.timestamp: rankings_ts = site_rankings_record.timestamp.strftime('%Y %b %d') # If there is no timestamp, just say that the rankings as accurate as of the last comp # This should only happen briefly after I add the timestamp to the rankings table, # but before the rankings are re-calculated else: rankings_ts = "last competition-ish" else: rankings_ts = None site_rankings = None sor_all = None sor_wca = None sor_non_wca = None kinch_all = None kinch_wca = None kinch_non_wca = None # Set a flag indicating if this page view is for a user viewing another user's page viewing_other_user = user.username != current_user.username return jsonify(list(rankings)) return render_template("user/profile.html", user=user, solve_count=solve_count, comp_count=comps_count, history=history, rankings=site_rankings, event_id_name_map=event_id_name_map, rankings_ts=rankings_ts, is_admin_viewing=current_user.is_admin, sor_all=sor_all, sor_wca=sor_wca, sor_non_wca=sor_non_wca, gold_count=gold_count, silver_count=silver_count, bronze_count=bronze_count, viewing_other_user=viewing_other_user, kinch_all=kinch_all, kinch_wca=kinch_wca, kinch_non_wca=kinch_non_wca)