models.py 2.45 KB
Newer Older
Christophe Siraut's avatar
Christophe Siraut committed
1
from django.db import models
2
from django.contrib.auth.models import User
Christophe Siraut's avatar
Christophe Siraut committed
3
from django.contrib.sites.models import Site
Christophe Siraut's avatar
Christophe Siraut committed
4 5
from django.contrib.auth.signals import user_logged_in
from django.core.exceptions import ObjectDoesNotExist
6
from django.core.urlresolvers import reverse
7
from django.utils.translation import ugettext_lazy as _
Christophe Siraut's avatar
Christophe Siraut committed
8
import datetime
9 10
import string
import random
christophe siraut's avatar
christophe siraut committed
11

Christophe Siraut's avatar
Christophe Siraut committed
12 13 14 15 16 17 18

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
19

Christophe Siraut's avatar
Christophe Siraut committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33
def login_handler(user, **kwargs):
    """Verify user profile exists"""
    try:
        profile = UserProfile.objects.get(user=user)
    except ObjectDoesNotExist:
        profile = UserProfile(user=user)
        profile.save()
    return

user_logged_in.connect(login_handler)


class UserProfile(models.Model):
    user = models.OneToOneField(User)
34
    email_notifications = models.BooleanField(default=False)
Christophe Siraut's avatar
Christophe Siraut committed
35

36

christophe siraut's avatar
christophe siraut committed
37
class Poll(models.Model):
Christophe Siraut's avatar
Christophe Siraut committed
38 39 40 41 42
    # Override django id AutoField with randomly generated hash
    id = models.CharField(
        primary_key=True,
        max_length=8,
        default=createId(5))
Christophe Siraut's avatar
Christophe Siraut committed
43
    title = models.CharField(_('Title'), max_length=80)
Christophe Siraut's avatar
Christophe Siraut committed
44
    pub_date = models.DateField(default=datetime.datetime.now)
45 46
    upd_date = models.DateField(auto_now=True)
    description = models.CharField(max_length=300)
47
    user = models.ForeignKey(User, null=True)
christophe siraut's avatar
christophe siraut committed
48 49 50 51

    def __unicode__(self):
        return self.title

Christophe Siraut's avatar
Christophe Siraut committed
52 53
    @property
    def link(self):
54 55 56
        return 'http://%s%s' % (
            Site.objects.get_current(),
            reverse('meetingpoll', args=(self.id,)))
Christophe Siraut's avatar
Christophe Siraut committed
57

Christophe Siraut's avatar
Christophe Siraut committed
58

christophe siraut's avatar
christophe siraut committed
59 60
class Choice(models.Model):
    poll = models.ForeignKey(Poll)
61
    choice = models.DateTimeField()
Christophe Siraut's avatar
Christophe Siraut committed
62 63
    details = models.CharField(max_length=200, blank=True)
    votecount = models.IntegerField(default=0, blank=True)
Christophe Siraut's avatar
Christophe Siraut committed
64

65 66
    class Meta:
        ordering = ['choice']
Christophe Siraut's avatar
Christophe Siraut committed
67

68
    def __unicode__(self):
Christophe Siraut's avatar
Christophe Siraut committed
69 70
        return str(self.choice)

71

christophe siraut's avatar
christophe siraut committed
72
class Bulletin(models.Model):
Christophe Siraut's avatar
Christophe Siraut committed
73 74
    poll = models.ForeignKey(Poll, editable=False)
    voter = models.CharField("", max_length=40)
christophe siraut's avatar
christophe siraut committed
75 76 77 78

    def __unicode__(self):
        return self.voter

Christophe Siraut's avatar
Christophe Siraut committed
79

christophe siraut's avatar
christophe siraut committed
80 81 82
class Vote(models.Model):
    choice = models.ForeignKey(Choice)
    bulletin = models.ForeignKey(Bulletin)
83
    voice = models.BooleanField()
Christophe Siraut's avatar
Christophe Siraut committed
84
    comment = models.CharField(max_length=80, blank=True)
Christophe Siraut's avatar
Christophe Siraut committed
85

86 87
    class Meta:
        ordering = ['choice']
Christophe Siraut's avatar
Christophe Siraut committed
88

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