<commit_before>#!/usr/bin/env python
# encoding: utf-8
"""
Django Template Tags

Created by <NAME> on 2006-08-22.
Copyright (c) 2006-2008 HUDORA GmbH. Consider it BSD licensed.
"""

import operator
import urllib
from django import template
from django.template import resolve_variable
from django.utils.html import conditional_escape
from django.utils.text import smart_split
from django.utils.safestring import mark_safe
huimages = None # lazy import, see ImageLink.__init__()


register = template.Library()


@register.filter
def format_location(value, dummy):
    """
    Formats a myPL location nicely.
    """
    
    # TODO: move to myPLfrontend - this is not of general interest.
    if len(value) == 6 and str(value).isdigit():
        return "%s-%s-%s" % (value[:2], value[2:4], value[4:])
    return value


@register.filter
def slashencode(value):
    """
    Like Django's urlencode but also encodes slashes.
    Example::
    
        {% 14600/WK|slashencode %} results in 14600%2FWK;
    """
    
    return urllib.quote(value, '')


@register.filter
def format_address(obj, autoescape=None):
    """Formats the address in a Lieferung/Lieferschein/Kunde as nice XHTML.
    
    The obj must follow the address-protocol outlined at
    http://cybernetics.hudora.biz/projects/wiki/AddressProtocol
    
    The generated XHTML follows hCard described at http://microformats.org/wiki/hcard
    """
    
    ret = []
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    
    kdnstr = '<span class="org name1">%s</span>' % esc(obj.name1)
    if hasattr(obj, 'kundennr') and obj.kundennr:
        kdnstr += ' (<span class="customerid">%s</span>)' % esc(obj.kundennr)
    if hasattr(obj, 'kundennummer') and obj.kundennummer: # TODO: remove support for kundennummer (legacy)
        kdnstr += ' (<span class="customerid">%s</span>)' % esc(obj.kundennummer)
    ret.append(kdnstr)
    for dataname in ['name2', 'name3']:
        data = ''
        if hasattr(obj, dataname):
            data = getattr(obj, dataname)
        if data:
            ret.append('<span class="%s">%s</span>' % (dataname, esc(data)))
    if hasattr(obj, 'iln') and obj.iln:
        kdnstr += 'ILN <span class="iln">%s</span>' % esc(obj.iln)
    
    ret.append('<span class="adr">')
    for dataname in ['adresse', 'address', 'street', 'strasse']:
        data = ''
        if hasattr(obj, dataname):
            data = getattr(obj, dataname)
        if data:
            ret.append('<span class="%s">%s</span>' % (dataname, esc(data)))
    if hasattr(obj, 'plz'):
        ortstr = ('<span class="zip postal-code">%s</span>'
                  ' <span class="city locality">%s</span>' % (esc(obj.plz), esc(obj.ort)))
    else:
        ortstr = ('<span class="zip postal-code">%s</span>'
                  ' <span class="city locality">%s</span>' % (esc(obj.postleitzahl), esc(obj.ort)))
    land = 'DE'
    if hasattr(obj, 'land'):
        land = obj.land
    else:
        land = obj.laenderkennzeichen
    if land != 'DE':
        ortstr = ('<span class="country-name land">%s</span>-' % esc(land)) + ortstr
    ret.append(ortstr)
    ret.append('</span">')
    
    # the whole thing is enclesed in a vcard class tag
    return mark_safe('<div class="address vcard">%s</div>' % '<br/>'.join(ret))
format_address.needs_autoescape = True


@register.filter
def format_addressproto(obj, autoescape=None):
    """Formats the address as nice XHTML. Includes telephone number and the like.
    
    The obj must follow the address-protocol outlined at
    http://cybernetics.hudora.biz/projects/wiki/AddressProtocol
    
    The generated XHTML follows hCard described at http://microformats.org/wiki/hcard
    """
    
    ret = []
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    
    kdnstr = '<span class="org name1">%s</span>' % esc(obj.name1)
    if hasattr(obj, 'kundennr') and obj.kundenr:
        kdnstr += ' (<span class="customerid">%s</span>)' % esc(obj.kundenr)
    elif hasattr(obj, 'softmid') and obj.softmid:
        kdnstr += ' (<span class="customerid">SC%s</span>)' % esc(obj.softmid)
    elif hasattr(obj, 'curmid') and obj.curmid:
        kdnstr += ' (<span class="customerid">%s</span>)' % esc(obj.curmid)
    ret.append(kdnstr)
    
    for dataname in ['name2', 'name3']:
        data = ''
        if hasattr(obj, dataname):
            data = getattr(obj, dataname)
        if data:
            ret.append('<span class="%s">%s</span>' % (dataname, esc(data)))
    
    if hasattr(obj, 'iln') and obj.iln:
        kdnstr += 'ILN <span class="iln">%s</span>' % esc(obj.iln)
    
    addr = []
    for dataname in ['adresse', 'address', 'street', 'strasse']:
        data = ''
        if hasattr(obj, dataname):
            data = getattr(obj, dataname)
        if data:
            addr.append('<span class="%s">%s</span>' % (dataname, esc(data)))
    if hasattr(obj, 'plz'):
        ortstr = ('<span class="zip postal-code">%s</span>'
                  ' <span class="city locality">%s</span>' % (esc(obj.plz), esc(obj.ort)))
    else:<commit_msg>Fix when formating adresses with `kundennr` set<commit_after>#!/usr/bin/env python
