Commits

Owen Nelson committed 08014fa

adds template support for #5 moves dispatch implementation to target ( fixes #3 ) and implements a ghetto k,v field on targets for #6

Comments (0)

Files changed (2)

src/trawler/admin.py

 from django.contrib import admin
-
 from trawler.models import Campaign, Target
 
 class TargetInline(admin.TabularInline):
 
 class CampaignAdmin(admin.ModelAdmin):
 
-    list_display = ['title', 'created', 'dispatched']
+    list_display = ['title', 'created', 'updated']
     date_hierarchy = 'created'
     actions = ['send_test_email', 'launch_campaign']
 
     inlines = [TargetInline, ]
 
     def send_test_email(self, request, queryset):
-        [campaign.dispatch(request.user.email) for campaign in queryset]
+        tmp_target = Target(email=request.user.email)
+        for campaign in queryset:
+            tmp_target.campaign = campaign
+            tmp_target.dispatch()
+
+        stuff = 'email'
         count = queryset.count()
-
-        if count == 1:
-            stuff = 'email'
-        else:
+        if count > 1:
             stuff = 'emails'
 
         self.message_user(request, "%d test %s sent to %s" % \
 
     def launch_campaign(self, request, queryset):
         [campaign.bulk_dispatch() for campaign in queryset]
+
+        stuff = 'campaign'
         count = queryset.count()
-
-        if count == 1:
-            stuff = 'campaign'
-        else:
+        if count > 1:
             stuff = 'campaigns'
 
         self.message_user(request, "%d %s launched. Game on!" % (count, stuff))

src/trawler/models.py

 from django.db import models
 from django.core.mail import EmailMessage, EmailMultiAlternatives
 from datetime import datetime
+from django.template import Context, Template
 
 
 class Campaign(models.Model):
     img = models.FileField(upload_to='uploads/%Y/%m/%d', null=True, blank=True)
 
     created = models.DateTimeField(auto_now_add=True, editable=False)
-    dispatched = models.DateTimeField(null=True, default=None, editable=False)
+    updated = models.DateTimeField(auto_now=True, editable=False)
 
 
     def __unicode__(self):
         return self.title
 
     def bulk_dispatch(self):
-        [self.dispatch(target.email, target.pk) \
-         for target in self.targets.all()]
-        self.dispatched = datetime.now()
-        self.save()
-
-    def dispatch(self, recipient, upk= -1):
-        """the upk default value is so it can be safely formatted as %d 
-        while still being 'invalid' """
-
-        if self.sender_name:
-            sender = '"%s" <%s>' % (self.sender_name, self.sender_email)
-        else:
-            sender = self.sender_email
-
-        text_content = self.email_plain % dict(recipient=recipient, upk=upk,
-                                                   campaign=self)
-        if self.email_html:
-            # plain text and html content
-            html_content = self.email_html % dict(recipient=recipient, upk=upk,
-                                                  campaign=self)
-            msg = EmailMultiAlternatives(self.subject, text_content,
-                                         sender, [recipient])
-            msg.attach_alternative(html_content, "text/html")
-        else:
-            # plain text only
-            msg = EmailMessage(self.subject, text_content, sender,
-                               [recipient])
-        msg.send()
+        [target.dispatch() for target in self.targets.all()]
+        self.save() # to update the self.updated value 
 
     def get_percents(self):
         """
     link_followed = models.BooleanField(default=False, editable=False)
     image_viewed = models.BooleanField(default=False, editable=False)
     updated = models.DateTimeField(auto_now=True, editable=False)
+    extra_context = models.TextField(blank=True, help_text="Key=value (comma separated). Keep it "
+                            "simple since anything complex might be silently "
+                            "dropped.")
 
     def __unicode__(self):
         return self.email
 
+    @property
+    def extra(self):
+        """
+        Parses k,v from self.extra and returns as dict and drops errors 
+        silently.
+        """
+        context = {}
+        # FIXME: retrieve, and parse/eval k,v data for extra
+        for i in self.extra_context.split(','):
+            try:
+                (k, v) = i.split('=')
+                context[k.strip()] = v.strip()
+            except:
+                pass
+        return context
+
+    def dispatch(self):
+
+        if self.campaign.sender_name:
+            sender = '"%s" <%s>' % (self.campaign.sender_name,
+                                    self.campaign.sender_email)
+        else:
+            sender = self.campaign.sender_email
+
+        context = Context(dict(target=self, campaign=self.campaign))
+
+        text_content = Template(self.campaign.email_plain).render(context)
+
+        if self.campaign.email_html:
+            # plain text and html content
+            html_content = Template(self.campaign.email_html).render(context)
+
+            msg = EmailMultiAlternatives(self.campaign.subject, text_content,
+                                         sender, [self.email])
+            msg.attach_alternative(html_content, "text/html")
+        else:
+            # plain text only
+            msg = EmailMessage(self.campaign.subject, text_content, sender,
+                               [self.email])
+        msg.send()
+