Commit 2b755471 authored by Adrián Ribao's avatar Adrián Ribao
Browse files

cleanup models

parent a64dab43
# -*- coding: utf-8 -*-
from django.contrib.sites.models import Site
from django.db import models
#from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _
import os
import datetime
from django.template import Template, Context
import json
from django.db.models.signals import m2m_changed
from django.dispatch import receiver
import string
import random
from django.core.urlresolvers import reverse
import string
from django.conf import settings
from cartero.conf import CarteroConf
#from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.db import models
from django.template import Template, Context
from django.utils.translation import ugettext_lazy as _
class Contact(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
site = models.ForeignKey(Site, verbose_name=_(u'Site'), default=settings.SITE_ID)
first_name = models.CharField(max_length=150, verbose_name=_(u'First name'), blank=False,
help_text=_(u'First name of the contact.'))
last_name = models.CharField(max_length=150, verbose_name=_(u'Last name'), blank=False,
help_text=_(u'Last name of the contact.'))
email = models.EmailField(max_length=200)
data = models.TextField(blank=True, verbose_name=_(u'JSON Data'),
help_text=_(u'JSON data thas is going into the template as the Context.'))
sent = models.BooleanField(default=False, verbose_name=_(u'Sent'), blank=True)
bounced = models.BooleanField(default=False, verbose_name=_(u'Bounced'), blank=True)
testing = models.BooleanField(default=False, verbose_name=_(u'Testing'), blank=True,
help_text=_(u'If this field is selected, the contact receives the testing emails.'))
site = models.ForeignKey(
Site,
verbose_name=_(u'Site'),
default=settings.SITE_ID
)
first_name = models.CharField(
max_length=150,
verbose_name=_(u'First name'),
blank=False,
help_text=_(u'First name of the contact.')
)
last_name = models.CharField(
max_length=150,
verbose_name=_(u'Last name'),
blank=False,
help_text=_(u'Last name of the contact.')
)
email = models.EmailField(
max_length=200
)
data = models.TextField(
blank=True,
verbose_name=_(u'JSON Data'),
help_text=_(u'JSON data thas is going into the template as '
u'the Context.')
)
sent = models.BooleanField(
default=False,
verbose_name=_(u'Sent'),
blank=True
)
bounced = models.BooleanField(
default=False,
verbose_name=_(u'Bounced'),
blank=True
)
testing = models.BooleanField(
default=False,
verbose_name=_(u'Testing'),
blank=True,
help_text=_(u'If this field is selected, the contact receives the '
u'testing emails.')
)
class Meta:
verbose_name = _(u'Contact')
verbose_name_plural = _(u'Contacts')
def __unicode__(self):
representation = u'{0} {1} {2}'.format(self.email, self.first_name, self.last_name)
representation = u'{0} {1} {2}'.format(self.email, self.first_name,
self.last_name)
if self.testing:
representation += ' (testing)'
return representation
class EmailTemplate(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
site = models.ForeignKey(Site, verbose_name=_(u'Site'))
name = models.CharField(max_length=150, verbose_name=_(u'Name'), blank=False,
help_text=_(u'Name of the template.'))
template = models.FilePathField(path=settings.CARTERO_EMAIL_TEMPLATES, match='\.html$', recursive=False, max_length=255)
context = models.TextField(blank=True, verbose_name=_(u'JSON context'),
help_text=_(u'JSON context pased to the template.'))
html = models.TextField(blank=True, verbose_name=_(u'HTML content'),
help_text=_(u'HTML template'))
plain = models.TextField(blank=True, verbose_name=_(u'Plain content'),
help_text=_(u'HTML template'))
site = models.ForeignKey(
Site,
verbose_name=_(u'Site')
)
name = models.CharField(
max_length=150,
verbose_name=_(u'Name'),
blank=False,
help_text=_(u'Name of the template.')
)
template = models.FilePathField(
path=settings.CARTERO_EMAIL_TEMPLATES,
match='\.html$',
recursive=False,
max_length=255
)
context = models.TextField(
blank=True,
verbose_name=_(u'JSON context'),
help_text=_(u'JSON context pased to the template.')
)
html = models.TextField(
blank=True,
verbose_name=_(u'HTML content'),
help_text=_(u'HTML template')
)
plain = models.TextField(
blank=True,
verbose_name=_(u'Plain content'),
help_text=_(u'HTML template')
)
class Meta:
verbose_name = _(u'Email template')
......@@ -81,6 +129,7 @@ class EmailTemplate(models.Model):
self.html = self.render_html()
super(EmailTemplate, self).save(*args, **kwargs)
class Campaign(models.Model):
"""
Model that stores the main information for the campaign.
......@@ -111,32 +160,70 @@ class Campaign(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
site = models.ForeignKey(Site, verbose_name=_(u'Site'), default=settings.SITE_ID)
name = models.CharField(max_length=150, verbose_name=_(u'Name'), blank=False,
help_text=_(u'Name of the campaign.'))
subject = models.CharField(max_length=150, verbose_name=_(u'Subject'), blank=False,
help_text=_(u'Subject for the email.'))
start = models.DateTimeField(default=datetime.datetime.now(), verbose_name=_(u'Start after'),
help_text=_(u'The date and time when the campaign sould be sent.'))
from_email = models.CharField(max_length=255, verbose_name=_(u'From email'), blank=True,
help_text=_(u'From email that will be shown as sender name. Can be in the form: Text you want <me@email.com>'))
reply_to = models.CharField(max_length=150, verbose_name=_(u'Reply to'), blank=True,
help_text=_(u'Email address used to reply the emails sent in the campaign.'))
contacts = models.ManyToManyField(Contact, verbose_name=_(u'Contacts'), blank=True,
help_text=_(u'Contacts to send the emails.'))
email_template = models.ForeignKey(EmailTemplate, verbose_name=_(u'Email template'))
#ready = models.BooleanField(default=False, verbose_name=_(u'Ready to be sent'), blank=True,
#help_text=_(u'When this is activated, the email is ready to be sent and will be scheduled.'))
#sent = models.BooleanField(default=False, verbose_name=_(u'Sent'), blank=True)
status = models.CharField(max_length=2, choices=CAMPAIGN_STATUS, default=DRAFT)
rate_limit_amount = models.PositiveSmallIntegerField(default=10000, verbose_name=_(u'Rate limit amount'),
help_text=_(u'Rate limit amount'))
rate_limit_interval = models.CharField(max_length=1, choices=RATE_LIMIT_CHOICES, default=RATE_LIMIT_HOURS, verbose_name=_(u'Rate limit interval'))
#slug = models.SlugField(max_length=255, unique=True)
site = models.ForeignKey(
Site,
verbose_name=_(u'Site'),
default=settings.SITE_ID
)
name = models.CharField(
max_length=150,
verbose_name=_(u'Name'),
blank=False,
help_text=_(u'Name of the campaign.')
)
subject = models.CharField(
max_length=150,
verbose_name=_(u'Subject'),
blank=False,
help_text=_(u'Subject for the email.')
)
start = models.DateTimeField(
default=datetime.datetime.now(),
verbose_name=_(u'Start after'),
help_text=_(u'The date and time when the campaign sould be sent.')
)
from_email = models.CharField(
max_length=255,
verbose_name=_(u'From email'),
blank=True,
help_text=_(u'From email that will be shown as sender name. Can be '
u'in the form: Text you want <me@email.com>')
)
reply_to = models.CharField(
max_length=150,
verbose_name=_(u'Reply to'),
blank=True,
help_text=_(u'Email address used to reply the emails sent '
u'in the campaign.')
)
contacts = models.ManyToManyField(
Contact,
verbose_name=_(u'Contacts'),
blank=True,
help_text=_(u'Contacts to send the emails.')
)
email_template = models.ForeignKey(
EmailTemplate,
verbose_name=_(u'Email template')
)
status = models.CharField(
max_length=2,
choices=CAMPAIGN_STATUS,
default=DRAFT
)
rate_limit_amount = models.PositiveSmallIntegerField(
default=10000,
verbose_name=_(u'Rate limit amount'),
help_text=_(u'Rate limit amount')
)
rate_limit_interval = models.CharField(
max_length=1,
choices=RATE_LIMIT_CHOICES,
default=RATE_LIMIT_HOURS,
verbose_name=_(u'Rate limit interval')
)
class Meta:
#ordering = ['order', ]
verbose_name = _(u'Campaign')
verbose_name_plural = _(u'Campaigns')
......@@ -145,43 +232,19 @@ class Campaign(models.Model):
@property
def rate_limit(self):
return '{0}/{1}'.format(self.rate_limit_amount, self.rate_limit_interval)
return '{0}/{1}'.format(
self.rate_limit_amount, self.rate_limit_interval)
def add_contacts(self, contacts):
"""
Adds the contacts to the campaign, and check if they are not excluded.
"""
self.excluded_emails = ExcludedEmail.objects.all().values_list('email', flat=True)
self.excluded_emails = ExcludedEmail.objects.all() \
.values_list('email', flat=True)
for contact in contacts:
if contact.email not in self.excluded_emails:
self.contacts.add(contact)
#def save(self, *args, **kwargs):
#self.slug = self._slugify()
#super(Model, self).save(*args, **kwargs)
#@models.permalink
#def get_absolute_url(self):
#return ('view_name', [self.slug, ])
#url = reverse('wjob', args=[self.slug, ])
#return u'%s#%s' % (url, self.id, )
#def _slugify(self):
#string = u'%s' % (self.id, )
#slug = slugify(string)
#return slug
#@receiver(m2m_changed, sender=Campaign.contacts.through, dispatch_uid='contacts_changed')
#def check_contacts_added(sender, instance, action, reverse, model, pk_set, using, *args, **kwargs):
#"""
#Check that the contacts are not in the exclusion list.
#"""
#if action == 'pre_add':
#excluded_emails = ExcludedEmail.objects.all().values_list('email', flat=True)
#for pk in pk_set:
#email = model.objects.get(pk=pk).email
#print pk
class ExcludedEmail(models.Model):
HARD_BOUNCE = 'HB'
......@@ -197,11 +260,22 @@ class ExcludedEmail(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
email = models.EmailField(max_length=200, unique=True)
reason = models.CharField(max_length=2, choices=REASONS, verbose_name=_(u'Reason'), blank=False,
help_text=_(u'Reason to be excluded.'))
comments = models.TextField(blank=True, verbose_name=_(u'Comments'),
help_text=_(u'Store here any comments about the exclusion.'))
email = models.EmailField(
max_length=200,
unique=True
)
reason = models.CharField(
max_length=2,
choices=REASONS,
verbose_name=_(u'Reason'),
blank=False,
help_text=_(u'Reason to be excluded.')
)
comments = models.TextField(
blank=True,
verbose_name=_(u'Comments'),
help_text=_(u'Store here any comments about the exclusion.')
)
class Meta:
verbose_name = _(u'Excluded Email')
......@@ -217,8 +291,17 @@ class ShortURL(models.Model):
"""
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
url = models.CharField(max_length=255, verbose_name=_(u'URL'), db_index=True)
code = models.CharField(max_length=75, verbose_name=_(u'Code'), db_index=True, unique=True)
url = models.CharField(
max_length=255,
verbose_name=_(u'URL'),
db_index=True
)
code = models.CharField(
max_length=75,
verbose_name=_(u'Code'),
db_index=True,
unique=True
)
class Meta:
verbose_name = _(u'Short URL')
......@@ -236,19 +319,24 @@ class ShortURL(models.Model):
self.code = self._generate_code()
super(ShortURL, self).save(*args, **kwargs)
class LinkClick(models.Model):
"""
Monitor link clicks.
"""
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
url = models.ForeignKey(ShortURL, verbose_name=_(u'url'))
#url = models.CharField(max_length=255, verbose_name=_(u'URL'), db_index=True)
#short_url = models.CharField(max_length=75, verbose_name=_(u'Short url'), db_index=True)
ip_address = models.GenericIPAddressField(verbose_name=_(u'IP Address'), null=True, db_index=True)
url = models.ForeignKey(
ShortURL,
verbose_name=_(u'url')
)
ip_address = models.GenericIPAddressField(
verbose_name=_(u'IP Address'),
null=True,
db_index=True
)
campaign = models.ForeignKey(Campaign, verbose_name=_(u'Campaign'))
class Meta:
verbose_name = _(u'Link Click')
verbose_name_plural = _(u'Link Clicks')
......@@ -256,17 +344,17 @@ class LinkClick(models.Model):
def __unicode__(self):
return u'{0} {1}'.format(self.url, self.campaign)
#def save(self, force_insert=False, force_update=False):
#@models.permalink
#def get_absolute_url(self):
#return ('view_or_url_name' )
class MailTracks(models.Model):
created = models.DateTimeField(auto_now_add=True)
campaign = models.ForeignKey(Campaign, verbose_name=_(u'Campaign'))
contact = models.ForeignKey(Contact, verbose_name=_(u'Contact'))
#code = models.CharField(max_length=75, verbose_name=_(u'Code'), db_index=True)
campaign = models.ForeignKey(
Campaign,
verbose_name=_(u'Campaign')
)
contact = models.ForeignKey(
Contact,
verbose_name=_(u'Contact')
)
class Meta:
verbose_name = _(u'Mail Track')
......@@ -275,15 +363,26 @@ class MailTracks(models.Model):
def __unicode__(self):
return u'{0} {1}'.format(self.campaign, self.contact)
class OpenEmail(models.Model):
"""
Open emails
"""
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
ip_address = models.GenericIPAddressField(verbose_name=_(u'IP Address'), null=True, db_index=True)
campaign = models.ForeignKey(Campaign, verbose_name=_(u'Campaign'))
contact = models.ForeignKey(Contact, verbose_name=_(u'Contact'))
ip_address = models.GenericIPAddressField(
verbose_name=_(u'IP Address'),
null=True,
db_index=True
)
campaign = models.ForeignKey(
Campaign,
verbose_name=_(u'Campaign')
)
contact = models.ForeignKey(
Contact,
verbose_name=_(u'Contact')
)
class Meta:
verbose_name = _(u'Open Mail')
......@@ -292,21 +391,30 @@ class OpenEmail(models.Model):
def __unicode__(self):
return u'{0} {1}'.format(self.campaign, self.contact)
#def save(self, force_insert=False, force_update=False):
#@models.permalink
#def get_absolute_url(self):
#return ('view_or_url_name' )
class Image(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
site = models.ForeignKey(Site, verbose_name=_(u'Site'), default=settings.SITE_ID)
name = models.CharField(max_length=150, verbose_name=_(u'Name'), blank=False,
help_text=_(u'Name of the campaign.'))
slug = models.SlugField(max_length=255, unique=True)
file = models.ImageField(upload_to='cartero/images/', verbose_name=_(u'Image File'),
help_text=_(u'Image file'))
site = models.ForeignKey(
Site,
verbose_name=_(u'Site'),
default=settings.SITE_ID
)
name = models.CharField(
max_length=150,
verbose_name=_(u'Name'),
blank=False,
help_text=_(u'Name of the campaign.')
)
slug = models.SlugField(
max_length=255,
unique=True
)
file = models.ImageField(
upload_to='cartero/images/',
verbose_name=_(u'Image File'),
help_text=_(u'Image file')
)
class Meta:
verbose_name = _(u'Image')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment