Commit 9317f457 authored by Adrián Ribao's avatar Adrián Ribao
Browse files

use contact lists in campaigns

parent 03bbb695
# -*- coding: utf-8 -*-
from django.conf.urls import patterns
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from cartero.models import Contact, EmailTemplate, Campaign, ExcludedEmail,\
ShortURL, LinkClick, OpenEmail, Image
from django.conf.urls import patterns
from cartero.views import CreateTemplateView, UpdateTemplateView
from cartero.utils.sending import CampaignDeliver
from .models import Contact, EmailTemplate, Campaign, ExcludedEmail,\
ShortURL, LinkClick, OpenEmail, Image, ContactList, ContactInList
from .utils.sending import CampaignDeliver
from .views import CreateTemplateView, UpdateTemplateView
class ContactAdmin(admin.ModelAdmin):
......@@ -43,6 +44,30 @@ class ContactAdmin(admin.ModelAdmin):
admin.site.register(Contact, ContactAdmin)
@admin.register(ContactList)
class ContactListAdmin(admin.ModelAdmin):
list_display = [
'name',
]
search_fields = [
'name',
]
@admin.register(ContactInList)
class ContactInListAdmin(admin.ModelAdmin):
list_display = [
'contactlist',
'contact',
]
search_fields = [
'contactlist__name',
'contact__first_name',
'contact__last_name',
'contact__email',
]
class EmailTemplateAdmin(admin.ModelAdmin):
date_hierarchy = 'created'
list_display = [
......@@ -75,47 +100,40 @@ admin.site.register(EmailTemplate, EmailTemplateAdmin)
class CampaignAdmin(admin.ModelAdmin):
date_hierarchy = 'start'
def num_contacts(self, obj):
return obj.contacts.all().count()
num_contacts.short_description = '# Contacts'
list_display = [
'name',
'start',
'subject',
'num_contacts',
'reply_to',
'site',
'status',
'rate_limit',
]
'name',
'start',
'subject',
'contact_list',
'reply_to',
'site',
'status',
'rate_limit',
]
list_filter = [
'status',
]
'status',
]
search_fields = [
'name',
'subject',
'reply_to',
]
'name',
'subject',
'reply_to',
]
actions = [
'send_test',
'send',
'cancel_send',
]
filter_horizontal = [
'contacts',
]
'send_test',
'send',
'cancel_send',
]
fields = (
'site',
'name',
'subject',
'start',
('reply_to', 'from_email'),
'contacts',
'email_template',
'status',
('rate_limit_amount', 'rate_limit_interval'),
)
'site',
'name',
'subject',
'start',
('reply_to', 'from_email'),
'contact_list',
'email_template',
'status',
('rate_limit_amount', 'rate_limit_interval'),
)
def send_test(self, request, queryset):
for campaign in queryset:
......
# -*- coding: utf-8 -*-
# Generated by Django 1.9.9 on 2016-09-07 13:33
from __future__ import unicode_literals
import datetime
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('cartero', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='ContactInList',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('modified', models.DateTimeField(auto_now=True)),
('contact', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cartero.Contact')),
],
options={
'verbose_name': 'Contact in list',
'verbose_name_plural': 'Contacts in lists',
},
),
migrations.CreateModel(
name='ContactList',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('modified', models.DateTimeField(auto_now=True)),
('name', models.CharField(help_text='Name of the list', max_length=150, verbose_name='Name')),
('contacts', models.ManyToManyField(blank=True, help_text='Contacts to send the emails.', through='cartero.ContactInList', to='cartero.Contact', verbose_name='Contacts')),
],
options={
'verbose_name': 'Contact list',
'verbose_name_plural': 'Contact lists',
},
),
migrations.RemoveField(
model_name='campaign',
name='contacts',
),
migrations.AlterField(
model_name='campaign',
name='rate_limit_interval',
field=models.CharField(choices=[(b's', 'per second')], default=b'h', max_length=1, verbose_name='Rate limit interval'),
),
migrations.AlterField(
model_name='campaign',
name='start',
field=models.DateTimeField(default=datetime.datetime(2016, 9, 7, 15, 33, 17, 810979), help_text='The date and time when the campaign sould be sent.', verbose_name='Start after'),
),
migrations.AlterField(
model_name='emailtemplate',
name='template',
field=models.FilePathField(match=b'\\.html$', max_length=255, path=b'/project/audiomol/templates/mails/campaigns'),
),
migrations.AddField(
model_name='contactinlist',
name='contactlist',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cartero.ContactList'),
),
migrations.AddField(
model_name='campaign',
name='contact_list',
field=models.ForeignKey(help_text='List that contains the contacts of the newsletter.', null=True, on_delete=django.db.models.deletion.CASCADE, to='cartero.ContactList', verbose_name='Contacts'),
),
]
......@@ -72,6 +72,63 @@ class Contact(models.Model):
return representation
class ContactList(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
name = models.CharField(
max_length=150,
verbose_name=_(u'Name'),
blank=False,
help_text=_(u'Name of the list')
)
contacts = models.ManyToManyField(
Contact,
blank=True,
verbose_name=_(u'Contacts'),
help_text=_(u'Contacts to send the emails.'),
through='ContactInList'
)
class Meta:
verbose_name = _(u'Contact list')
verbose_name_plural = _(u'Contact lists')
def __unicode__(self):
return u'{0} ({1})'.format(self.name, self.contacts.count())
def add_contacts(self, contacts):
"""
Adds the contacts to the list, and check if they are not excluded.
"""
excluded_emails = ExcludedEmail.objects.all() \
.values_list('email', flat=True)
for contact in contacts:
if contact.email not in excluded_emails:
ContactInList.objects.get_or_create(
contact=contact,
contactlist=self
)
class ContactInList(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
contact = models.ForeignKey(Contact)
contactlist = models.ForeignKey(ContactList)
class Meta:
verbose_name = _(u'Contact in list')
verbose_name_plural = _(u'Contacts in lists')
def __unicode__(self):
return u'{0} {1} <{2}> -> {3}'.format(
self.contact.first_name,
self.contact.last_name,
self.contact.email,
self.contactlist.name,
)
class EmailTemplate(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
......@@ -198,11 +255,12 @@ class Campaign(models.Model):
help_text=_(u'Email address used to reply the emails sent '
u'in the campaign.')
)
contacts = models.ManyToManyField(
Contact,
contact_list = models.ForeignKey(
ContactList,
verbose_name=_(u'Contacts'),
blank=True,
help_text=_(u'Contacts to send the emails.')
blank=False,
null=True,
help_text=_(u'List that contains the contacts of the newsletter.')
)
email_template = models.ForeignKey(
EmailTemplate,
......@@ -248,16 +306,6 @@ class Campaign(models.Model):
return (self.rate_limit_amount / factor)
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)
for contact in contacts:
if contact.email not in self.excluded_emails:
self.contacts.add(contact)
class ExcludedEmail(models.Model):
HARD_BOUNCE = 'HB'
......
......@@ -126,7 +126,7 @@ class CampaignDeliver(object):
body=text_content,
to=[contact.email, ],
from_email=from_email,
headers = {
headers={
'Reply-To': reply_to
}
)
......@@ -135,7 +135,7 @@ class CampaignDeliver(object):
return msg
def get_contacts(self, testing=False):
contacts = self.campaign.contacts.all()
contacts = self.contact_list.contacts.all()
if testing:
contacts = contacts.filter(testing=True)
return contacts
......
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