models.py 1.67 KB
Newer Older
1
from django.contrib.auth.models import User
2
from django.contrib.sites.models import Site
chris's avatar
chris committed
3 4
import os
from binascii import hexlify
5 6
from django.db import models
from django.utils.translation import ugettext_lazy as _
chris's avatar
chris committed
7 8

def _createId():
9
    # Random 4 caracters string
chris's avatar
chris committed
10 11 12 13
    return hexlify(os.urandom(2))

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

    def __unicode__(self):
        return self.title

24 25 26 27
    @property
    def link(self):
        return 'http://%s/%s/' % (Site.objects.get_current(), self.id)

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

chris's avatar
chris committed
38
class Bulletin(models.Model):
Christophe Siraut's avatar
Christophe Siraut committed
39 40
    poll = models.ForeignKey(Poll, editable=False)
    voter = models.CharField("", max_length=40)
chris's avatar
chris committed
41 42 43 44 45 46 47

    def __unicode__(self):
        return self.voter

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