annotate contests/models.py @ 788:84aa49497718

Added a current contests template tag & installed in sidebar.
author Brian Neal <bgneal@gmail.com>
date Sun, 18 May 2014 17:43:37 -0500
parents 3e1905e523be
children 5977b43499f7
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@540 38 winner = models.ForeignKey(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@540 42
bgneal@540 43 objects = models.Manager()
bgneal@540 44 public_objects = PublicContestManager()
bgneal@540 45
bgneal@540 46 class Meta:
bgneal@540 47 ordering = ['-creation_date']
bgneal@540 48
bgneal@540 49 def __unicode__(self):
bgneal@540 50 return self.title
bgneal@540 51
bgneal@540 52 @models.permalink
bgneal@540 53 def get_absolute_url(self):
bgneal@540 54 return ('contests-contest', [], {'slug': self.slug})
bgneal@540 55
bgneal@540 56 def save(self, *args, **kwargs):
bgneal@540 57 if not self.pk and not self.creation_date:
bgneal@540 58 self.creation_date = datetime.datetime.now()
bgneal@540 59
bgneal@540 60 super(Contest, self).save(*args, **kwargs)
bgneal@540 61
bgneal@540 62 def is_active(self):
bgneal@540 63 """
bgneal@540 64 Returns True if the contest is still active.
bgneal@540 65
bgneal@540 66 """
bgneal@540 67 now = datetime.datetime.now()
bgneal@540 68 return self.creation_date <= now < self.end_date
bgneal@540 69
bgneal@540 70 def can_enter(self):
bgneal@540 71 """
bgneal@540 72 Returns True if the contest is still active and does not have a winner.
bgneal@540 73
bgneal@540 74 """
bgneal@540 75 return not self.winner and self.is_active()
bgneal@540 76
bgneal@540 77 def pick_winner(self):
bgneal@540 78 """
bgneal@540 79 This function randomly picks a winner from all the contestants.
bgneal@540 80
bgneal@540 81 """
bgneal@540 82 user_ids = self.contestants.values_list('id', flat=True)
bgneal@540 83 winner_id = random.choice(user_ids)
bgneal@540 84 self.winner = User.objects.get(id=winner_id)
bgneal@540 85 self.win_date = datetime.datetime.now()
bgneal@540 86
bgneal@540 87 def ogp_tags(self):
bgneal@540 88 """
bgneal@540 89 Returns a dict of Open Graph Protocol meta tags.
bgneal@540 90
bgneal@540 91 """
bgneal@540 92 return {
bgneal@540 93 'og:title': self.title,
bgneal@567 94 'og:type': 'article',
bgneal@540 95 'og:url': self.get_absolute_url(),
bgneal@540 96 'og:description': self.meta_description,
bgneal@540 97 }