<commit_before>from django import template
from django.core.urlresolvers import reverse

register = template.Library()

class NewsletterstyleNode(template.Node):
    def __init__(self, key, style, request):
        self.key = key
        self.style = style
        self.request_var = template.Variable(request)
        
    def render(self, context):
        request = self.request_var.resolve(context)
        if not hasattr(request, '_pennyblack_newsletterstyle'):
            setattr(request, '_pennyblack_newsletterstyle', dict())
        request._pennyblack_newsletterstyle[str(self.key)] = self.style.render(context)
        return ''

def newsletterstyle(parser, token):
    """
    Stores the content in your context
    {% newsletterstyle request text_only_content %}
    """
    bits = list(token.split_contents())
    if len(bits) != 3 :
        raise template.TemplateSyntaxError("%r expected format is 'newsletterstyle request stylename'" %
            bits[0])
    request = bits[1]
    key = parser.compile_filter(bits[2])
    style = parser.parse(('endnewsletterstyle',))
    parser.delete_first_token()
    return NewsletterstyleNode(key, style, request)
newsletterstyle = register.tag(newsletterstyle)

class NewsletterGetStyleNode(template.Node):
    def __init__(self, key, request):
        self.key = key
        self.request_var = template.Variable(request)
    
    def render(self, context):
        request = self.request_var.resolve(context)
        if not hasattr(request, '_pennyblack_newsletterstyle'):
            return ''
        if str(self.key) not in request._pennyblack_newsletterstyle.keys():
            return ''
        return request._pennyblack_newsletterstyle[str(self.key)]
        
def get_newsletterstyle(parser, token):
    """
    Loads a stored style and renders it
    """
    bits = list(token.split_contents())
    if len(bits) != 3 :
        raise template.TemplateSyntaxError("%r expected format is 'newsletterstyle request stylename'" %
            bits[0])
    request = bits[1]
    key = parser.compile_filter(bits[2])
    return NewsletterGetStyleNode(key, request)
get_newsletterstyle = register.tag(get_newsletterstyle)


class NewsletterHeaderImageNode(template.Node):
    def __init__(self, extra_args={}):
        self.extra_args = extra_args
        
    def render(self, context):
        newsletter = context['newsletter']
        if context['webview']:
            header_url = ''
            header_image = newsletter.header_image.get_absolute_url()
        else:
            mail = context['mail']
            header_url = mail.get_header_url()
            header_image = newsletter.get_base_url() + reverse('pennyblack.ping', kwargs={'mail_hash':mail.mail_hash, 'filename':newsletter.header_image})
        return """<a href="%s" target="_blank"><img src="%s" border="0" %s/></a>""" % (header_url, header_image, ' '.join(self.extra_args))

@register.tag
def header_image(parser, token):
    """
    Renders the header image with tracking any extra args are given to the img tag
    
    {% header_image alt="Header" width="283" height="157" align="left" vspace="0" hspace="0" border="0" %}
    
    renders
    
    <a href="" target="_blank"><img src="link to the image" border="0" alt="Header" width="283" height="157" align="left" vspace="0" hspace="0" border="0"/></a>
    """
    bits = list(token.split_contents())
    extra_args = []
    for bit in bits:
        splitted = bit.split('=')
        if len(splitted) == 2:
            extra_args.append(bit)
    return NewsletterHeaderImageNode(extra_args=extra_args)

class NewsletterLinkUrlNode(template.Node):
    def __init__(self, identifier=None):<commit_msg>use tracable header image link if in webview and email ist set<commit_after>from django import template
from django.core.urlresolvers import reverse

register = template.Library()

class NewsletterstyleNode(template.Node):
    def __init__(self, key, style, request):
        self.key = key
        self.style = style
        self.request_var = template.Variable(request)
        
    def render(self, context):
        request = self.request_var.resolve(context)
        if not hasattr(request, '_pennyblack_newsletterstyle'):
            setattr(request, '_pennyblack_newsletterstyle', dict())
        request._pennyblack_newsletterstyle[str(self.key)] = self.style.render(context)
        return ''

def newsletterstyle(parser, token):
    """
    Stores the content in your context
    {% newsletterstyle request text_only_content %}
    """
    bits = list(token.split_contents())
    if len(bits) != 3 :
        raise template.TemplateSyntaxError("%r expected format is 'newsletterstyle request stylename'" %
            bits[0])
    request = bits[1]
    key = parser.compile_filter(bits[2])
    style = parser.parse(('endnewsletterstyle',))
    parser.delete_first_token()
    return NewsletterstyleNode(key, style, request)
newsletterstyle = register.tag(newsletterstyle)

class NewsletterGetStyleNode(template.Node):
    def __init__(self, key, request):
        self.key = key
        self.request_var = template.Variable(request)
    
    def render(self, context):
        request = self.request_var.resolve(context)
        if not hasattr(request, '_pennyblack_newsletterstyle'):
            return ''
        if str(self.key) not in request._pennyblack_newsletterstyle.keys():
            return ''
        return request._pennyblack_newsletterstyle[str(self.key)]
        
def get_newsletterstyle(parser, token):
    """
    Loads a stored style and renders it
    """
    bits = list(token.split_contents())
    if len(bits) != 3 :
        raise template.TemplateSyntaxError("%r expected format is 'newsletterstyle request stylename'" %
            bits[0])
    request = bits[1]
    key = parser.compile_filter(bits[2])
    return NewsletterGetStyleNode(key, request)
get_newsletterstyle = register.tag(get_newsletterstyle)


class NewsletterHeaderImageNode(template.Node):
    def __init__(self, extra_args={}):
        self.extra_args = extra_args
        
    def render(self, context):
        newsletter = context['newsletter']
        if context['webview']:
            if 'mail' in context:
                mail = context['mail']
                header_url = mail.get_header_url()
            else:
                header_url = ''
            header_image = newsletter.header_image.get_absolute_url()
        else:
            mail = context['mail']
            header_url = mail.get_header_url()
            header_image = newsletter.get_base_url() + reverse('pennyblack.ping', kwargs={'mail_hash':mail.mail_hash, 'filename':newsletter.header_image})
        return """<a href="%s" target="_blank"><img src="%s" border="0" %s/></a>""" % (header_url, header_image, ' '.join(self.extra_args))

@register.tag
def header_image(parser, token):
    """
    Renders the header image with tracking any extra args are given to the img tag
    
    {% header_image alt="Header" width="283" height="157" align="left" vspace="0" hspace="0" border="0" %}
    
    renders
    
    <a href="" target="_blank"><img src="link to the image" border="0" alt="Header" width="283" height="157" align="left" vspace="0" hspace="0" border="0"/></a>
    """
    bits = list(token.split_contents())
    extra_args = []
    for bit in bits:
        splitted = bit.split('=')
        if len(splitted) == 2:
            extra_args.append(bit)
    return NewsletterHeaderImageNode(extra_args=extra_args)

class NewsletterLinkUrlNode(template.Node):
    def __init__(self, identifier=None):
{'fraction_non_alphanumeric': 0.12026402640264026, 'fraction_numerical': 0.007656765676567656, 'mean_word_length': 3.237136465324385, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2967711', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2188, 'n_words': 495}