models.py 1.61 KB
Newer Older
christophe siraut's avatar
christophe siraut committed
1 2
import os
from binascii import hexlify
3 4
from django.db import models
from django.utils.translation import ugettext_lazy as _
christophe siraut's avatar
christophe siraut committed
5 6 7 8 9 10 11 12

def _createId():
    # Generate a 4 caracters id
    return hexlify(os.urandom(2))

class Poll(models.Model):
    # Override django id AutoField with randomly generatyed hash
    id = models.CharField(primary_key=True,unique=True,max_length=8,default=_createId)
13 14 15 16 17
    title = models.CharField(_('Title'),max_length=80)
    pub_date = models.DateField(auto_now_add=True)
    upd_date = models.DateField(auto_now=True)
    author = models.CharField(max_length=40)
    description = models.CharField(max_length=300)
christophe siraut's avatar
christophe siraut committed
18 19 20 21 22 23

    def __unicode__(self):
        return self.title

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
24
    choice = models.DateField()
25
    details = models.CharField(max_length=200,blank=True)
26
    votecount = models.IntegerField(default=0,blank=True)
27 28
    class Meta:
        ordering = ['choice']
29 30 31
    def __unicode__(self):
        return str(self.choice) # hacky?

christophe siraut's avatar
christophe siraut committed
32 33
class Bulletin(models.Model):
    poll = models.ForeignKey(Poll,editable=False)
34
    voter = models.CharField("",max_length=40)
christophe siraut's avatar
christophe siraut committed
35 36 37 38 39 40 41

    def __unicode__(self):
        return self.voter

class Vote(models.Model):
    choice = models.ForeignKey(Choice)
    bulletin = models.ForeignKey(Bulletin)
42
    voice = models.BooleanField()
43
    comment = models.CharField(max_length=80,blank=True)
christophe siraut's avatar
christophe siraut committed
44 45 46 47 48 49 50 51 52 53

    def __unicode__(self):
        return self.voice

"""
# First, define the Manager subclass.
class VoteBuletin(models.Manager):
    def get_query_set(self):
        return super(VoteBuletin, self).get_query_set().filter(author='Roald Dahl')
"""