def new_draftemail(self, *args, **kwargs): """Creates and saves a DraftEmail. If kwarg 'recipients' is given, it must be an iterable, and its contents will be added to pending_recipients.""" recipients = [] if 'recipients' in kwargs: recipients.extend(kwargs['recipients']) del kwargs['recipients'] d = DraftEmail(**kwargs) d.save() for r in recipients: d.pending_recipients.add(r) return d
def test_compile_basic(self): de = DraftEmail(fromemail='', subject='Subject') html = '<html><head></head><body></body></html>' de.message = html de.save() e = de._compile() self.assertEquals(e.subject, 'Subject') self.assertEquals(e.txtmsg, '') self.assertEqualsHtml(e.htmlmsg, html) # There's only one Tracker object so far self.assertEquals(Tracker.objects.count(), 1) self.assertEquals(e.tracker, Tracker.objects.all()[0])
def test_send_single(self): """Sends an email to a single recipient.""" v = Visitor(username='******', emailaddress='*****@*****.**') v.save() de = DraftEmail(fromemail='', subject='Subject', pixelimage=False) html = '<html><head></head><body></body></html>' de.message = html de.save() de.pending_recipients.add(v) de.save() e = de.send() self.assertTrue(de.sent) self.assertEquals(e.subject, 'Subject') self.assertEquals(e.txtmsg, '') self.assertEqualsHtml(e.htmlmsg, html) # There's only one Tracker object so far self.assertEquals(Tracker.objects.count(), 1) self.assertEquals(e.tracker, Tracker.objects.all()[0]) self.assertEquals(len(django_email.outbox), 1) self.assertEquals(django_email.outbox[0].subject, 'Subject') self.assertEquals(django_email.outbox[0].recipients(), ['*****@*****.**'])
def composeEmail(request, emailid=None): """The view in which to compose an email, or edit an existing (but not-yet-sent) email. If emailid is None, compose a new email; if emailid refers to a valid but unsent email, edit the existing email; if emailid refers to a valid but sent email, display the contents but do not allow it to be edited. """ # TODO: if emailid is not None and not found, do what? if request.method == 'POST': # If the form has been submitted... if emailid is not None: instance = DraftEmail.objects.get(pk=emailid) else: instance = DraftEmail() instance.save() form = ComposeEmailForm(request.POST, instance=instance) if form.is_valid(): # All validation rules pass # get data in 'to' and convert it to 'trackees' d = resolve_emails(form.cleaned_data['to']) # Process the data in form.cleaned_data draft = form.save(commit=False) draft.pending_recipients.clear() for t in d['trackees']: draft.pending_recipients.add(t) draft.save() form.save_m2m() if 'do_save' in request.POST: url = urlreverse('linkanalytics-email-idcompose', kwargs={'emailid':draft.pk}) return HttpResponseRedirect(url) elif 'do_send' in request.POST: draft.send() url = urlreverse('linkanalytics-email-idcompose', kwargs={'emailid':draft.pk}) return HttpResponseRedirect(url) else: if emailid is not None: form = ComposeEmailForm(instance=DraftEmail.objects.get(pk=emailid)) else: form = ComposeEmailForm() contacts = Visitor.objects.exclude(emailaddress='') return _email_render_to_response('linkanalytics/email/compose.html', {'form': form, 'emailid': emailid, 'contacts': contacts}, context_instance=RequestContext(request))
def test_send_basic(self): """Sends an email to no one.""" de = DraftEmail(fromemail='', subject='Subject', pixelimage=False) html = '<html><head></head><body></body></html>' de.message = html de.save() e = de.send() self.assertTrue(de.sent) self.assertEquals(e.subject, 'Subject') self.assertEquals(e.txtmsg, '') self.assertEqualsHtml(e.htmlmsg, html) # There's only one Tracker object so far self.assertEquals(Tracker.objects.count(), 1) self.assertEquals(e.tracker, Tracker.objects.all()[0]) self.assertEquals(len(django_email.outbox), 0)
def test_send_blankSubject(self): v = Visitor(username='******', emailaddress='*****@*****.**') v.save() de = DraftEmail(fromemail='', pixelimage=False) de.message = 'My message.' de.save() de.pending_recipients.add(v) de.save() e = de.send() self.assertTrue(de.sent) self.assertEquals(e.subject, app_settings.EMAIL_DEFAULT_SUBJECT) self.assertEquals(len(django_email.outbox), 1) self.assertEquals( django_email.outbox[0].subject, app_settings.EMAIL_DEFAULT_SUBJECT )
def test_draft_count(self): self.create_users(1) with self.scoped_login('user0', 'password'): url = urlreverse('linkanalytics-email-main') # Case 1: no drafts response = self.client.get(url) self.assertEquals(response.context['draft_count'], 0) # Case 2: one unsent draft e = DraftEmail() e.save() response = self.client.get(url) self.assertEquals(response.context['draft_count'], 1) # Case 3: first two unsent drafts, then one unsent draft and one # sent draft e = DraftEmail() e.save() response = self.client.get(url) self.assertEquals(response.context['draft_count'], 2) e.sent = True e.save() response = self.client.get(url) self.assertEquals(response.context['draft_count'], 1)
def test_send_pixelimage(self): """Sends an email containing a pixelimage.""" v = Visitor(username='******', emailaddress='*****@*****.**') v.save() html = '<html><head></head><body></body></html>' de = DraftEmail(fromemail='', subject='Subject') de.message = html de.pixelimage = True de.save() de.pending_recipients.add(v) de.save() eml = de.send() self.assertTrue(de.sent) qs = TrackedInstance.objects.filter(visitor=v) self.assertEquals(qs.count(), 1) uuid = qs[0].uuid self.assertEquals(len(django_email.outbox), 1) msg = django_email.outbox[0] content,mime = msg.alternatives[0] self.assertEquals(mime, 'text/html') ##print '\n{0}\n'.format(content) e = xml.etree.ElementTree.fromstring(content) self.assertEquals(e.tag, 'html') body = e.find('body') self.assertNotEquals(body, None) img = body.find('img') self.assertNotEquals(img, None) src = img.get('src', default=None) self.assertNotEquals(src, None) url = urlex.hashedurl_pixelpng(uuid) self.assertEquals(src, '{0}{1}'.format(app_settings.URLBASE, url))