def ranking(request): start_time = time.time() current_games, current_games_bets, my_current_games_bets, scores = build_ranking_data(request) elapsed_time = time.time() - start_time print '[1]: %.3f' % (elapsed_time) start_time = time.time() round_of_16_matches = Game.get_round_of_16_games() quarter_finals_matches = Game.get_quarter_finals_games() semi_finals = Game.get_semi_finals_games() finals = Game.get_finals_games() rendered_template = render_to_string('ranking.html', {'bet_room': request.user.player.bet_room, 'scores': scores, 'my_current_games_bets': my_current_games_bets, 'current_games': current_games, 'current_games_ids': map(lambda x: x.id, current_games), 'current_games_bets': current_games_bets, 'me': request.user, 'round_of_16_matches': round_of_16_matches, 'quarter_finals_matches': quarter_finals_matches, 'semi_finals': semi_finals, 'finals': finals, 'third_place_dispute': finals[0], 'final_match': finals[1]}, RequestContext(request)) elapsed_time = time.time() - start_time print '[2]: %.3f' % (elapsed_time) return HttpResponse(rendered_template, content_type="text/html")
def setUp(self): db.create_all() bra = Team(name='Brasil', alias='BRA') arg = Team(name='Argentina', alias='ARG') uru = Team(name='Uruguai', alias='URU') chi = Team(name='Chile', alias='CHI') self.game1 = Game(team1=bra, team2=arg) self.game2 = Game(team1=bra, team2=uru) self.game3 = Game(team1=bra, team2=chi) self.game4 = Game(team1=arg, team2=uru) self.game5 = Game(team1=arg, team2=bra) self.game6 = Game(team1=arg, team2=chi) self.game7 = Game(team1=uru, team2=bra) self.game8 = Game(team1=uru, team2=arg) self.game9 = Game(team1=uru, team2=chi) db.session.add(self.game1) db.session.add(self.game2) db.session.add(self.game3) db.session.add(self.game4) db.session.add(self.game5) db.session.add(self.game6) db.session.add(self.game7) db.session.add(self.game8) db.session.commit()
def populate(self): from bolao.database import db from bolao.models import Game, Team def find_team(name, alias): team = Team.query.filter_by(alias=alias).first() if not team: team = Team(name=name, alias=alias) db.session.add(team) return team data = open('games.json') for _game in json.load(data): team1 = find_team(_game['team1'], _game['alias_team1']) team2 = find_team(_game['team2'], _game['alias_team2']) game = Game() game.team1 = team1 game.team2 = team2 date_time = _game['date_time'] #FIXME drop the day of week date_time = " ".join(date_time.split()[1:]) game.time = datetime.datetime.strptime(date_time, '%d/%m/%Y - %H:%M') game.place = _game['place'] game.round = _game['round'] game.group = _game['group'] db.session.add(game) db.session.commit()
def test_bet_game_duplicated(self): bra = Team(name="Brasil", alias="BRA") usa = Team(name="United States", alias="USA") time = datetime(2014, 6, 15) game = Game(team1=bra, team2=usa, time=time) db.session.add(bra) db.session.add(usa) db.session.add(game) db.session.commit() with self.client.session_transaction() as sess: sess['user_id'] = self.user.id data = {'game_id': game.id, 'score_team1': 1, 'score_team2': 1} # first request response = self.client.post(url_for('.bet_game'), data=data) self.assertStatus(response, 302) bets = BetGame.query.filter_by(game=game, user=self.user).all() self.assertEqual(1, len(bets)) before = bets[0] self.assertIsNone(before.updated_at) # again response = self.client.post(url_for('.bet_game'), data=data) self.assertStatus(response, 302) bets = BetGame.query.filter_by(game=game, user=self.user).all() self.assertEqual(1, len(bets)) after = bets[0] self.assertIsNotNone(after.updated_at)
def test_view_games_after_limit(self): bra = Team(name="Brasil", alias="BRA") usa = Team(name="United States", alias="USA") time = datetime(2014, 6, 12) game = Game(team1=bra, team2=usa, time=time) db.session.add(bra) db.session.add(usa) db.session.add(game) db.session.commit() with self.client.session_transaction() as sess: sess['user_id'] = self.user.id response = self.client.get(url_for('.games', show='all')) self.assertIn('expirou', response.data)
def setUp(self): db.create_all() bra = Team(name='Brasil', alias='BRA') arg = Team(name='Argentina', alias='ARG') game = Game(team1=bra, team2=arg) db.session.add(game) user = User(name="Test", email="*****@*****.**", active=True) user2 = User(name="Test2", email="*****@*****.**", active=True) db.session.add(user) db.session.add(user2) db.session.commit() self.game = game self.user = user self.user2 = user2
def build_ranking_data(request): start_time = time.time() scores = get_scores(request.user.player.bet_room) elapsed_time = time.time() - start_time print '[build_ranking_data.1]: %.3f' % (elapsed_time) start_time = time.time() current_games = Game.get_current_games() current_games_bets = [] my_current_games_bets = [] ranking = 1 previous_score = None for score in scores: """ adding ranking to each score """ if previous_score is not None: if previous_score.total_score != score.total_score: score.ranking = ranking else: score.ranking = None else: score.ranking = ranking ranking += 1 previous_score = score score.set_games_for_variation(current_games) game_bets = [] for game in current_games: next_bet = score.get_bet(game.id) if score.player == request.user.player: my_current_games_bets.append(next_bet) else: game_bets.append(next_bet) if score.player != request.user.player: current_games_bets.append({'first_name': score.player.user.first_name, 'last_name': score.player.user.last_name, 'bets': game_bets}) elapsed_time = time.time() - start_time print '[build_ranking_data.2]: %.3f' % (elapsed_time) return current_games, current_games_bets, my_current_games_bets, scores
def test_bet_game_view_after_limit(self): bra = Team(name="Brasil", alias="BRA") usa = Team(name="United States", alias="USA") now = datetime.now() + timedelta(days=-1) game = Game(team1=bra, team2=usa, time=now) db.session.add(bra) db.session.add(usa) db.session.add(game) db.session.commit() with self.client.session_transaction() as sess: sess['user_id'] = self.user.id response = self.client.get(url_for('.bet_game_view', game_id=game.id)) self.assertRedirects(response, url_for('.games')) expected_message = u"O prazo para apostar em <strong>%s</strong> expirou." % game self.assert_flashes(expected_message, category='warning')
def test_games(self): bra = Team(name="Brasil", alias="BRA") usa = Team(name="United States", alias="USA") now = datetime.now() + timedelta(days=1) game = Game(team1=bra, team2=usa, time=now) db.session.add(bra) db.session.add(usa) db.session.add(game) db.session.commit() with self.client.session_transaction() as sess: sess['user_id'] = self.user.id response = self.client.get("/jogos") self.assert200(response) self.assertIn('BRA', response.data) self.assertIn('USA', response.data) self.assertIn('apostar', response.data)
def test_bet_games_with_inactive_user(self): from bolao.views import INACTIVE_USER_MESSAGE bra = Team(name="Brasil", alias="BRA") usa = Team(name="United States", alias="USA") now = datetime.now() game = Game(team1=bra, team2=usa, time=now) db.session.add(bra) db.session.add(usa) db.session.add(game) db.session.commit() user = User(name="Inactive", email="*****@*****.**", active=False) db.session.add(user) db.session.commit() with self.client.session_transaction() as sess: sess['user_id'] = user.id response = self.client.get(url_for('.bet_game_view', game_id=game.id)) self.assertRedirects(response, url_for('.games')) self.assert_flashes(INACTIVE_USER_MESSAGE, category='warning')