# encoding: utf-8
"""
Django Template Tags

Created by <NAME> on 2006-08-22.
Copyright (c) 2006-2008 HUDORA GmbH. Consider it BSD licensed.
"""

import operator
import urllib
from django import template
from django.template import resolve_variable
from django.utils.html import conditional_escape
from django.utils.text import smart_split
from django.utils.safestring import mark_safe
huimages = None # lazy import, see ImageLink.__init__()


register = template.Library()


@register.filter
def format_location(value, dummy):
    """
    Formats a myPL location nicely.
    """
    
    # TODO: move to myPLfrontend - this is not of general interest.
    if len(value) == 6 and str(value).isdigit():
        return "%s-%s-%s" % (value[:2], value[2:4], value[4:])
    return value


@register.filter
def slashencode(value):
    """
    Like Django's urlencode but also encodes slashes.
    Example::
    
        {% 14600/WK|slashencode %} results in 14600%2FWK;
    """
    
    return urllib.quote(value, '')


@register.filter
def format_address(obj, autoescape=None):
    """Formats the address in a Lieferung/Lieferschein/Kunde as nice XHTML.
    
    The obj must follow the address-protocol outlined at
    http://cybernetics.hudora.biz/projects/wiki/AddressProtocol
    
    The generated XHTML follows hCard described at http://microformats.org/wiki/hcard
    """
    
    ret = []
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    
    kdnstr = '<span class="org name1">%s</span>' % esc(obj.name1)
    if hasattr(obj, 'kundennr') and obj.kundennr:
        kdnstr += ' (<span class="customerid">%s</span>)' % esc(obj.kundennr)
    if hasattr(obj, 'kundennummer') and obj.kundennummer: # TODO: remove support for kundennummer (legacy)
        kdnstr += ' (<span class="customerid">%s</span>)' % esc(obj.kundennummer)
    ret.append(kdnstr)
    for dataname in ['name2', 'name3']:
        data = ''
        if hasattr(obj, dataname):
            data = getattr(obj, dataname)
        if data:
            ret.append('<span class="%s">%s</span>' % (dataname, esc(data)))
    if hasattr(obj, 'iln') and obj.iln:
        kdnstr += 'ILN <span class="iln">%s</span>' % esc(obj.iln)
    
    ret.append('<span class="adr">')
    for dataname in ['adresse', 'address', 'street', 'strasse']:
        data = ''
        if hasattr(obj, dataname):
            data = getattr(obj, dataname)
        if data:
            ret.append('<span class="%s">%s</span>' % (dataname, esc(data)))
    if hasattr(obj, 'plz'):
        ortstr = ('<span class="zip postal-code">%s</span>'
                  ' <span class="city locality">%s</span>' % (esc(obj.plz), esc(obj.ort)))
    else:
        ortstr = ('<span class="zip postal-code">%s</span>'
                  ' <span class="city locality">%s</span>' % (esc(obj.postleitzahl), esc(obj.ort)))
    land = 'DE'
    if hasattr(obj, 'land'):
        land = obj.land
    else:
        land = obj.laenderkennzeichen
    if land != 'DE':
        ortstr = ('<span class="country-name land">%s</span>-' % esc(land)) + ortstr
    ret.append(ortstr)
    ret.append('</span">')
    
    # the whole thing is enclesed in a vcard class tag
    return mark_safe('<div class="address vcard">%s</div>' % '<br/>'.join(ret))
format_address.needs_autoescape = True


@register.filter
def format_addressproto(obj, autoescape=None):
    """Formats the address as nice XHTML. Includes telephone number and the like.
    
    The obj must follow the address-protocol outlined at
    http://cybernetics.hudora.biz/projects/wiki/AddressProtocol
    
    The generated XHTML follows hCard described at http://microformats.org/wiki/hcard
    """
    
    print "XXX", dir(obj)
    ret = []
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    
    kdnstr = '<span class="org name1">%s</span>' % esc(obj.name1)
    if getattr(obj, 'kundennr'):
        kdnstr += ' (<span class="customerid">%s</span>)' % esc(obj.kundennr)
    elif getattr(obj, 'softmid'):
        kdnstr += ' (<span class="customerid">SC%s</span>)' % esc(obj.softmid)
    elif getattr(obj, 'curmid'):
        kdnstr += ' (<span class="customerid">%s</span>)' % esc(obj.curmid)
    ret.append(kdnstr)

    for dataname in ['name2', 'name3']:
        data = ''
        if hasattr(obj, dataname):
            data = getattr(obj, dataname)
        if data:
            ret.append('<span class="%s">%s</span>' % (dataname, esc(data)))
    
    if hasattr(obj, 'iln') and obj.iln:
        kdnstr += 'ILN <span class="iln">%s</span>' % esc(obj.iln)
    
    addr = []
    for dataname in ['adresse', 'address', 'street', 'strasse']:
        data = ''
        if hasattr(obj, dataname):
            data = getattr(obj, dataname)
        if data:
            addr.append('<span class="%s">%s</span>' % (dataname, esc(data)))
    if hasattr(obj, 'plz'):
        ortstr = ('<span class="zip postal-code">%s</span>'
                  ' <span class="city locality">%s</span>' % (esc(obj.plz), esc(obj.ort)))
    else:
{'fraction_non_alphanumeric': 0.15709558107845045, 'fraction_numerical': 0.007981312049834533, 'mean_word_length': 2.8154474563683625, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 95, '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': '6939137', 'n_tokens_mistral': 3583, 'n_tokens_neox': 3461, 'n_words': 840}