coding utf-8 import datetime from pytils import translit from django d

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8
import datetime
from pytils import translit
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from data.models import Event
from accounts.models import Punter
from gambling.managers import RateManager
class Bookie(models.Model):
title = models.CharField(max_length=32)
slug = models.SlugField(max_length=32)
image = StdImageField(upload_to="uploads/bookie/")
affiliate = models.URLField()
def __unicode__(self):
return self.title
class Rate(models.Model):
home = models.DecimalField(decimal_places=2, max_digits=5)
draw = models.DecimalField(decimal_places=2, max_digits=5)
away = models.DecimalField(decimal_places=2, max_digits=5)
under = models.DecimalField(decimal_places=2, max_digits=5, blank=True)
over = models.DecimalField(decimal_places=2, max_digits=5, blank=True)
handicap = models.DecimalField(decimal_places=2, max_digits=5, blank=True)
bookie = models.ForeignKey(Bookie)
event = models.ForeignKey(Event)
#managers
objects = RateManager()
#overriding methods
def __unicode__(self):
return "%s - %s vs %s: %s | %s | %s" % (self.bookie.title, self.event.home, self.event.away, self.home, self.draw, self.away)
class Tip(models.Model):
#variables
OPEN = 1
RUNNING = 2
CLOSED = 3
CANCELED = 4
STATUS = ((OPEN, _(u"Open")), (RUNNING, _(u"Running")), (CLOSED, _(u"Closed")),(CANCELED, _(u"Canceled")))
HOME = 1
DRAW = 2
AWAY = 3
UNKNOWN = 4
PICK = ((HOME, 1), (DRAW, u"x"), (AWAY, 2))
STRAIGHT = 1
ASIAN = 2
UNDEROVER = 3
BETTYPE = ((STRAIGHT, _(u"Straight")), (ASIAN, _(u"Asian Handicap")), (UNDEROVER, _(u"Under-Over")))
STAKE = zip(range(1, 11), range(1, 11))
#fields
slug = models.SlugField(max_length=100, editable=False)
punter = models.ForeignKey(Punter)
event = models.ForeignKey(Event)
content = models.TextField()
bet_type = models.SmallIntegerField(choices=BETTYPE)
bookie = models.ForeignKey(Bookie, editable=False)
pick = models.IntegerField(choices=PICK)
odds = models.DecimalField(decimal_places=2, max_digits=5, editable=False)
stake = models.PositiveIntegerField(choices=STAKE)
date = models.DateTimeField(editable=False)
status = models.IntegerField(choices=STATUS)
signal = models.BooleanField()
#url methods
show_url = lambda self: reverse("show-tip", kwargs={'year': self.date.year, 'month': str(self.date.month).zfill(2), 'day': str(self.date.day).zfill(2), 'slug': self.slug})
#custom methods
def title(self):
return "%s: %s - %s" % (self.event.league.title, self.event.home.title, self.event.away.title)
@property
def who_won(self):
if self.status != self.CLOSED:
return self.UNKNOWN
if self.event.home_score > self.event.away_score:
return self.HOME
elif self.event.home_score == self.event.away_score:
return self.DRAW
elif self.event.home_score < self.event.away_score:
return self.AWAY
def get_result(self):
if self.status != self.CLOSED or self.who_won == self.UNKNOWN:
return "/static/theme/img/icon-push.gif"
if self.pick == self.who_won:
return "/static/theme/img/icon-won.gif"
else:
return "/static/theme/img/icon-lost.gif"
#overriding methods
def __unicode__(self):
return "%s - %s" % (self.event.home.title, self.event.away.title)
def save(self):
if not self.id:
self.date = datetime.datetime.utcnow()
self.slug = translit.slugify("%s %s %s %s %s %s" % (self.punter.username, self.date.year, self.date.month, self.date.day, self.event.home.title, self.event.away.title))
#TODO - change logic of rate choosing
rate = Rate.objects.odds_for_event(self.event)[self.pick]
self.odds = getattr(rate, ("home", "draw", "away")[self.bet])
self.bookie = rate.bookie
super(Tip, self).save()
class Meta:
ordering = ['-date']