models.py 1.93 KB
Newer Older
christophe siraut's avatar
christophe siraut committed
1
import os
2
3
from django.db import models
from django.utils.translation import ugettext_lazy as _
christophe siraut's avatar
christophe siraut committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from binascii import hexlify

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)
    title = models.CharField('Question',max_length=50)
    pub_date = models.DateField('date published',auto_now_add=True)
    upd_date = models.DateField('date updated',auto_now=True)
    author = models.CharField(max_length=20)
    description = models.CharField(max_length=200)
    #public = models.BooleanField()

    def __unicode__(self):
        return self.title

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
26
27
28
29
30
31
32
33
34
35
36
    choice = models.DateField()
    details = models.CharField(max_length=120,blank=True)
    votecount = models.IntegerField(default=0,blank=True)

    def __unicode__(self):
        return str(self.choice) # hacky?

class NodateChoice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=80)
    details = models.CharField(max_length=120,blank=True)
christophe siraut's avatar
christophe siraut committed
37
38
39
40
41
42
43
    votecount = models.IntegerField(default=0,blank=True)

    def __unicode__(self):
        return self.choice

class Bulletin(models.Model):
    poll = models.ForeignKey(Poll,editable=False)
44
    voter = models.CharField(_('Your name'),max_length=40)
christophe siraut's avatar
christophe siraut committed
45
46
47
48
49
50
51
52

    def __unicode__(self):
        return self.voter

class Vote(models.Model):
    choice = models.ForeignKey(Choice)
    bulletin = models.ForeignKey(Bulletin)
    voice = models.BooleanField(blank=True)
53
    comment = models.CharField(max_length=80,blank=True)
christophe siraut's avatar
christophe siraut committed
54
55
56
57
58
59
60
61
62
63
64

    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')
"""