Mercurial > public > sg101
comparison contests/models.py @ 796:5977b43499f7
Modified contests to have multiple winners.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Fri, 04 Jul 2014 20:30:14 -0500 |
parents | 84aa49497718 |
children | 21946dc3662d |
comparison
equal
deleted
inserted
replaced
795:5d48fd80f27d | 796:5977b43499f7 |
---|---|
33 is_public = models.BooleanField(db_index=True) | 33 is_public = models.BooleanField(db_index=True) |
34 creation_date = models.DateTimeField(blank=True) | 34 creation_date = models.DateTimeField(blank=True) |
35 end_date = models.DateTimeField() | 35 end_date = models.DateTimeField() |
36 contestants = models.ManyToManyField(User, related_name='contests', | 36 contestants = models.ManyToManyField(User, related_name='contests', |
37 null=True, blank=True) | 37 null=True, blank=True) |
38 winner = models.ForeignKey(User, null=True, blank=True, | 38 winners = models.ManyToManyField(User, null=True, blank=True, |
39 related_name='winning_contests') | 39 related_name='winning_contests') |
40 win_date = models.DateTimeField(null=True, blank=True) | 40 win_date = models.DateTimeField(null=True, blank=True) |
41 meta_description = models.TextField() | 41 meta_description = models.TextField() |
42 num_winners = models.IntegerField(default=1, | |
43 verbose_name='Number of winners') | |
42 | 44 |
43 objects = models.Manager() | 45 objects = models.Manager() |
44 public_objects = PublicContestManager() | 46 public_objects = PublicContestManager() |
45 | 47 |
46 class Meta: | 48 class Meta: |
67 now = datetime.datetime.now() | 69 now = datetime.datetime.now() |
68 return self.creation_date <= now < self.end_date | 70 return self.creation_date <= now < self.end_date |
69 | 71 |
70 def can_enter(self): | 72 def can_enter(self): |
71 """ | 73 """ |
72 Returns True if the contest is still active and does not have a winner. | 74 Returns True if the contest is still active and does not have any |
75 winners. | |
73 | 76 |
74 """ | 77 """ |
75 return not self.winner and self.is_active() | 78 return not self.win_date and self.is_active() |
76 | 79 |
77 def pick_winner(self): | 80 def pick_winners(self): |
78 """ | 81 """ |
79 This function randomly picks a winner from all the contestants. | 82 This function randomly picks winners from all the contestants. |
80 | 83 |
81 """ | 84 """ |
82 user_ids = self.contestants.values_list('id', flat=True) | 85 user_ids = list(self.contestants.values_list('id', flat=True)) |
83 winner_id = random.choice(user_ids) | 86 |
84 self.winner = User.objects.get(id=winner_id) | 87 winner_count = min(len(user_ids), self.num_winners) |
88 if winner_count == 0: | |
89 return | |
90 | |
91 winner_ids = [] | |
92 for n in xrange(winner_count): | |
93 winner = random.choice(user_ids) | |
94 winner_ids.append(winner) | |
95 user_ids.remove(winner) | |
96 | |
97 winners = list(User.objects.filter(pk__in=winner_ids)) | |
98 self.winners.add(*winners) | |
85 self.win_date = datetime.datetime.now() | 99 self.win_date = datetime.datetime.now() |
86 | 100 |
87 def ogp_tags(self): | 101 def ogp_tags(self): |
88 """ | 102 """ |
89 Returns a dict of Open Graph Protocol meta tags. | 103 Returns a dict of Open Graph Protocol meta tags. |