-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
39 lines (35 loc) · 1.05 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
ballots = models.IntegerField(default=0)
def total_votes(self):
v = 0
for c in self.choice_set.all():
v += c.votes
return v
def __unicode__(self):
return self.question
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def percentage(self):
if self.poll.ballots == 0:
return 0
return self.votes*100/self.poll.ballots
def __unicode__(self):
return self.choice_text
#TODO: Use these to better-track who's voting
#class Ballot(models.Model):
# poll = models.ForeignKey(Poll)
# timestamp = models.DateTimeField('time voted')
# ip = models.GenericIPAddressField()
# def __unicode__(self):
# return self.ip + " at " + self.timestamp
#
#class Vote(models.Model):
# ballot = models.ForeignKey(Ballot)
# choice = models.ForeignKey(Choice)
# def __unicode__(self):
# return self.ballot + " for " + self.choice