annotate gpp/polls/models.py @ 172:0fa78ef80356

Implementing #55 - Add function to view a users posts from their profile.
author Brian Neal <bgneal@gmail.com>
date Sun, 28 Feb 2010 22:20:15 +0000
parents dbd703f7d63a
children cb121a3abf46
rev   line source
gremmie@1 1 '''
gremmie@1 2 Models for the Polls application.
gremmie@1 3 '''
gremmie@1 4
gremmie@1 5 import datetime
gremmie@1 6 from django.db import models
gremmie@1 7 from django.db.models import Q
gremmie@1 8
gremmie@1 9
gremmie@1 10 class PollManager(models.Manager):
gremmie@1 11 """Manager for the Poll model"""
gremmie@1 12
gremmie@1 13 def get_current_polls(self):
gremmie@1 14 now = datetime.datetime.now()
gremmie@1 15 return self.filter(
gremmie@1 16 Q(is_enabled=True),
gremmie@1 17 Q(start_date__lte=now),
gremmie@1 18 Q(end_date__isnull=True) | Q(end_date__gte=now))
gremmie@1 19
gremmie@1 20 def get_old_polls(self):
gremmie@1 21 now = datetime.datetime.now()
gremmie@1 22 return self.filter(
gremmie@1 23 Q(is_enabled=True),
gremmie@1 24 Q(end_date__isnull=False),
gremmie@1 25 Q(end_date__lt=now))
gremmie@1 26
gremmie@1 27
gremmie@1 28 class Poll(models.Model):
gremmie@1 29 """Model to represent polls"""
gremmie@1 30 start_date = models.DateTimeField(db_index=True,
gremmie@1 31 help_text='Date/time the poll will be eligible for voting.',)
gremmie@1 32 end_date = models.DateTimeField(blank=True, null=True, db_index=True,
gremmie@1 33 help_text='Date/time the poll will be ineligible for voting. '\
gremmie@1 34 'Leave blank for an open ended poll.')
gremmie@1 35 is_enabled = models.BooleanField(default=True, db_index=True,
gremmie@1 36 help_text='Check to allow the poll to be viewed on the site.')
gremmie@1 37 question = models.CharField(max_length=200)
gremmie@1 38
gremmie@1 39 objects = PollManager()
gremmie@1 40
gremmie@1 41 def __unicode__(self):
gremmie@1 42 return self.question
gremmie@1 43
gremmie@1 44 class Meta:
gremmie@1 45 ordering = ('-start_date', )
gremmie@1 46 get_latest_by = 'start_date'
gremmie@1 47
gremmie@1 48 @models.permalink
gremmie@1 49 def get_absolute_url(self):
gremmie@1 50 return ('polls.views.poll_detail', [str(self.id)])
gremmie@1 51
gremmie@1 52 def results(self):
gremmie@1 53 """
gremmie@1 54 Returns a tuple; element 0 is the total votes, element 1 is a list of
gremmie@1 55 {choice, votes, pct}
gremmie@1 56 """
gremmie@1 57 choices = []
gremmie@1 58 total_votes = 0
gremmie@1 59 for choice in self.choice_set.all():
gremmie@1 60 total_votes += choice.votes
gremmie@1 61 choices.append({'choice': choice.choice, 'votes': choice.votes, 'pct': 0.0})
gremmie@1 62
gremmie@1 63 if total_votes > 0:
gremmie@1 64 for choice in choices:
gremmie@1 65 choice['pct'] = float(choice['votes']) / total_votes * 100.0
gremmie@1 66
gremmie@1 67 return (total_votes, choices)
gremmie@1 68
gremmie@1 69 def is_open(self):
gremmie@1 70 now = datetime.datetime.now()
gremmie@1 71 return self.start_date <= now and (not self.end_date or now <= self.end_date)
gremmie@1 72
gremmie@1 73 def can_comment_on(self):
gremmie@1 74 return self.is_open()
gremmie@1 75
gremmie@1 76
gremmie@1 77 class Choice(models.Model):
gremmie@1 78 """Model for poll choices"""
gremmie@1 79 poll = models.ForeignKey(Poll)
gremmie@1 80 choice = models.CharField(max_length = 200)
gremmie@1 81 votes = models.IntegerField(default = 0)
gremmie@1 82
gremmie@1 83 def __unicode__(self):
gremmie@1 84 return self.choice