Esempio n. 1
0
def handle_finish_round(round_id):
    current = rounds.get_one_round(round_id)
    if current is None:
        raise errors.NotFound(what='Round')
    if current['finished_at'] is not None:
        raise errors.RoundAlreadyFinished()
    rounds.finish_round(round_id, datetime.utcnow())
    result = rounds.get_one_round(round_id)
    if result is None:
        raise errors.NotFound(what='Round')
    return create_success(result)
Esempio n. 2
0
def handle_get_one_round(round_id: int):
    result = rounds.get_one_round(round_id)

    if result is None:
        raise errors.NotFound(what='Round')

    return create_success(result)
Esempio n. 3
0
def handle_get_recent_round():
    result = rounds.get_recent_round()

    if len(result) is None:
        raise errors.NotFound(what='Round')

    return create_success(result)
Esempio n. 4
0
def handle_stats(round_id: int):
    current_round = rounds.get_one_round(round_id)
    if current_round is None:
        raise errors.NotFound('Round for stats')
    if current_round['finished_at'] is None:
        raise errors.RoundStillActive()
    stats = votes.get_stats(round_id)
    return create_success(stats)
Esempio n. 5
0
def handle_vote(round_id):
    data = request.json
    vote = data.get('vote')
    username = data.get('username')

    if vote < VOTE_MIN_VALUE:
        raise errors.InvalidVote(
            f'Your vote should be at least {VOTE_MIN_VALUE}.')
    if vote > VOTE_MAX_VALUE:
        raise errors.InvalidVote(
            f'Your vote should be at most {VOTE_MAX_VALUE}.')
    if votes.is_voted(round_id, username):
        raise errors.InvalidVote(f'You have already voted.')

    current_round = rounds.get_one_round(round_id)
    if current_round is None:
        raise errors.NotFound('Round for vote')
    if current_round['finished_at'] is not None:
        raise errors.InvalidVote(f'The round is already finished.')

    votes.add_vote(round_id, username, vote)

    return create_success({})
Esempio n. 6
0
def handle_start_round():
    round_id = rounds.create_round(datetime.utcnow())
    result = rounds.get_one_round(round_id)
    if result is None:
        raise errors.NotFound(what='Round')
    return create_success(result)