annotate contests/models.py @ 697:67f8d49a9377

Cleaned up the code a bit. Separated the S3 stuff out into its own class. This class maybe should be in core. Still want to do some kind of context manager around the temporary file we are creating to ensure it gets deleted.
author Brian Neal <bgneal@gmail.com>
date Sun, 08 Sep 2013 21:02:58 -0500
parents ee87ea74d46b
children 3e1905e523be
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@540 17 def get_query_set(self):
bgneal@540 18 return super(PublicContestManager, self).get_query_set().filter(is_public=True)
bgneal@540 19
bgneal@540 20
bgneal@540 21 class Contest(models.Model):
bgneal@540 22 """
bgneal@540 23 A model to represent contests where users sign up to win something.
bgneal@540 24
bgneal@540 25 """
bgneal@540 26 title = models.CharField(max_length=64)
bgneal@540 27 slug = models.SlugField(max_length=64)
bgneal@540 28 description = models.TextField()
bgneal@540 29 is_public = models.BooleanField(db_index=True)
bgneal@540 30 creation_date = models.DateTimeField(blank=True)
bgneal@540 31 end_date = models.DateTimeField()
bgneal@540 32 contestants = models.ManyToManyField(User, related_name='contests',
bgneal@540 33 null=True, blank=True)
bgneal@540 34 winner = models.ForeignKey(User, null=True, blank=True,
bgneal@540 35 related_name='winning_contests')
bgneal@540 36 win_date = models.DateTimeField(null=True, blank=True)
bgneal@540 37 meta_description = models.TextField()
bgneal@540 38
bgneal@540 39 objects = models.Manager()
bgneal@540 40 public_objects = PublicContestManager()
bgneal@540 41
bgneal@540 42 class Meta:
bgneal@540 43 ordering = ['-creation_date']
bgneal@540 44
bgneal@540 45 def __unicode__(self):
bgneal@540 46 return self.title
bgneal@540 47
bgneal@540 48 @models.permalink
bgneal@540 49 def get_absolute_url(self):
bgneal@540 50 return ('contests-contest', [], {'slug': self.slug})
bgneal@540 51
bgneal@540 52 def save(self, *args, **kwargs):
bgneal@540 53 if not self.pk and not self.creation_date:
bgneal@540 54 self.creation_date = datetime.datetime.now()
bgneal@540 55
bgneal@540 56 super(Contest, self).save(*args, **kwargs)
bgneal@540 57
bgneal@540 58 def is_active(self):
bgneal@540 59 """
bgneal@540 60 Returns True if the contest is still active.
bgneal@540 61
bgneal@540 62 """
bgneal@540 63 now = datetime.datetime.now()
bgneal@540 64 return self.creation_date <= now < self.end_date
bgneal@540 65
bgneal@540 66 def can_enter(self):
bgneal@540 67 """
bgneal@540 68 Returns True if the contest is still active and does not have a winner.
bgneal@540 69
bgneal@540 70 """
bgneal@540 71 return not self.winner and self.is_active()
bgneal@540 72
bgneal@540 73 def pick_winner(self):
bgneal@540 74 """
bgneal@540 75 This function randomly picks a winner from all the contestants.
bgneal@540 76
bgneal@540 77 """
bgneal@540 78 user_ids = self.contestants.values_list('id', flat=True)
bgneal@540 79 winner_id = random.choice(user_ids)
bgneal@540 80 self.winner = User.objects.get(id=winner_id)
bgneal@540 81 self.win_date = datetime.datetime.now()
bgneal@540 82
bgneal@540 83 def ogp_tags(self):
bgneal@540 84 """
bgneal@540 85 Returns a dict of Open Graph Protocol meta tags.
bgneal@540 86
bgneal@540 87 """
bgneal@540 88 return {
bgneal@540 89 'og:title': self.title,
bgneal@567 90 'og:type': 'article',
bgneal@540 91 'og:url': self.get_absolute_url(),
bgneal@540 92 'og:description': self.meta_description,
bgneal@540 93 }