annotate contests/models.py @ 893:3aecf9058130

Merge with upstream.
author Brian Neal <bgneal@gmail.com>
date Tue, 17 Feb 2015 18:59:06 -0600
parents 21946dc3662d
children e14f54f16dbc
rev   line source
bgneal@540 1 """
bgneal@540 2 Models for the contest application.
bgneal@540 3
bgneal@540 4 """
bgneal@540 5 import random
bgneal@540 6 import datetime
bgneal@540 7
bgneal@540 8 from django.db import models
bgneal@540 9 from django.contrib.auth.models import User
bgneal@540 10
bgneal@540 11
bgneal@540 12 class PublicContestManager(models.Manager):
bgneal@540 13 """
bgneal@540 14 The manager for all public contests.
bgneal@540 15
bgneal@540 16 """
bgneal@740 17 def get_queryset(self):
bgneal@740 18 return super(PublicContestManager, self).get_queryset().filter(is_public=True)
bgneal@540 19
bgneal@788 20 def get_current_contests(self):
bgneal@788 21 now = datetime.datetime.now()
bgneal@788 22 return self.filter(creation_date__lte=now, win_date__isnull=True)
bgneal@788 23
bgneal@540 24
bgneal@540 25 class Contest(models.Model):
bgneal@540 26 """
bgneal@540 27 A model to represent contests where users sign up to win something.
bgneal@540 28
bgneal@540 29 """
bgneal@540 30 title = models.CharField(max_length=64)
bgneal@540 31 slug = models.SlugField(max_length=64)
bgneal@540 32 description = models.TextField()
bgneal@540 33 is_public = models.BooleanField(db_index=True)
bgneal@540 34 creation_date = models.DateTimeField(blank=True)
bgneal@540 35 end_date = models.DateTimeField()
bgneal@540 36 contestants = models.ManyToManyField(User, related_name='contests',
bgneal@540 37 null=True, blank=True)
bgneal@796 38 winners = models.ManyToManyField(User, null=True, blank=True,
bgneal@540 39 related_name='winning_contests')
bgneal@540 40 win_date = models.DateTimeField(null=True, blank=True)
bgneal@540 41 meta_description = models.TextField()
bgneal@796 42 num_winners = models.IntegerField(default=1,
bgneal@796 43 verbose_name='Number of winners')
bgneal@540 44
bgneal@540 45 objects = models.Manager()
bgneal@540 46 public_objects = PublicContestManager()
bgneal@540 47
bgneal@540 48 class Meta:
bgneal@540 49 ordering = ['-creation_date']
bgneal@540 50
bgneal@540 51 def __unicode__(self):
bgneal@540 52 return self.title
bgneal@540 53
bgneal@540 54 @models.permalink
bgneal@540 55 def get_absolute_url(self):
bgneal@540 56 return ('contests-contest', [], {'slug': self.slug})
bgneal@540 57
bgneal@540 58 def save(self, *args, **kwargs):
bgneal@540 59 if not self.pk and not self.creation_date:
bgneal@540 60 self.creation_date = datetime.datetime.now()
bgneal@540 61
bgneal@540 62 super(Contest, self).save(*args, **kwargs)
bgneal@540 63
bgneal@540 64 def is_active(self):
bgneal@540 65 """
bgneal@540 66 Returns True if the contest is still active.
bgneal@540 67
bgneal@540 68 """
bgneal@540 69 now = datetime.datetime.now()
bgneal@540 70 return self.creation_date <= now < self.end_date
bgneal@540 71
bgneal@540 72 def can_enter(self):
bgneal@540 73 """
bgneal@796 74 Returns True if the contest is still active and does not have any
bgneal@796 75 winners.
bgneal@540 76
bgneal@540 77 """
bgneal@796 78 return not self.win_date and self.is_active()
bgneal@540 79
bgneal@867 80 def can_comment_on(self):
bgneal@867 81 return self.is_active()
bgneal@867 82
bgneal@796 83 def pick_winners(self):
bgneal@540 84 """
bgneal@796 85 This function randomly picks winners from all the contestants.
bgneal@540 86
bgneal@540 87 """
bgneal@796 88 user_ids = list(self.contestants.values_list('id', flat=True))
bgneal@796 89
bgneal@796 90 winner_count = min(len(user_ids), self.num_winners)
bgneal@796 91 if winner_count == 0:
bgneal@796 92 return
bgneal@796 93
bgneal@796 94 winner_ids = []
bgneal@796 95 for n in xrange(winner_count):
bgneal@796 96 winner = random.choice(user_ids)
bgneal@796 97 winner_ids.append(winner)
bgneal@796 98 user_ids.remove(winner)
bgneal@796 99
bgneal@796 100 winners = list(User.objects.filter(pk__in=winner_ids))
bgneal@796 101 self.winners.add(*winners)
bgneal@540 102 self.win_date = datetime.datetime.now()
bgneal@540 103
bgneal@540 104 def ogp_tags(self):
bgneal@540 105 """
bgneal@540 106 Returns a dict of Open Graph Protocol meta tags.
bgneal@540 107
bgneal@540 108 """
bgneal@540 109 return {
bgneal@540 110 'og:title': self.title,
bgneal@567 111 'og:type': 'article',
bgneal@540 112 'og:url': self.get_absolute_url(),
bgneal@540 113 'og:description': self.meta_description,
bgneal@540 114 }