def premail(self, template=None, canonicalize=True, track=True, plaintext=True): """ Run the newsletter template through several methods to prep it for mailing. :param template: The template to premail, defaults to this newsletters current template. :param canonicalize: If True, canonicalize the links in this template. :param track: If True, subject this template to link tracking. :param plaintext: Whether to return a plaintext copy of this template. :return: Returns a tuple (html_template, plaintext_template) containing the two rendered templates. If plaintext is False, plaintext_template will be None. """ html_template = None plaintext_template = None if not template: template = self.template # Canonicalize relative links if canonicalize: template = canonicalize_links(template) # Track links if track: template = track_document(template, domain=self.tracking_domain, campaign=self.tracking_campaign, source='newsletter-%s-issue-%s' % (self.newsletter.pk, self.pk,)) # Run premailer if getattr(settings, 'NOVA_USE_PREMAILER', False): html_template = self.premailer(template) plaintext_template = self.premailer(template, plaintext=True) else: html_template = template return (html_template, plaintext_template)
def test_track_document(self): """ Ensure that the track_document helper method works as expected. """ domain = 'example.com' campaign = 'test-campaign' source = 'test-source' medium = 'test-medium' template = """\ <html> <head> <title>Test Link Tracking</title> </head> <body> <!-- some links --> <a href="http://www.google.com/">Google</a> <a href="http://www.example.com/?param=true">Example 1</a> <a href="http://subdomain.example.com/">Example 2</a> <a href="http://www.bing.com/">Bing</a> <!-- url with a space at the end --> <a href="http://www.example.com/ ">Example 3</a> <!-- missing href attr --> <a>hello</a> </body> </html> """ # Track this template tracked_template = track_document(html=template, domain=domain, campaign=campaign, source=source, medium=medium) # Expected results track1 = """<a href="http://www.example.com/?param=true&utm_campaign={campaign}&utm_medium={medium}&utm_source={source}&utm_term={term}" class="tracked">Example 1</a>""".format(campaign=campaign, source=source, medium=medium, term='%s-%s-%s' % (source, 'link-2', 'Example+1',)) track2 = """<a href="http://subdomain.example.com/?utm_campaign={campaign}&utm_medium={medium}&utm_source={source}&utm_term={term}" class="tracked">Example 2</a>""".format(campaign=campaign, source=source, medium=medium, term='%s-%s-%s' % (source, 'link-3', 'Example+2',)) track3 = """<a href="http://www.example.com/?utm_campaign={campaign}&utm_medium={medium}&utm_source={source}&utm_term={term}" class="tracked">Example 3</a>""".format(campaign=campaign, source=source, medium=medium, term='%s-%s-%s' % (source, 'link-5', 'Example+3',)) # Assert that all three example.com links were tracked self.assertTrue(track1 in tracked_template) self.assertTrue(track2 in tracked_template) self.assertTrue(track3 in tracked_template) # Assert that only three links were tracked self.assertEqual(tracked_template.count('tracked'), 3) # Assert that the html comment wasn't munged self.assertTrue("<!-- some links -->" in tracked_template) self.assertTrue("<!--<!--" not in tracked_template)
def premail(self, template=None, canonicalize=True, track=True, plaintext=True): """ Run the newsletter template through several methods to prep it for mailing. :param template: The template to premail, defaults to this newsletters current template. :param canonicalize: If True, canonicalize the links in this template. :param track: If True, subject this template to link tracking. :param plaintext: Whether to return a plaintext copy of this template. :return: Returns a tuple (html_template, plaintext_template) containing the two rendered templates. If plaintext is False, plaintext_template will be None. """ html_template = None plaintext_template = None if not template: template = self.template # Canonicalize relative links if canonicalize: template = canonicalize_links(template) # Track links if track: template = track_document(template, domain=self.tracking_domain, campaign=self.tracking_campaign, source='newsletter-%s-issue-%s' % ( self.newsletter.pk, self.pk, )) # Run premailer if getattr(settings, 'NOVA_USE_PREMAILER', False): html_template = self.premailer(template) plaintext_template = self.premailer(template, plaintext=True) else: html_template = template return (html_template, plaintext_template)
def test_track_document(self): """ Ensure that the track_document helper method works as expected. """ domain = 'example.com' campaign = 'test-campaign' source = 'test-source' medium = 'test-medium' template = """\ <html> <head> <title>Test Link Tracking</title> </head> <body> <!-- some links --> <a href="http://www.google.com/">Google</a> <a href="http://www.example.com/?param=true">Example 1</a> <a href="http://subdomain.example.com/">Example 2</a> <a href="http://www.bing.com/">Bing</a> <!-- url with a space at the end --> <a href="http://www.example.com/ ">Example 3</a> <!-- missing href attr --> <a>hello</a> </body> </html> """ # Track this template tracked_template = track_document(html=template, domain=domain, campaign=campaign, source=source, medium=medium) # Expected results track1 = """<a href="http://www.example.com/?param=true&utm_campaign={campaign}&utm_medium={medium}&utm_source={source}&utm_term={term}" class="tracked">Example 1</a>""".format( campaign=campaign, source=source, medium=medium, term='%s-%s-%s' % ( source, 'link-2', 'Example+1', )) track2 = """<a href="http://subdomain.example.com/?utm_campaign={campaign}&utm_medium={medium}&utm_source={source}&utm_term={term}" class="tracked">Example 2</a>""".format( campaign=campaign, source=source, medium=medium, term='%s-%s-%s' % ( source, 'link-3', 'Example+2', )) track3 = """<a href="http://www.example.com/?utm_campaign={campaign}&utm_medium={medium}&utm_source={source}&utm_term={term}" class="tracked">Example 3</a>""".format( campaign=campaign, source=source, medium=medium, term='%s-%s-%s' % ( source, 'link-5', 'Example+3', )) # Assert that all three example.com links were tracked self.assertTrue(track1 in tracked_template) self.assertTrue(track2 in tracked_template) self.assertTrue(track3 in tracked_template) # Assert that only three links were tracked self.assertEqual(tracked_template.count('tracked'), 3) # Assert that the html comment wasn't munged self.assertTrue("<!-- some links -->" in tracked_template) self.assertTrue("<!--<!--" not in tracked_template)