Django

Code

Ticket #924: 924.4.diff

File 924.4.diff, 3.3 kB (added by Maniac <Maniac@SoftwareManiacs.Org>, 3 years ago)

Patch for magic-removal branch

  • django/template/defaultfilters.py

    old new  
    88 
    99register = Library() 
    1010 
     11DEFAULT_CHARSET = settings.DEFAULT_CHARSET 
     12 
    1113################### 
    1214# STRINGS         # 
    1315################### 
     
    1921 
    2022def capfirst(value): 
    2123    "Capitalizes the first character of the value" 
    22     value = str(value) 
    23     return value and value[0].upper() + value[1:] 
     24    if not value: 
     25        return value 
     26    else: 
     27        value = str(value).decode(DEFAULT_CHARSET) 
     28        value = value[0].upper() + value[1:] 
     29        return value.encode(DEFAULT_CHARSET) 
    2430 
    2531def fix_ampersands(value): 
    2632    "Replaces ampersands with ``&amp;`` entities" 
     
    5460 
    5561def lower(value): 
    5662    "Converts a string into all lowercase" 
    57     return value.lower(
     63    return str(value).decode(DEFAULT_CHARSET).lower().encode(DEFAULT_CHARSET
    5864 
    5965def make_list(value): 
    6066    """ 
     
    8490 
    8591def title(value): 
    8692    "Converts a string into titlecase" 
    87     return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title()
     93    return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), str(value).decode(DEFAULT_CHARSET).title()).encode(DEFAULT_CHARSET
    8894 
    8995def truncatewords(value, arg): 
    9096    """ 
     
    97103        length = int(arg) 
    98104    except ValueError: # invalid literal for int() 
    99105        return value # Fail silently. 
    100     if not isinstance(value, basestring): 
    101         value = str(value) 
    102     return truncate_words(value, length) 
     106    return truncate_words(str(value).decode(DEFAULT_CHARSET), length).encode(DEFAULT_CHARSET) 
    103107 
    104108def upper(value): 
    105109    "Converts a string into all uppercase" 
    106     return value.upper(
     110    return str(value).decode(DEFAULT_CHARSET).upper().encode(DEFAULT_CHARSET
    107111 
    108112def urlencode(value): 
    109113    "Escapes a value for use in a URL" 
     
    136140    Argument: number of words to wrap the text at. 
    137141    """ 
    138142    from django.utils.text import wrap 
    139     return wrap(str(value), int(arg)
     143    return wrap(str(value).decode(DEFAULT_CHARSET), int(arg)).encode(DEFAULT_CHARSET
    140144 
    141145def ljust(value, arg): 
    142146    """ 
     
    144148 
    145149    Argument: field size 
    146150    """ 
    147     return str(value).ljust(int(arg)
     151    return str(value).decode(DEFAULT_CHARSET).ljust(int(arg)).encode(DEFAULT_CHARSET
    148152 
    149153def rjust(value, arg): 
    150154    """ 
     
    152156 
    153157    Argument: field size 
    154158    """ 
    155     return str(value).rjust(int(arg)
     159    return str(value).decode(DEFAULT_CHARSET).rjust(int(arg)).encode(DEFAULT_CHARSET
    156160 
    157161def center(value, arg): 
    158162    "Centers the value in a field of a given width" 
    159     return str(value).center(int(arg)
     163    return str(value).decode(DEFAULT_CHARSET).center(int(arg)).encode(DEFAULT_CHARSET
    160164 
    161165def cut(value, arg): 
    162166    "Removes all values of arg from the given string" 
     
    236240 
    237241def length(value): 
    238242    "Returns the length of the value - useful for lists" 
     243    if isinstance(value,str): 
     244        value=value.decode(DEFAULT_CHARSET) 
    239245    return len(value) 
    240246 
    241247def length_is(value, arg): 
    242248    "Returns a boolean of whether the value's length is the argument" 
     249    if isinstance(value,str): 
     250        value=value.decode(DEFAULT_CHARSET) 
    243251    return len(value) == int(arg) 
    244252 
    245253def random(value):