Ejemplo n.º 1
0
def get_shell_account_hook():
    if api.config.enable_shell:
        return WebSuccess(data=api.team.get_shell_account())
    return WebError(data="Shell is not available.")
Ejemplo n.º 2
0
def logout_hook():
    if api.auth.is_logged_in():
        api.auth.logout()
        return WebSuccess("Successfully logged out.")
    else:
        return WebError("You do not appear to be logged in.")
Ejemplo n.º 3
0
import api
import bson
from api.annotations import (api_wrapper, block_after_competition,
                             block_before_competition, check_csrf, log_action,
                             require_admin, require_login, require_teacher)
from api.common import WebError, WebSuccess
from flask import (Blueprint, Flask, render_template, request,
                   send_from_directory, session)

blueprint = Blueprint("stats_api", __name__)


@blueprint.route('/team/solved_problems', methods=['GET'])
@api_wrapper
@require_login
@block_before_competition(WebError("The competition has not begun yet!"))
def get_team_solved_problems_hook():
    tid = request.args.get("tid", None)
    stats = {
        "problems": api.stats.get_problems_by_category(),
        "members": api.stats.get_team_member_stats(tid)
    }

    return WebSuccess(data=stats)


@blueprint.route('/team/score_progression', methods=['GET'])
@api_wrapper
@require_login
@block_before_competition(WebError("The competition has not begun yet!"))
def get_team_score_progression():
Ejemplo n.º 4
0
def get_all_users_hook():
    users = api.user.get_all_users()
    if users is None:
        return WebError("There was an error query users from the database.")
    return WebSuccess(data=users)
Ejemplo n.º 5
0
def get_memeber_information_hook(gid=None):
    gid = request.args.get("gid")
    if not api.group.is_owner_of_group(gid):
        return WebError("You do not own that group!")

    return WebSuccess(data=api.group.get_member_information(gid=gid))
Ejemplo n.º 6
0
def get_all_problems_hook():
    problems = api.problem.get_all_problems()
    if problems is None:
        return WebError("There was an error querying problems from the database.")
    return WebSuccess(data=problems)
Ejemplo n.º 7
0
def get_team_score_hook():
    score = api.stats.get_score(tid=api.user.get_user()['tid'])
    if score is not None:
        return WebSuccess(data={'score': score})
    return WebError("There was an error retrieving your score.")
Ejemplo n.º 8
0
    return WebSuccess(data=api.team.get_team_information())


@app.route('/api/team/score', methods=['GET'])
@api_wrapper
@require_login
def get_team_score_hook():
    score = api.stats.get_score(tid=api.user.get_user()['tid'])
    if score is not None:
        return WebSuccess(data={'score': score})
    return WebError("There was an error retrieving your score.")

@app.route('/api/stats/team/solved_problems', methods=['GET'])
@api_wrapper
@require_login
@block_before_competition(WebError("The competition has not begun yet!"))
def get_team_solved_problems_hook():
    tid = request.args.get("tid", None)
    stats = {
        "problems": api.stats.get_problems_by_category(),
        "members": api.stats.get_team_member_stats(tid)
    }

    return WebSuccess(data=stats)

@app.route('/api/stats/team/score_progression', methods=['GET'])
@api_wrapper
@require_login
@block_before_competition(WebError("The competition has not begun yet!"))
def get_team_score_progression():
    category = request.form.get("category", None)
Ejemplo n.º 9
0
def get_scoreboard_hook(board, page):
    def get_user_pos(scoreboard, tid):
        for pos, team in enumerate(scoreboard):
            if team["tid"] == tid:
                return pos
        return 1

    user = None
    if api.auth.is_logged_in():
        user = api.user.get_user()

    # Old board, limit 1-50
    if board is None:
        result = {'tid': 0, 'groups': []}
        global_board = api.stats.get_all_team_scores(include_ineligible=True)
        result['global'] = {
            'name': 'global',
            'pages': math.ceil(len(global_board) / scoreboard_page_len),
            'start_page': 1
        }
        if user is None:
            result['global']['scoreboard'] = global_board[:scoreboard_page_len]
        else:
            result['tid'] = user['tid']
            global_pos = get_user_pos(global_board, user["tid"])
            start_slice = math.floor(global_pos / 50) * 50
            result['global']['scoreboard'] = global_board[
                start_slice:start_slice + 50]
            result['global']['start_page'] = math.ceil((global_pos + 1) / 50)

            result['country'] = user["country"]
            student_board = api.stats.get_all_team_scores()
            student_pos = get_user_pos(student_board, user["tid"])
            start_slice = math.floor(student_pos / 50) * 50
            result['student'] = {
                'name': 'student',
                'pages': math.ceil(len(student_board) / scoreboard_page_len),
                'scoreboard': student_board[start_slice:start_slice + 50],
                'start_page': math.ceil((student_pos + 1) / 50),
            }

            for group in api.team.get_groups(uid=user["uid"]):
                # this is called on every scoreboard pageload and should be cached
                # to support large groups
                group_board = api.stats.get_group_scores(gid=group['gid'])
                group_pos = get_user_pos(group_board, user["tid"])
                start_slice = math.floor(group_pos / 50) * 50
                result['groups'].append({
                    'gid':
                    group['gid'],
                    'name':
                    group['name'],
                    'scoreboard':
                    group_board[start_slice:start_slice + 50],
                    'pages':
                    math.ceil(len(group_board) / scoreboard_page_len),
                    'start_page':
                    math.ceil((group_pos + 1) / 50),
                })

        return WebSuccess(data=result)
    else:
        if board in ["groups", "global", "student"]:
            # 1-index page
            start = scoreboard_page_len * (page - 1)
            end = start + scoreboard_page_len
            result = []
            if api.auth.is_logged_in():
                user = api.user.get_user()
            if board == "groups":
                for group in api.team.get_groups(uid=user.get("uid")):
                    group_board = api.stats.get_group_scores(gid=group['gid'])
                    result.append({
                        'gid': group['gid'],
                        'name': group['name'],
                        'scoreboard': group_board[start:end]
                    })
            elif board == "global":
                result = api.stats.get_all_team_scores(
                    include_ineligible=True)[start:end]
            elif board == "student":
                result = api.stats.get_all_team_scores()[start:end]
            else:
                result = []
            return WebSuccess(data=result)
        else:
            return WebError("A valid board must be specified")