models.py 1.03 KB
Newer Older
okhin's avatar
okhin committed
1 2 3
from django.db import models
from django.utils.translation import ugettext_lazy as _

okhin's avatar
okhin committed
4 5
from picampaign.campaign.models import Campaign

okhin's avatar
okhin committed
6 7 8 9 10 11 12
class Importer(models.Model):
    """Importer model. Used to populate campaign with contacts"""
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=64)
    data_format = models.CharField(max_length=4, choices=[('JSON', 'json'), ('CSV', 'csv')])
    file = models.FileField(upload_to='imports/', blank=True, null=True)
    url = models.URLField(blank=True, null=True)
okhin's avatar
okhin committed
13
    campaign = models.ForeignKey(Campaign, on_delete=models.SET_NULL, null=True)
okhin's avatar
okhin committed
14 15

    def __str__(self):
16 17
        return _('%{name}: %{format} %{type} importer' % {'format': self.data_format,
                                                'type': self.kind(),
okhin's avatar
okhin committed
18
                                                'name': self.name})
19 20 21 22 23 24 25 26

    def kind(self):
        if self.url is None and self.file is None:
            return None
        if self.url is None:
            return 'file'
        else:
            return 'url'