models.py 1.94 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
from django.core.urlresolvers import reverse
4 5
from django.db import models
from django.utils.translation import ugettext_lazy as _
Christophe Siraut's avatar
Christophe Siraut committed
6
import datetime
7 8
import string
import random
christophe siraut's avatar
christophe siraut committed
9

Christophe Siraut's avatar
Christophe Siraut committed
10 11 12 13 14 15 16

def createId(length):
    firstchar = string.ascii_letters
    c = string.digits + string.ascii_letters
    lastchars = ''.join([random.choice(c) for dummy in xrange(0, length - 1)])
    return random.choice(firstchar) + lastchars

christophe siraut's avatar
christophe siraut committed
17 18

class Poll(models.Model):
Christophe Siraut's avatar
Christophe Siraut committed
19 20 21 22 23
    # Override django id AutoField with randomly generated hash
    id = models.CharField(
        primary_key=True,
        max_length=8,
        default=createId(5))
Christophe Siraut's avatar
Cleanup  
Christophe Siraut committed
24
    title = models.CharField(_('Title'), max_length=80)
Christophe Siraut's avatar
Christophe Siraut committed
25
    pub_date = models.DateField(default=datetime.datetime.now)
26 27
    upd_date = models.DateField(auto_now=True)
    description = models.CharField(max_length=300)
28
    user = models.ForeignKey(User, null=True)
christophe siraut's avatar
christophe siraut committed
29 30 31 32

    def __unicode__(self):
        return self.title

Christophe Siraut's avatar
Christophe Siraut committed
33 34
    @property
    def link(self):
Christophe Siraut's avatar
Christophe Siraut committed
35 36 37
        return 'http://%s%s' % (
            Site.objects.get_current(),
            reverse('meetingpoll', args=(self.id,)))
Christophe Siraut's avatar
Christophe Siraut committed
38

Christophe Siraut's avatar
Christophe Siraut committed
39

christophe siraut's avatar
christophe siraut committed
40 41
class Choice(models.Model):
    poll = models.ForeignKey(Poll)
42
    choice = models.DateTimeField()
Christophe Siraut's avatar
Cleanup  
Christophe Siraut committed
43 44
    details = models.CharField(max_length=200, blank=True)
    votecount = models.IntegerField(default=0, blank=True)
Christophe Siraut's avatar
Christophe Siraut committed
45

46 47
    class Meta:
        ordering = ['choice']
Christophe Siraut's avatar
Christophe Siraut committed
48

49
    def __unicode__(self):
Christophe Siraut's avatar
Christophe Siraut committed
50 51
        return str(self.choice)

52

christophe siraut's avatar
christophe siraut committed
53
class Bulletin(models.Model):
Christophe Siraut's avatar
Cleanup  
Christophe Siraut committed
54 55
    poll = models.ForeignKey(Poll, editable=False)
    voter = models.CharField("", max_length=40)
christophe siraut's avatar
christophe siraut committed
56 57 58 59

    def __unicode__(self):
        return self.voter

Christophe Siraut's avatar
Christophe Siraut committed
60

christophe siraut's avatar
christophe siraut committed
61 62 63
class Vote(models.Model):
    choice = models.ForeignKey(Choice)
    bulletin = models.ForeignKey(Bulletin)
64
    voice = models.BooleanField()
Christophe Siraut's avatar
Cleanup  
Christophe Siraut committed
65
    comment = models.CharField(max_length=80, blank=True)
Christophe Siraut's avatar
Christophe Siraut committed
66

67 68
    class Meta:
        ordering = ['choice']
Christophe Siraut's avatar
Christophe Siraut committed
69

christophe siraut's avatar
christophe siraut committed
70
    def __unicode__(self):
Christophe Siraut's avatar
Christophe Siraut committed
71
        return str(self.voice)