Example #1
0
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)
Example #2
0
def __get_user_site_rankings(user_id):
    """ Retrieves a user site rankings record by user id. """

    event_id_name_map = get_events_id_name_mapping()
    site_rankings = dict()

    # 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)

    # Iterate over all events, making sure there's an entry in the user site rankings for everything,
    # even events they haven't participated in, in case the other user has done that event.
    for event_id in event_id_name_map.keys():
        if event_id not in site_rankings.keys():
            site_rankings[event_id] = EVENT_NOT_PARTICIPATED

    return site_rankings
Example #3
0
def __render_versus_page_for_users(user1, user2, errors):
    """ Renders and returns a user versus page for the specified two users. """

    # Get a map of event ID to event name, to facilitate rendering the template.
    # Sort it by the global sort order so the event records table has the same ordering
    # as everywhere else.
    event_id_name_map = get_events_id_name_mapping()
    event_id_name_map = sort_event_id_name_map_by_global_sort_order(event_id_name_map)

    # Get site rankings info for both users
    # Get users' medal counts, number of total solves, number of competitions participated in
    rankings1, user1_stats = __get_versus_page_info_for_user(user1)
    rankings2, user2_stats = __get_versus_page_info_for_user(user2)

    # Remove any events which neither user has participated in
    __remove_events_not_participated_in(event_id_name_map, rankings1, rankings2)

    return render_template("user/versus.html", username1=user1.username, username2=user2.username,
        rankings1=rankings1, rankings2=rankings2, event_id_name_map=event_id_name_map,
        user1_stats=user1_stats, user2_stats=user2_stats, errors=errors)
Example #4
0
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))
Example #5
0
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)
Example #6
0
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)