comparison polls/models.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/polls/models.py@ac9217eef610
children eeaf387803c6
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Models for the Polls application.
3
4 """
5 import datetime
6
7 from django.contrib.auth.models import User
8 from django.db import models
9 from django.db.models import Q
10
11
12 class PollManager(models.Manager):
13 """Manager for the Poll model"""
14
15 def get_current_polls(self):
16 now = datetime.datetime.now()
17 return self.filter(
18 Q(is_enabled=True),
19 Q(start_date__lte=now),
20 Q(end_date__isnull=True) | Q(end_date__gte=now))
21
22 def get_old_polls(self):
23 now = datetime.datetime.now()
24 return self.filter(
25 is_enabled=True,
26 end_date__isnull=False,
27 end_date__lt=now)
28
29
30 class Poll(models.Model):
31 """Model to represent polls"""
32 start_date = models.DateTimeField(db_index=True,
33 help_text='Date/time the poll will be eligible for voting.',)
34 end_date = models.DateTimeField(blank=True, null=True, db_index=True,
35 help_text='Date/time the poll will be ineligible for voting. '
36 'Leave blank for an open ended poll.')
37 is_enabled = models.BooleanField(default=True, db_index=True,
38 help_text='Check to allow the poll to be viewed on the site.')
39 question = models.CharField(max_length=200)
40
41 objects = PollManager()
42
43 def __unicode__(self):
44 return self.question
45
46 class Meta:
47 ordering = ('-start_date', )
48 get_latest_by = 'start_date'
49
50 @models.permalink
51 def get_absolute_url(self):
52 return ('polls-detail', [], {'poll_id': str(self.id)})
53
54 def results(self):
55 """
56 Returns a tuple; element 0 is the total votes, element 1 is a list of
57 {choice, votes, pct}
58 """
59 choices = []
60 total_votes = 0
61 for choice in self.choice_set.all():
62 total_votes += choice.votes
63 choices.append({'choice': choice.choice, 'votes': choice.votes, 'pct': 0.0})
64
65 if total_votes > 0:
66 for choice in choices:
67 choice['pct'] = float(choice['votes']) / total_votes * 100.0
68
69 return (total_votes, choices)
70
71 def _total_votes(self):
72 """
73 Returns the number of votes cast in this poll to date.
74
75 """
76 if not hasattr(self, '_total_votes_cache'):
77 self._total_votes_cache = sum(choice.votes for choice in
78 self.choice_set.all())
79 return self._total_votes_cache
80 total_votes = property(_total_votes)
81
82 def is_open(self):
83 now = datetime.datetime.now()
84 return self.start_date <= now and (not self.end_date or now <= self.end_date)
85
86 def can_comment_on(self):
87 return self.is_open()
88
89
90 class Choice(models.Model):
91 """Model for poll choices"""
92 poll = models.ForeignKey(Poll)
93 choice = models.CharField(max_length=200)
94 votes = models.IntegerField(default=0)
95 voters = models.ManyToManyField(User, blank=True)
96
97 def __unicode__(self):
98 return self.choice