Skip to content

Commit

Permalink
Deny bet for games after the threshold.
Browse files Browse the repository at this point in the history
  • Loading branch information
Osvaldo Matos-Junior committed May 29, 2014
1 parent 902f917 commit ee6c9bb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 14 deletions.
1 change: 1 addition & 0 deletions bolao/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Config(object):
# BOLAO CONFIGURATION
BOLAO_BET_CHAMPIONS_LIMIT = datetime(2014, 6, 17, 23, 59)
BOLAO_BET_SCORER_LIMIT = datetime(2014, 6, 12, 16, 00)
BOLAO_BET_GAME_LIMIT = timedelta(hours=-1)


class Dev(Config):
Expand Down
3 changes: 3 additions & 0 deletions bolao/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def is_active(self):
def __repr__(self):
return self.name


class Team(db.Model):
id = db.Column(db.Integer, primary_key=True)
alias = db.Column(db.String(3), unique=True)
Expand All @@ -35,6 +36,7 @@ class Team(db.Model):
def __repr__(self):
return self.name


class Scorer(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
Expand Down Expand Up @@ -63,6 +65,7 @@ class Game(db.Model):
def __repr__(self):
return u"%s vs %s" % (self.team1, self.team2)


class BetGame(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
Expand Down
14 changes: 10 additions & 4 deletions bolao/templates/games.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ <h4>{{day.strftime("%A, %d de %B de %Y").decode('utf-8')}}</h4>
<td class="text-right">{{game.team1.alias}}<img class="flag" src="{{url_for('static', filename='images/flags/%s.png'%game.team1.alias)}}" width="30" height="30" alt="{{game.team1}}" /></td>
<td class="score">{{ '%d x %d' % (game.score_team1, game.score_team2) if game.score_team1 is number else 'x'}}<br />
{% if game.id in bets -%}
{% set bet = bets[game.id] -%}
<small><a href="{{url_for('.bet_game_view', game_id=game.id)}}">{{bet.score_team1}} x {{bet.score_team2}}</a></small>
{%- else %}
<small><a href="{{url_for('.bet_game_view', game_id=game.id)}}">apostar</a></small>
{% set bet = bets[game.id] -%}
{% if now() < game.time + config.BOLAO_BET_GAME_LIMIT %}
<small><a href="{{url_for('.bet_game_view', game_id=game.id)}}">{{bet.score_team1}} x {{bet.score_team2}}</a></small>
{% else %}
<small>{{bet.score_team1}} x {{bet.score_team2}}</small>
{% endif %}
{%- elif now() < game.time + config.BOLAO_BET_GAME_LIMIT %}
<small><a href="{{url_for('.bet_game_view', game_id=game.id)}}">apostar</a></small>
{%- else %}
<small>expirou</small>
{%- endif %}
</td>
<td><img class="flag" src="{{url_for('static', filename='images/flags/%s.png'%game.team2.alias)}}" width="30" height="30" alt="{{game.team2}}" />{{game.team2.alias}}</td>
Expand Down
4 changes: 2 additions & 2 deletions bolao/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ <h2>{{profile.score_games + (profile.bet_champions.score or 0) + (profile.bet_sc
<th width=50>pts</th>
</tr>
</thead>
{% for bet in profile.games -%}
{% for bet in profile.games|sort(attribute='game.time') -%}
{% set game = bet.game %}
<tr>
<td class="text-right"><img class="flag" src="{{url_for('static', filename='images/flags/%s.png'%game.team1.alias)}}" width="30" height="30" alt="{{game.team1}}" /></td>
<td class="score">{{ '%d x %d' % (bet.score_team1, bet.score_team2)}}<br />
</td>
<td><img class="flag" src="{{url_for('static', filename='images/flags/%s.png'%game.team2.alias)}}" width="30" height="30" alt="{{game.team2}}" /></td>
<td>{{bet.score or '0'}}</td>
<td>{{bet.score}}</td>
</tr>
{% endfor %}
</table>
Expand Down
10 changes: 10 additions & 0 deletions bolao/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def index():
@app.route('/profile/<int:id>')
def profile(id):
profile = User.query.get(id)
# show only expired games for visitor
if g.user.get_id() != str(id): # See Flask-Login reference
profile.games = [bet for bet in profile.games if __is_game_expired(bet.game)]
return render_template('profile.html', profile=profile)


Expand Down Expand Up @@ -57,14 +60,21 @@ def __group_by_day(games):
groups[key] = [game]
return groups

def __is_game_expired(game):
return datetime.now() > game.time + flask.current_app.config['BOLAO_BET_GAME_LIMIT']


@app.route('/apostar_jogo/<int:game_id>')
@login_required
def bet_game_view(game_id):
if not g.user.is_active():
flask.flash(INACTIVE_USER_MESSAGE, category='warning')
return redirect(url_for('.games'))

game = Game.query.get(game_id)
if __is_game_expired(game):
flask.flash(u'O prazo para apostar em <strong>%s</strong> expirou.' % game, category='warning')
return redirect(url_for('.games'))
bet = BetGame.query.filter_by(user=g.user, game=game).first()
return render_template('bet_game.html', game=game, bet=bet)

Expand Down
51 changes: 43 additions & 8 deletions tests/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

import datetime
from datetime import datetime, timedelta

from flask import url_for
from flask.ext.testing import TestCase as FlaskTestCase
Expand Down Expand Up @@ -38,7 +38,7 @@ def create_app(self):

def setUp(self):
db.create_all()
self.user = User(name="Test", email="[email protected]")
self.user = User(name="Test", email="[email protected]", active=True)
db.session.add(self.user)
db.session.commit()

Expand All @@ -50,7 +50,7 @@ def test_games(self):

bra = Team(name="Brasil", alias="BRA")
usa = Team(name="United States", alias="USA")
now = datetime.datetime.utcnow()
now = datetime.now() + timedelta(days=1)
game = Game(team1=bra, team2=usa, time=now)
db.session.add(bra)
db.session.add(usa)
Expand All @@ -64,15 +64,50 @@ def test_games(self):
self.assert200(response)
self.assertIn('BRA', response.data)
self.assertIn('USA', response.data)
self.assertIn('apostar', response.data)
# self.assert_template_used('games.html')

def test_view_games_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('.games'))
self.assertIn('expirou', response.data)

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_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.datetime.utcnow()
now = datetime.now()
game = Game(team1=bra, team2=usa, time=now)
db.session.add(bra)
db.session.add(usa)
Expand Down Expand Up @@ -104,14 +139,14 @@ def tearDown(self):
db.drop_all()

def test_view_champions_limit(self):
self.app.config['BOLAO_BET_CHAMPIONS_LIMIT'] = datetime.datetime(1990, 1, 1)
self.app.config['BOLAO_BET_CHAMPIONS_LIMIT'] = datetime(1990, 1, 1)
response = self.client.get('/campeoes')
self.assertRedirects(response, url_for('bolao.index'))
self.assert_flashes("O prazo para escolher as primeiras colocadas expirou.", category='warning')

def test_bet_champions_limit(self):
data = {}
self.app.config['BOLAO_BET_CHAMPIONS_LIMIT'] = datetime.datetime(1990, 1, 1)
self.app.config['BOLAO_BET_CHAMPIONS_LIMIT'] = datetime(1990, 1, 1)
response = self.client.post('/campeoes', data=data)
self.assertRedirects(response, url_for('bolao.index'))
self.assert_flashes("O prazo para escolher as primeiras colocadas expirou.", category='warning')
Expand Down Expand Up @@ -145,14 +180,14 @@ def tearDown(self):
db.drop_all()

def test_view_scorer_limit(self):
self.app.config['BOLAO_BET_SCORER_LIMIT'] = datetime.datetime(1990, 1, 1)
self.app.config['BOLAO_BET_SCORER_LIMIT'] = datetime(1990, 1, 1)
response = self.client.get('/artilheiros')
self.assertRedirects(response, url_for('bolao.index'))
self.assert_flashes("O prazo para escolher os artilheiros expirou.", category='warning')

def test_bet_scorer_limit(self):
data = {}
self.app.config['BOLAO_BET_SCORER_LIMIT'] = datetime.datetime(1990, 1, 1)
self.app.config['BOLAO_BET_SCORER_LIMIT'] = datetime(1990, 1, 1)
response = self.client.post('/artilheiros', data=data)
self.assertRedirects(response, url_for('bolao.index'))
self.assert_flashes("O prazo para escolher os artilheiros expirou.", category='warning')
Expand Down

0 comments on commit ee6c9bb

Please sign in to comment.