Mercurial > public > sg101
comparison gpp/contests/models.py @ 540:51fa1e0ca218
For #243, create a contests application.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 09 Jan 2012 01:13:08 +0000 |
parents | |
children | 0a8e6a9ccf53 |
comparison
equal
deleted
inserted
replaced
539:2f0a372c92b4 | 540:51fa1e0ca218 |
---|---|
1 """ | |
2 Models for the contest application. | |
3 | |
4 """ | |
5 import random | |
6 import datetime | |
7 | |
8 from django.db import models | |
9 from django.contrib.auth.models import User | |
10 | |
11 | |
12 class PublicContestManager(models.Manager): | |
13 """ | |
14 The manager for all public contests. | |
15 | |
16 """ | |
17 def get_query_set(self): | |
18 return super(PublicContestManager, self).get_query_set().filter(is_public=True) | |
19 | |
20 | |
21 class Contest(models.Model): | |
22 """ | |
23 A model to represent contests where users sign up to win something. | |
24 | |
25 """ | |
26 title = models.CharField(max_length=64) | |
27 slug = models.SlugField(max_length=64) | |
28 description = models.TextField() | |
29 is_public = models.BooleanField(db_index=True) | |
30 creation_date = models.DateTimeField(blank=True) | |
31 end_date = models.DateTimeField() | |
32 contestants = models.ManyToManyField(User, related_name='contests', | |
33 null=True, blank=True) | |
34 winner = models.ForeignKey(User, null=True, blank=True, | |
35 related_name='winning_contests') | |
36 win_date = models.DateTimeField(null=True, blank=True) | |
37 meta_description = models.TextField() | |
38 | |
39 objects = models.Manager() | |
40 public_objects = PublicContestManager() | |
41 | |
42 class Meta: | |
43 ordering = ['-creation_date'] | |
44 | |
45 def __unicode__(self): | |
46 return self.title | |
47 | |
48 @models.permalink | |
49 def get_absolute_url(self): | |
50 return ('contests-contest', [], {'slug': self.slug}) | |
51 | |
52 def save(self, *args, **kwargs): | |
53 if not self.pk and not self.creation_date: | |
54 self.creation_date = datetime.datetime.now() | |
55 | |
56 super(Contest, self).save(*args, **kwargs) | |
57 | |
58 def is_active(self): | |
59 """ | |
60 Returns True if the contest is still active. | |
61 | |
62 """ | |
63 now = datetime.datetime.now() | |
64 return self.creation_date <= now < self.end_date | |
65 | |
66 def can_enter(self): | |
67 """ | |
68 Returns True if the contest is still active and does not have a winner. | |
69 | |
70 """ | |
71 return not self.winner and self.is_active() | |
72 | |
73 def pick_winner(self): | |
74 """ | |
75 This function randomly picks a winner from all the contestants. | |
76 | |
77 """ | |
78 user_ids = self.contestants.values_list('id', flat=True) | |
79 winner_id = random.choice(user_ids) | |
80 self.winner = User.objects.get(id=winner_id) | |
81 self.win_date = datetime.datetime.now() | |
82 | |
83 def ogp_tags(self): | |
84 """ | |
85 Returns a dict of Open Graph Protocol meta tags. | |
86 | |
87 """ | |
88 return { | |
89 'og:title': self.title, | |
90 'og:type': 'game', | |
91 'og:url': self.get_absolute_url(), | |
92 'og:description': self.meta_description, | |
93 } |