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

imporved tasks

parent f7c4a399
......@@ -33,9 +33,21 @@ class ContactAdmin(admin.ModelAdmin):
'last_name',
'email',
]
actions = [
'mark_unsent',
'mark_sent',
]
#inlines = [
#]
#prepopulated_fields = {"slug": ("name", )}
def mark_unsent(self, request, queryset):
queryset.update(sent=False)
mark_unsent.short_description = _(u'Mark as unsent')
def mark_sent(self, request, queryset):
queryset.update(sent=True)
mark_sent.short_description = _(u'Mark as sent')
admin.site.register(Contact, ContactAdmin)
class EmailTemplateAdmin(admin.ModelAdmin):
......@@ -70,13 +82,11 @@ class CampaignAdmin(admin.ModelAdmin):
'subject',
'reply_to',
'site',
'ready',
'sent',
'status',
'rate_limit',
]
list_filter = [
'ready',
'sent',
'status',
]
search_fields = [
'name',
......@@ -102,8 +112,7 @@ class CampaignAdmin(admin.ModelAdmin):
('reply_to', 'from_email'),
'contacts',
'email_template',
'ready',
'sent',
'status',
('rate_limit_amount', 'rate_limit_interval'),
)
......@@ -114,11 +123,11 @@ class CampaignAdmin(admin.ModelAdmin):
send_test.short_description = _(u'Send test')
def send(self, request, queryset):
queryset.update(ready=True)
send.short_description = _(u'Mark campaign as ready to send')
queryset.update(status=Campaign.READY_TO_BE_SENT)
send.short_description = _(u'Mark campaign as ready to be sent')
def cancel_send(self, request, queryset):
queryset.update(ready=False)
queryset.update(status=Campaign.CANCELLED)
cancel_send.short_description = _(u'Cancel the campaign')
admin.site.register(Campaign, CampaignAdmin)
......
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Deleting field 'Campaign.ready'
db.delete_column('cartero_campaign', 'ready')
# Deleting field 'Campaign.sent'
db.delete_column('cartero_campaign', 'sent')
# Adding field 'Campaign.status'
db.add_column('cartero_campaign', 'status',
self.gf('django.db.models.fields.CharField')(default='dr', max_length=2),
keep_default=False)
def backwards(self, orm):
# Adding field 'Campaign.ready'
db.add_column('cartero_campaign', 'ready',
self.gf('django.db.models.fields.BooleanField')(default=False),
keep_default=False)
# Adding field 'Campaign.sent'
db.add_column('cartero_campaign', 'sent',
self.gf('django.db.models.fields.BooleanField')(default=False),
keep_default=False)
# Deleting field 'Campaign.status'
db.delete_column('cartero_campaign', 'status')
models = {
'cartero.campaign': {
'Meta': {'object_name': 'Campaign'},
'contacts': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['cartero.Contact']", 'symmetrical': 'False', 'blank': 'True'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'email_template': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cartero.EmailTemplate']"}),
'from_email': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '150'}),
'rate_limit_amount': ('django.db.models.fields.PositiveSmallIntegerField', [], {'default': '60'}),
'rate_limit_interval': ('django.db.models.fields.CharField', [], {'default': "'h'", 'max_length': '1'}),
'reply_to': ('django.db.models.fields.CharField', [], {'max_length': '150', 'blank': 'True'}),
'site': ('django.db.models.fields.related.ForeignKey', [], {'default': '1', 'to': "orm['sites.Site']"}),
'start': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2013, 3, 6, 0, 0)'}),
'status': ('django.db.models.fields.CharField', [], {'default': "'dr'", 'max_length': '2'}),
'subject': ('django.db.models.fields.CharField', [], {'max_length': '150'})
},
'cartero.contact': {
'Meta': {'object_name': 'Contact'},
'bounced': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'data': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '200'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '150'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '150'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'sent': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'site': ('django.db.models.fields.related.ForeignKey', [], {'default': '1', 'to': "orm['sites.Site']"}),
'testing': ('django.db.models.fields.BooleanField', [], {'default': 'False'})
},
'cartero.emailtemplate': {
'Meta': {'object_name': 'EmailTemplate'},
'context': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'html': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '150'}),
'plain': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'site': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['sites.Site']"}),
'template': ('django.db.models.fields.FilePathField', [], {'path': "'/home/antares/paginas_web/adcxml/templates/mails/campaigns/'", 'max_length': '255', 'match': "'\\\\.html$'"})
},
'cartero.excludedemail': {
'Meta': {'object_name': 'ExcludedEmail'},
'comments': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'email': ('django.db.models.fields.EmailField', [], {'unique': 'True', 'max_length': '200'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'reason': ('django.db.models.fields.CharField', [], {'max_length': '2'})
},
'cartero.image': {
'Meta': {'object_name': 'Image'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'file': ('django.db.models.fields.files.ImageField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '150'}),
'site': ('django.db.models.fields.related.ForeignKey', [], {'default': '1', 'to': "orm['sites.Site']"}),
'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '255'})
},
'cartero.linkclick': {
'Meta': {'object_name': 'LinkClick'},
'campaign': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cartero.Campaign']"}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'ip_address': ('django.db.models.fields.IPAddressField', [], {'max_length': '15', 'null': 'True', 'db_index': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'url': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cartero.ShortURL']"})
},
'cartero.mailtracks': {
'Meta': {'object_name': 'MailTracks'},
'campaign': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cartero.Campaign']"}),
'contact': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cartero.Contact']"}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
},
'cartero.openemail': {
'Meta': {'object_name': 'OpenEmail'},
'campaign': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cartero.Campaign']"}),
'contact': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cartero.Contact']"}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'ip_address': ('django.db.models.fields.IPAddressField', [], {'max_length': '15', 'null': 'True', 'db_index': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
},
'cartero.shorturl': {
'Meta': {'object_name': 'ShortURL'},
'code': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '75', 'db_index': 'True'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'url': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'})
},
'sites.site': {
'Meta': {'ordering': "('domain',)", 'object_name': 'Site', 'db_table': "'django_site'"},
'domain': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
}
}
complete_apps = ['cartero']
\ No newline at end of file
......@@ -37,7 +37,10 @@ class Contact(models.Model):
verbose_name_plural = _(u'Contacts')
def __unicode__(self):
return 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)
......@@ -91,6 +94,20 @@ class Campaign(models.Model):
(RATE_LIMIT_HOURS, _(u'per hour')),
)
# STATUS
DRAFT = 'dr'
READY_TO_BE_SENT = 'rs'
SENDING = 'se'
SENT = 'st'
CANCELLED = 'ca'
CAMPAIGN_STATUS = (
(DRAFT, _(u'Draft')),
(READY_TO_BE_SENT, _(u'Ready to be sent')),
(SENDING, _(u'Sending...')),
(SENT, _(u'Sent')),
(CANCELLED, _(u'Cancelled')),
)
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)
......@@ -107,9 +124,10 @@ class Campaign(models.Model):
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)
#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=60, 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'))
......
# -*- coding: utf-8 -*-
from celery import task
from celery import task, group
from cartero.utils.sending import CampaignDeliver
from cartero.models import Campaign
from django.utils import timezone
......@@ -9,22 +9,28 @@ def check_campaigns():
now = timezone.now()
campaigns_to_be_sent = Campaign.objects.filter(
start__lte = now,
ready = True,
sent = False
status = Campaign.READY_TO_BE_SENT
)
for campaign in campaigns_to_be_sent:
# Change status to sending
campaign.status = Campaign.SENDING
campaign.save()
cd = CampaignDeliver(campaign)
# Set the rate limit
task.control.rate_limit('cartero.tasks.send_message', campaign.rate_limit)
contacts = cd.get_contacts()
for contact in contacts:
send_message.delay(cd, contact).get()
campaign.sent = True
g = group(send_message.s(cd, contact) for contact in contacts)
res = g()
res.get()
campaign.status = Campaign.SENT
campaign.save()
@task.task
def send_message(campaign_deliver, contact):
campaign_deliver.send_to_contact(contact)
......
......@@ -84,6 +84,8 @@ class CampaignDeliver(object):
def send_to_contact(self, contact):
msg = self.compose_email(contact)
msg.send()
contact.sent = True
contact.save()
def compose_email(self, contact):
html = self.campaign.email_template.html
......
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