Ticket #924: 924.3.diff

File 924.3.diff, 3.5 KB (added by Maniac <Maniac@…>, 19 years ago)

Updated to current trunk (was broken by fixing #1110)

  • django/core/template/defaultfilters.py

     
    11"Default variable filters"
    22
    33from django.core.template import resolve_variable, Library
    4 from django.conf.settings import DATE_FORMAT, TIME_FORMAT
     4from django.conf.settings import DATE_FORMAT, TIME_FORMAT, DEFAULT_CHARSET
    55from django.utils.translation import gettext
    66import re
    77import random as random_module
     
    1919
    2020def capfirst(value):
    2121    "Capitalizes the first character of the value"
    22     value = str(value)
    23     return value and value[0].upper() + value[1:]
     22    if not value:
     23        return value
     24    else:
     25        value = str(value).decode(DEFAULT_CHARSET)
     26        value = value[0].upper() + value[1:]
     27        return value.encode(DEFAULT_CHARSET)
    2428
    2529def fix_ampersands(value):
    2630    "Replaces ampersands with ``&amp;`` entities"
     
    5458
    5559def lower(value):
    5660    "Converts a string into all lowercase"
    57     return value.lower()
     61    return str(value).decode(DEFAULT_CHARSET).lower().encode(DEFAULT_CHARSET)
    5862
    5963def make_list(value):
    6064    """
     
    8488
    8589def title(value):
    8690    "Converts a string into titlecase"
    87     return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title())
     91    return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), str(value).decode(DEFAULT_CHARSET).title()).encode(DEFAULT_CHARSET)
    8892
    8993def truncatewords(value, arg):
    9094    """
     
    97101        length = int(arg)
    98102    except ValueError: # invalid literal for int()
    99103        return value # Fail silently.
    100     if not isinstance(value, basestring):
    101         value = str(value)
    102     return truncate_words(value, length)
     104    return truncate_words(str(value).decode(DEFAULT_CHARSET), length).encode(DEFAULT_CHARSET)
    103105
    104106def upper(value):
    105107    "Converts a string into all uppercase"
    106     return value.upper()
     108    return str(value).decode(DEFAULT_CHARSET).upper().encode(DEFAULT_CHARSET)
    107109
    108110def urlencode(value):
    109111    "Escapes a value for use in a URL"
     
    136138    Argument: number of words to wrap the text at.
    137139    """
    138140    from django.utils.text import wrap
    139     return wrap(str(value), int(arg))
     141    return wrap(str(value).decode(DEFAULT_CHARSET), int(arg)).encode(DEFAULT_CHARSET)
    140142
    141143def ljust(value, arg):
    142144    """
     
    144146
    145147    Argument: field size
    146148    """
    147     return str(value).ljust(int(arg))
     149    return str(value).decode(DEFAULT_CHARSET).ljust(int(arg)).encode(DEFAULT_CHARSET)
    148150
    149151def rjust(value, arg):
    150152    """
     
    152154
    153155    Argument: field size
    154156    """
    155     return str(value).rjust(int(arg))
     157    return str(value).decode(DEFAULT_CHARSET).rjust(int(arg)).encode(DEFAULT_CHARSET)
    156158
    157159def center(value, arg):
    158160    "Centers the value in a field of a given width"
    159     return str(value).center(int(arg))
     161    return str(value).decode(DEFAULT_CHARSET).center(int(arg)).encode(DEFAULT_CHARSET)
    160162
    161163def cut(value, arg):
    162164    "Removes all values of arg from the given string"
     
    236238
    237239def length(value):
    238240    "Returns the length of the value - useful for lists"
     241    if isinstance(value,str):
     242        value=value.decode(DEFAULT_CHARSET)
    239243    return len(value)
    240244
    241245def length_is(value, arg):
    242246    "Returns a boolean of whether the value's length is the argument"
     247    if isinstance(value,str):
     248        value=value.decode(DEFAULT_CHARSET)
    243249    return len(value) == int(arg)
    244250
    245251def random(value):
Back to Top