def test_canonicalize_links(self): """ Ensure that links are canonicalized correctly. """ template = """\ <a href="/">Home</a> <a href="/store/?page=2">Store</a> <a href="www.example.com/store/new/">New</a> <a href="subdomain.example.com/">Subdomain</a> <a href="http://www.google.com/">Fully Qualified</a>""" rendered_template = canonicalize_links(template, 'http://www.example.com') canon1 = '<a href="http://www.example.com/">Home</a>' canon2 = '<a href="http://www.example.com/store/?page=2">Store</a>' canon3 = '<a href="http://www.example.com/store/new/">New</a>' self.assertTrue(canon1 in rendered_template) self.assertTrue(canon2 in rendered_template) self.assertTrue(canon3 in rendered_template) ignore1 = '<a href="http://www.google.com/">Fully Qualified</a>' ignore2 = '<a href="subdomain.example.com/">Subdomain</a>' self.assertTrue(ignore1 in rendered_template) self.assertTrue(ignore2 in rendered_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 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)