from armstrong.utils.backends import GenericBackend
from django.db import models
from django.template import Template
try:
    from django.utils.text import slugify
except ImportError:
    from django.template.defaultfilters import slugify


from . import email
from . import utils


class NamedSlugMixin(object):
    @property
    def slug(self):
        return slugify(self.name)


class Receipient(NamedSlugMixin, models.Model):
    name = models.CharField(max_length=255)
    email = models.EmailField()
    active = models.BooleanField(default=True)

    def __unicode__(self):
        return u'%s (%s)' % (self.name, self.email)

    def send(self, receipient_list, blast):
        utils.dispatch_to_backend('TT_DAILYEMAILBLAST_RECEIPIENT',
                'tt_dailyemailblast.send_backends.sync_receipient',
                self, receipient_list, blast)


class ReceipientList(NamedSlugMixin, models.Model):
    name = models.CharField(max_length=255)
    receipients = models.ManyToManyField(Receipient, related_name='lists')

    def __unicode__(self):
        return u'%s (%s)' % (self.name, self.receipients.count())

    def send(self, blast):
        utils.dispatch_to_backend('TT_DAILYEMAILBLAST_RECEIPIENTLIST',
                'tt_dailyemailblast.send_backends.sync_receipientlist',
                self, blast)


class DailyEmailBlastType(NamedSlugMixin, models.Model):
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name


class DailyEmailBlast(models.Model):
    blast_type = models.ForeignKey(DailyEmailBlastType, related_name='blasts')
    created_on = models.DateTimeField(auto_now_add=True)
    body = models.TextField()
    receipient_lists = models.ManyToManyField(ReceipientList,
            related_name='blasts')

    def __unicode__(self):
        return u'%s (%s)' % (self.blast_type, self.created_on)

    def send(self):
        utils.dispatch_to_backend('TT_DAILYEMAILBLAST_BLASTBACKEND',
                'tt_dailyemailblast.send_backends.sync_dailyemailblasts',
                self)

    def render(self, receipient, receipient_list):
        context = self.get_context_data(receipient, receipient_list)
        t = Template(utils.get_template_names(self, receipient_list,
                receipient))
        return t.render(context)

    def get_context_data(self, receipient, receipient_list):
        context_backend = GenericBackend('TT_DAILYEMAILBLAST_CONTEXT',
                defaults=['tt_dailyemailblast.context_backend.basic'])
        return context_backend(self, receipient, receipient_list)

@@ -643,33 +643,32 @@
 f send(self, rec
-e
 ipient_list, bla
@@ -793,25 +793,24 @@
 nds.sync_rec
-e
 ipient',%0A   
@@ -823,33 +823,32 @@
        self, rec
-e
 ipient_list, bla
@@ -952,25 +952,24 @@
 255)%0A    rec
-e
 ipients = mo
@@ -1099,17 +1099,16 @@
 self.rec
-e
 ipients.
@@ -1273,17 +1273,16 @@
 sync_rec
-e
 ipientli
@@ -1679,25 +1679,24 @@
 ld()%0A    rec
-e
 ipient_lists
@@ -2066,33 +2066,32 @@
 render(self, rec
-e
 ipient, receipie
@@ -2077,33 +2077,32 @@
 , recipient, rec
-e
 ipient_list):%0A  
@@ -2142,17 +2142,16 @@
 data(rec
-e
 ipient, 
@@ -2145,33 +2145,32 @@
 a(recipient, rec
-e
 ipient_list)%0A   
@@ -2217,25 +2217,24 @@
 es(self, rec
-e
 ipient_list,
@@ -2249,25 +2249,24 @@
          rec
-e
 ipient))%0A   
@@ -2322,33 +2322,32 @@
 t_data(self, rec
-e
 ipient, receipie
@@ -2333,33 +2333,32 @@
 , recipient, rec
-e
 ipient_list):%0A  
@@ -2533,25 +2533,24 @@
 nd(self, rec
-e
 ipient, rece
@@ -2548,17 +2548,16 @@
 ent, rec
-e
 ipient_l

Fix spelling typo