models.py 1.73 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
from django.contrib.sites.models import Site
christophe siraut's avatar
christophe siraut committed
3 4
import os
from binascii import hexlify
5 6
from django.db import models
from django.utils.translation import ugettext_lazy as _
christophe siraut's avatar
christophe siraut committed
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
Christophe Siraut's avatar
Cleanup  
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 19
    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
Cleanup  
Christophe Siraut committed
20
    user = models.ForeignKey(User, blank=True, null=True)
christophe siraut's avatar
christophe siraut committed
21 22 23 24

    def __unicode__(self):
        return self.title

Christophe Siraut's avatar
Christophe Siraut committed
25 26 27 28
    @property
    def link(self):
        return 'http://%s/%s/' % (Site.objects.get_current(), self.id)

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

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

    def __unicode__(self):
        return self.voter

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