Django

Code

Ticket #924: 924.2.diff

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

Patch: convert str-unicode-str where needed

  • django/core/template/defaultfilters.py

    old new  
    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 
    55import re 
    66import random as random_module 
    77 
     
    1818 
    1919def capfirst(value): 
    2020    "Capitalizes the first character of the value" 
    21     value = str(value) 
    22     return value and value[0].upper() + value[1:] 
     21    if not value: 
     22        return value 
     23    else: 
     24        value = str(value).decode(DEFAULT_CHARSET) 
     25        value = value[0].upper() + value[1:] 
     26        return value.encode(DEFAULT_CHARSET) 
    2327 
    2428def fix_ampersands(value): 
    2529    "Replaces ampersands with ``&amp;`` entities" 
     
    5357 
    5458def lower(value): 
    5559    "Converts a string into all lowercase" 
    56     return value.lower(
     60    return str(value).decode(DEFAULT_CHARSET).lower().encode(DEFAULT_CHARSET
    5761 
    5862def make_list(value): 
    5963    """ 
     
    8387 
    8488def title(value): 
    8589    "Converts a string into titlecase" 
    86     return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title()
     90    return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), str(value).decode(DEFAULT_CHARSET).title()).encode(DEFAULT_CHARSET
    8791 
    8892def truncatewords(value, arg): 
    8993    """ 
     
    96100        length = int(arg) 
    97101    except ValueError: # invalid literal for int() 
    98102        return value # Fail silently. 
    99     if not isinstance(value, basestring): 
    100         value = str(value) 
    101     return truncate_words(value, length) 
     103    return truncate_words(str(value).decode(DEFAULT_CHARSET), length).encode(DEFAULT_CHARSET) 
    102104 
    103105def upper(value): 
    104106    "Converts a string into all uppercase" 
    105     return value.upper(
     107    return str(value).decode(DEFAULT_CHARSET).upper().encode(DEFAULT_CHARSET
    106108 
    107109def urlencode(value): 
    108110    "Escapes a value for use in a URL" 
     
    134136    Argument: number of words to wrap the text at. 
    135137    """ 
    136138    from django.utils.text import wrap 
    137     return wrap(value, int(arg)
     139    return wrap(str(value).decode(DEFAULT_CHARSET), int(arg)).encode(DEFAULT_CHARSET
    138140 
    139141def ljust(value, arg): 
    140142    """ 
     
    142144 
    143145    Argument: field size 
    144146    """ 
    145     return str(value).ljust(int(arg)
     147    return str(value).decode(DEFAULT_CHARSET).ljust(int(arg)).encode(DEFAULT_CHARSET
    146148 
    147149def rjust(value, arg): 
    148150    """ 
     
    150152 
    151153    Argument: field size 
    152154    """ 
    153     return str(value).rjust(int(arg)
     155    return str(value).decode(DEFAULT_CHARSET).rjust(int(arg)).encode(DEFAULT_CHARSET
    154156 
    155157def center(value, arg): 
    156158    "Centers the value in a field of a given width" 
    157     return str(value).center(int(arg)
     159    return str(value).decode(DEFAULT_CHARSET).center(int(arg)).encode(DEFAULT_CHARSET
    158160 
    159161def cut(value, arg): 
    160162    "Removes all values of arg from the given string" 
     
    234236 
    235237def length(value): 
    236238    "Returns the length of the value - useful for lists" 
     239    if isinstance(value,str): 
     240        value=value.decode(DEFAULT_CHARSET) 
    237241    return len(value) 
    238242 
    239243def length_is(value, arg): 
    240244    "Returns a boolean of whether the value's length is the argument" 
     245    if isinstance(value,str): 
     246        value=value.decode(DEFAULT_CHARSET) 
    241247    return len(value) == int(arg) 
    242248 
    243249def random(value):