models.py 1.88 KB
Newer Older
Aymeric Barantal's avatar
Aymeric Barantal committed
1
from django.db import models
2 3 4
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

5 6
from picampaign.organization.models import Organization
from picampaign.contact.models import Contact
Aymeric Barantal's avatar
Aymeric Barantal committed
7 8


9
LANGUAGES = map(lambda k: (k[0], _(k[1])), settings.LANGUAGES)
Aymeric Barantal's avatar
Aymeric Barantal committed
10 11 12 13 14 15


class Campaign(models.Model):
    """Campaign model, describe what have to be achieved"""
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=255)
16
    description = models.TextField(null=True)
17
    organization = models.ForeignKey(Organization, related_name='campaigns')
18 19
    start_date = models.DateField()
    end_date = models.DateField()
20 21 22
    default_lang = models.CharField(max_length=5,
                                    choices=LANGUAGES,
                                    verbose_name=_('language'))
Aymeric Barantal's avatar
Aymeric Barantal committed
23

24
    def __str__(self):
25
        return self.title.decode('utf-8')
26

Aymeric Barantal's avatar
Aymeric Barantal committed
27 28

class Argumentary(models.Model):
29
    """Argumentary in a given language for a campaign"""
Aymeric Barantal's avatar
Aymeric Barantal committed
30
    id = models.AutoField(primary_key=True)
Aymeric Barantal's avatar
Aymeric Barantal committed
31
    campaign = models.ForeignKey(Campaign)
32 33 34
    lang = models.CharField(max_length=5,
                            choices=LANGUAGES,
                            verbose_name=_('language'))
Aymeric Barantal's avatar
Aymeric Barantal committed
35
    text = models.TextField(null=True)
Aymeric Barantal's avatar
Aymeric Barantal committed
36

37
    def __str__(self):
Aymeric Barantal's avatar
Aymeric Barantal committed
38 39
        args = {'lang': self.lang, 'title': self.campaign.title}
        return _('Argumentary in %(lang)s for %(title)s') % args
40

Aymeric Barantal's avatar
Aymeric Barantal committed
41 42 43 44

class CampaignContact(models.Model):
    """List contact related to a campaign with a given weight"""
    id = models.AutoField(primary_key=True)
Aymeric Barantal's avatar
Aymeric Barantal committed
45 46
    campaign = models.ForeignKey(Campaign)
    contact = models.ForeignKey(Contact)
Aymeric Barantal's avatar
Aymeric Barantal committed
47
    weight = models.IntegerField(default=0)
Aymeric Barantal's avatar
Aymeric Barantal committed
48

49
    def __str__(self):
Aymeric Barantal's avatar
Aymeric Barantal committed
50 51
        args = {'contact': self.contact, 'title': self.campaign.title}
        return _('Contact %(contact)s on %(title)s') % args
52 53 54

    def all_groups(self):
        return [x.name for x in self.contact.groups.all()]