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_site_stats(user_id): """ Retrieves a user's stats related to their usage of the site: """ medals_count = get_user_medals_count(user_id) site_rankings_record = get_site_rankings_for_user(user_id) if site_rankings_record: 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() 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() else: sor_all = 0 sor_wca = 0 sor_non_wca = 0 kinch_all = 0 kinch_wca = 0 kinch_non_wca = 0 return { 'solve_count': get_user_completed_solves_count(user_id), 'comps_count': get_user_participated_competitions_count(user_id), 'medals_count': { 'gold': medals_count[0], 'silver': medals_count[1], 'bronze': medals_count[2] }, 'kinchranks': { 'all': kinch_all, 'wca': kinch_wca, 'non_wca': kinch_non_wca }, 'sum_of_ranks': { 'all': sor_all, 'wca': sor_wca, 'non_wca': sor_non_wca } }
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)
def profile(username): """ A route for showing a user's profile. """ user = get_user_by_username_case_insensitive(username) if not user: no_user_msg = MSG_NO_SUCH_USER.format(username) return render_template('error.html', error_message=no_user_msg), HTTPStatus.NOT_FOUND # Pull username from the user itself, so we know it's cased correctly for comparisons and # queries later username = user.username # Determine whether we're showing blacklisted results include_blacklisted = __should_show_blacklisted_results( username, current_user.is_admin) # 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() # Timestamp of the last time site rankings were run, formatted like "2019 Jan 11" rankings_ts = site_rankings_record.timestamp.strftime(TIMESTAMP_FORMAT) 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_self = user.username == current_user.username # Check if user has set WCA ID to be public show_wca_id = get_boolean_setting_for_user(user.id, SettingCode.SHOW_WCA_ID) # Set flags to indicate if user is missing a Reddit/WCA profile association missing_wca_association = viewing_self and username == user.reddit_id and not user.wca_id missing_reddit_association = viewing_self and username == user.wca_id and not user.reddit_id 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_self=viewing_self, kinch_all=kinch_all, kinch_wca=kinch_wca, kinch_non_wca=kinch_non_wca, show_wca_id=show_wca_id, missing_wca_association=missing_wca_association, missing_reddit_association=missing_reddit_association)