Beispiel #1
0
def delete_player():
    """Delete a player."""
    form = PlayerDeleteForm(request.form, csrf_enabled=False)
    if form.validate_on_submit():
        Player.delete(Player.get_by_id(form.player_id.data))
        flash('Player deleted!', 'success')
    else:
        flash_errors(form)
    return redirect(url_for('dashboard.get_players'))
Beispiel #2
0
def create_player():
    """Create a new player."""
    form = PlayerCreateForm(request.form, csrf_enabled=False)
    if form.validate_on_submit():
        Player.create(first_name=form.first_name.data,
                      last_name=form.last_name.data,
                      aga_id=form.aga_id.data,
                      aga_rank=form.aga_rank.data)
        flash('Player created!', 'success')
    else:
        flash_errors(form)
    players = Player.query.all()
    return render_template('dashboard/players.html',
                           players=players,
                           player_create_form=form)
Beispiel #3
0
def create_game():
    """Create a new game."""
    form = GameCreateForm(request.form)
    _set_game_create_choices(form)
    if form.validate_on_submit():
        white = Player.get_by_id(form.white_id.data)
        black = Player.get_by_id(form.black_id.data)
        played_at = None
        if form.played_at.data is not None:
            played_at = form.played_at.data.astimezone(timezone.utc)
        game = Game.create(white=white,
                           black=black,
                           winner=form.winner.data,
                           handicap=form.handicap.data,
                           komi=form.komi.data,
                           season=form.season.data,
                           episode=form.episode.data,
                           played_at=played_at)
        return jsonify(game.to_dict()), 201
    else:
        return jsonify(**form.errors), 404
Beispiel #4
0
def _set_game_create_choices(game_create_form):
    """
    Calculate choices for season and episode and update form.

    Should allow up to one more than current maxima.
    """
    max_season, max_episode = Game.get_max_season_ep()
    game_create_form.season.choices = [(s, s)
                                       for s in range(1, max_season + 2)]
    game_create_form.episode.choices = [(e, e)
                                        for e in range(1, max_episode + 2)]

    player_choices = [(player.id, '{} ({})'.format(player.full_name,
                                                   player.aga_id))
                      for player in Player.get_players()]
    game_create_form.white_id.choices = player_choices
    game_create_form.black_id.choices = player_choices
Beispiel #5
0
def get_player(player_id):
    """Get game history and statistics for player."""
    form = LoginForm(request.form)
    # Handle logging in
    if request.method == 'POST':
        if form.validate_on_submit():
            login_user(form.user)
            flash('You are logged in.', 'success')
            redirect_url = request.args.get('next') or url_for('public.home')
            return redirect(redirect_url)
        else:
            flash_errors(form)

    player = Player.get_by_id(player_id)

    return render_template('dashboard/player.html',
                           player=player,
                           episode_stats=player.episode_stats(),
                           season_stats=player.season_stats(),
                           league_stats=player.league_stats(),
                           login_form=form)
Beispiel #6
0
 def validate_player_id(form, field):
     """Check that players are not in extant games."""
     if len(Player.get_by_id(field.data).games) > 0:
         raise ValidationError(
             'Players with extant games cannot be deleted')