1👍
First of all, your model hierarchy is not well-designed. Both Participant
and Winner
models serve to similar purposes and have the same fields.
Moreover, it looks like Participant
is the superset of Winner
since everyone will be defined as participant at first than one of them will win the game and become a winner. So, I would recommend you to create Winner
by subclassing from Participant
and define same fields in the super class like below:
class Participant(models.Model):
user = models.ForeignKey('auth.User')
creation_date = models.DateTimeField()
phone_no = models.CharField(max_length=20)
def __unicode__(self):
return self.user.email
class Winner(Participant):
win_date = models.DateTimeField()
One beautiful thing about this approach is that Django will create an implicit OneToOneField
field from winner to participant so you’ll be directly connected to participant details of a winner automatically.
As for your actual question, about checking if a user has won any game in the last 4 months, you can do the following:
from datetime import timedelta
from django.utils.timezone import now
def has_won_any_game_since(user, period):
return Winner.objects.filter(user=user, win_date__gt=now() - period).exists()
def get_random_winner():
player_list = list(Participant.objects.all())
four_months = timedelta(days=120) # ~4 months
while len(player_list) > 0:
player = random.choice(player_list)
if not has_won_any_game_since(player.user, four_months):
return player
else:
player_list.remove(player)
else:
return None
>>> winner = get_random_winner()
<Winner: ozgur@winner.com, win_date='07/23/2014 07:23:86'> # win_date is supposed to be less than 120 days.
Hope this helps.