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

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)
14
15
16
17
18
    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
19
    username = models.OneToOneField(User)
christophe siraut's avatar
christophe siraut committed
20

Christophe Siraut's avatar
Christophe Siraut committed
21
m django.contrib.auth.models import User
christophe siraut's avatar
christophe siraut committed
22
23
24
25
26
    def __unicode__(self):
        return self.title

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

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

    def __unicode__(self):
        return self.voter

class Vote(models.Model):
    choice = models.ForeignKey(Choice)
    bulletin = models.ForeignKey(Bulletin)
45
    voice = models.BooleanField()
46
    comment = models.CharField(max_length=80,blank=True)
47
48
    class Meta:
        ordering = ['choice']
christophe siraut's avatar
christophe siraut committed
49
    def __unicode__(self):
Christophe Siraut's avatar
Christophe Siraut committed
50
        return str(self.voice)
christophe siraut's avatar
christophe siraut committed
51
52
53
54
55
56
57

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