def players(self): session = Session() players = [{ 'first': p.first, 'last': p.last, 'position': p.position, 'id': p.id } for p in session.query(Player).all()] return players
def add_player(self): first = self.form_result['first'] last = self.form_result['last'] position = self.form_result['position'] session = Session() if session.query(Player).filter_by(first=first, last=last).count() > 0: h.flash("Player already exists!") return h.redirect_to(controller='roster') player = Player(first, last, position) session.add(player) session.commit() return h.redirect_to(controller='roster', action='index')
def save_player(self): id = self.form_result['id'] first = self.form_result['first'] last = self.form_result['last'] position = self.form_result['position'] session = Session() player = session.query(Player).filter_by(id=id).one() player.first = first player.last = last player.last = last player.position = position session.commit() return h.redirect_to(controller='roster')
def delete_player(self): session = Session() id = self.form_result['id'] first = self.form_result['first'] last = self.form_result['last'] position = self.form_result['position'] players = session.query(Player).filter_by(id=id, first=first, last=last, position=position).all() if len(players) <> 1: h.flash( "The player was modified by someone else while you were staring at the screen!" ) else: player = players[0] session.delete(player) session.commit() h.flash("Player %s was deleted" % player.id) return h.redirect_to(controller='roster')
def test_create_player(self): session = Session() player1 = Player('Josh', 'Juneau', 'forward') player2 = Player('Jim', 'Baker', 'forward') player3 = Player('Frank', 'Wierzbicki', 'defense') player4 = Player('Leo', 'Soto', 'defense') player5 = Player('Vic', 'Ng', 'center') session.add(player1) session.add(player2) session.add(player3) session.add(player4) session.add(player5) # But 5 are in the session, but not in the database assert 5 == session.query(Player).count() assert 0 == engine.execute( "select count(id) from player").fetchone()[0] session.commit() # Check that 5 records are all in the database assert 5 == session.query(Player).count() assert 5 == engine.execute( "select count(id) from player").fetchone()[0]
def cleanup(self): session = Session() for player in session.query(Player): session.delete(player) session.commit()
def players(self): session = Session() players = [{ 'first': p.first, 'last': p.last, 'position': p.position, 'id': p.id } for p in session.query(Player).all()] return players @jsonify def add_player(self): obj = json.loads(request.body) schema = PlayerForm() try: form_result = schema.to_python(obj) except formencode.Invalid, error: response.content_type = 'text/plain' return 'Invalid: ' + unicode(error) else: session = Session() first, last, position = obj['first'], obj['last'], obj['position'] if session.query(Player).filter_by(last=last, first=first, position=position).count() == 0: session.add(Player(first, last, position)) session.commit() return {'result': 'OK'} else: return {'result': 'fail', 'msg': 'Player already exists'}
def test_create_player(self): session = Session() player1 = Player('Josh', 'Juneau', 'forward') player2 = Player('Jim', 'Baker', 'forward') player3 = Player('Frank', 'Wierzbicki', 'defense') player4 = Player('Leo', 'Soto', 'defense') player5 = Player('Vic', 'Ng', 'center') session.add(player1) session.add(player2) session.add(player3) session.add(player4) session.add(player5) # But 5 are in the session, but not in the database assert 5 == session.query(Player).count() assert 0 == engine.execute("select count(id) from player").fetchone()[0] session.commit() # Check that 5 records are all in the database assert 5 == session.query(Player).count() assert 5 == engine.execute("select count(id) from player").fetchone()[0]
def edit_player(self, id): session = Session() player = session.query(Player).filter_by(id=id).one() c.player = player return render('edit_player.html')
def index(self): db_session = Session() c.page_title = 'Player List' c.players = session.query(Player).all() return render('list_players.html')