def body_insertion(content, insertion, end=False): """Insert an HTML content into the body HTML node""" if not content.startswith('<body'): content = '<body>%s</body>' % content soup = BeautifulSoup(content) if end: soup.body.append(insertion) else: soup.body.insert(0, insertion) return soup.prettify()
def track_links(content, context): """Convert all links in the template for the user to track his navigation""" if not context.get('uidb36'): return content soup = BeautifulSoup(content) for link_markup in soup('a'): if link_markup.get('href'): link_href = link_markup['href'] link_title = link_markup.get('title', link_href) link, created = Link.objects.get_or_create(url=link_href, defaults={'title': link_title}) link_markup['href'] = 'http://%s%s' % (context['domain'], reverse('os3marketing_clicked_link', args=[context['newsletter'].slug, context['uidb36'], context['token'], link.pk])) return soup.prettify()