Ticket #924: 924.2.diff
File 924.2.diff, 3.4 KB (added by , 19 years ago) |
---|
-
django/core/template/defaultfilters.py
1 1 "Default variable filters" 2 2 3 3 from django.core.template import resolve_variable, Library 4 from django.conf.settings import DATE_FORMAT, TIME_FORMAT 4 from django.conf.settings import DATE_FORMAT, TIME_FORMAT, DEFAULT_CHARSET 5 5 import re 6 6 import random as random_module 7 7 … … 18 18 19 19 def capfirst(value): 20 20 "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) 23 27 24 28 def fix_ampersands(value): 25 29 "Replaces ampersands with ``&`` entities" … … 53 57 54 58 def lower(value): 55 59 "Converts a string into all lowercase" 56 return value.lower()60 return str(value).decode(DEFAULT_CHARSET).lower().encode(DEFAULT_CHARSET) 57 61 58 62 def make_list(value): 59 63 """ … … 83 87 84 88 def title(value): 85 89 "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) 87 91 88 92 def truncatewords(value, arg): 89 93 """ … … 96 100 length = int(arg) 97 101 except ValueError: # invalid literal for int() 98 102 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) 102 104 103 105 def upper(value): 104 106 "Converts a string into all uppercase" 105 return value.upper()107 return str(value).decode(DEFAULT_CHARSET).upper().encode(DEFAULT_CHARSET) 106 108 107 109 def urlencode(value): 108 110 "Escapes a value for use in a URL" … … 134 136 Argument: number of words to wrap the text at. 135 137 """ 136 138 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) 138 140 139 141 def ljust(value, arg): 140 142 """ … … 142 144 143 145 Argument: field size 144 146 """ 145 return str(value). ljust(int(arg))147 return str(value).decode(DEFAULT_CHARSET).ljust(int(arg)).encode(DEFAULT_CHARSET) 146 148 147 149 def rjust(value, arg): 148 150 """ … … 150 152 151 153 Argument: field size 152 154 """ 153 return str(value). rjust(int(arg))155 return str(value).decode(DEFAULT_CHARSET).rjust(int(arg)).encode(DEFAULT_CHARSET) 154 156 155 157 def center(value, arg): 156 158 "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) 158 160 159 161 def cut(value, arg): 160 162 "Removes all values of arg from the given string" … … 234 236 235 237 def length(value): 236 238 "Returns the length of the value - useful for lists" 239 if isinstance(value,str): 240 value=value.decode(DEFAULT_CHARSET) 237 241 return len(value) 238 242 239 243 def length_is(value, arg): 240 244 "Returns a boolean of whether the value's length is the argument" 245 if isinstance(value,str): 246 value=value.decode(DEFAULT_CHARSET) 241 247 return len(value) == int(arg) 242 248 243 249 def random(value):