def add_opponent(team_id=0): if not g.user.is_team_leader(team_id): flash(u'You must be a team leader to add an opponent.', 'error') return redirect(url_for('teams.my_teams')) opponents = Opponent.query.\ filter_by(team_id=team_id).\ order_by(Opponent.name.asc()).\ all() api = request.values.get('api') == '1' form = OpponentForm() form.name.validators[0].values = [o.name for o in opponents] if form.validate_on_submit(): opponent = Opponent(team_id=team_id, name=form.name.data, tag=form.tag.data) db.session.add(opponent) db.session.commit() flash(u'The opponent was successfully added.', 'success') if api: return jsonify(success=True, opponent_id=opponent.id, csrf=form.csrf_token.data, opponent_name=opponent.name) if form.f.data == 'add_match': return redirect(url_for('matches.add', new_opponent_id=opponent.id)) elif form.f.data: return redirect( url_for('matches.show', action='edit', match_id=int(form.f.data), new_opponent_id=opponent.id)) return redirect(url_for('opponents', team_id=team_id)) form.f.data = request.values.get('f') or '' return rt('team_admin/opponent_form.html', team={ 'id': team_id, 'name': g.user.teams[team_id].name }, page={ 'top': 'my_teams', 'sub': 'opponents' }, adding=True, form=form)
def opponent(team_id, opponent_id, action): if not g.user.is_team_leader(team_id): flash(u'You must be a team leader to add an opponent.', 'error') return redirect(url_for('teams.my_teams')) opponent = Opponent.query.filter_by(id=opponent_id).first() if not opponent or not g.user.is_team_leader(opponent.team_id) \ or team_id != opponent.team_id: flash(u'Opponent not found.', 'error') return redirect(url_for('teams.my_teams')) if action == 'edit': opponents = Opponent.query.\ filter_by(team_id=team_id).\ order_by(Opponent.name.asc()).\ all() form = OpponentForm(request.form, obj=opponent) form.name.validators[0].values = [ o.name for o in opponents if o.id\ != opponent_id ] if form.validate_on_submit(): form.populate_obj(opponent) db.session.commit() flash(u'The opponent was successfully updated.', 'success') else: return rt('team_admin/opponent_form.html', page={ 'top': 'my_teams', 'sub': 'opponents' }, team={ 'id': team_id, 'name': g.user.teams[team_id].name }, opponent_id=opponent_id, adding=False, form=form) elif action == 'delete': if request.method == 'POST': db.session.delete(opponent) db.session.commit() flash(u'The opponent was successfully deleted.', 'success') return redirect(url_for('opponents', team_id=team_id))
def add_opponent(team_id=0): if not g.user.is_team_leader(team_id): flash(u'You must be a team leader to add an opponent.', 'error') return redirect(url_for('teams.my_teams')) opponents = Opponent.query.\ filter_by(team_id=team_id).\ order_by(Opponent.name.asc()).\ all() api = request.values.get('api') == '1' form = OpponentForm() form.name.validators[0].values = [ o.name for o in opponents ] if form.validate_on_submit(): opponent = Opponent(team_id=team_id, name=form.name.data, tag=form.tag.data) db.session.add(opponent) db.session.commit() flash(u'The opponent was successfully added.', 'success') if api: return jsonify(success=True, opponent_id=opponent.id, csrf=form.csrf_token.data, opponent_name=opponent.name) if form.f.data == 'add_match': return redirect(url_for('matches.add', new_opponent_id=opponent.id)) elif form.f.data: return redirect(url_for('matches.show', action='edit', match_id=int(form.f.data), new_opponent_id=opponent.id)) return redirect(url_for('opponents',team_id=team_id)) form.f.data = request.values.get('f') or '' return rt('team_admin/opponent_form.html', team={'id' : team_id, 'name':g.user.teams[team_id].name}, page={'top':'my_teams', 'sub':'opponents'}, adding=True, form=form)
def opponent(team_id, opponent_id, action): if not g.user.is_team_leader(team_id): flash(u'You must be a team leader to add an opponent.', 'error') return redirect(url_for('teams.my_teams')) opponent = Opponent.query.filter_by(id=opponent_id).first() if not opponent or not g.user.is_team_leader(opponent.team_id) \ or team_id != opponent.team_id: flash(u'Opponent not found.', 'error') return redirect(url_for('teams.my_teams')) if action == 'edit': opponents = Opponent.query.\ filter_by(team_id=team_id).\ order_by(Opponent.name.asc()).\ all() form = OpponentForm(request.form, obj=opponent) form.name.validators[0].values = [ o.name for o in opponents if o.id\ != opponent_id ] if form.validate_on_submit(): form.populate_obj(opponent) db.session.commit() flash(u'The opponent was successfully updated.', 'success') else: return rt('team_admin/opponent_form.html', page={'top':'my_teams', 'sub':'opponents'}, team={'id' : team_id, 'name':g.user.teams[team_id].name}, opponent_id=opponent_id, adding=False, form=form) elif action == 'delete': if request.method == 'POST': db.session.delete(opponent) db.session.commit() flash(u'The opponent was successfully deleted.', 'success') return redirect(url_for('opponents', team_id=team_id))