Ticket #924: 924.4.diff
File 924.4.diff, 3.3 KB (added by , 19 years ago) |
---|
-
django/template/defaultfilters.py
8 8 9 9 register = Library() 10 10 11 DEFAULT_CHARSET = settings.DEFAULT_CHARSET 12 11 13 ################### 12 14 # STRINGS # 13 15 ################### … … 19 21 20 22 def capfirst(value): 21 23 "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) 24 30 25 31 def fix_ampersands(value): 26 32 "Replaces ampersands with ``&`` entities" … … 54 60 55 61 def lower(value): 56 62 "Converts a string into all lowercase" 57 return value.lower()63 return str(value).decode(DEFAULT_CHARSET).lower().encode(DEFAULT_CHARSET) 58 64 59 65 def make_list(value): 60 66 """ … … 84 90 85 91 def title(value): 86 92 "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) 88 94 89 95 def truncatewords(value, arg): 90 96 """ … … 97 103 length = int(arg) 98 104 except ValueError: # invalid literal for int() 99 105 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) 103 107 104 108 def upper(value): 105 109 "Converts a string into all uppercase" 106 return value.upper()110 return str(value).decode(DEFAULT_CHARSET).upper().encode(DEFAULT_CHARSET) 107 111 108 112 def urlencode(value): 109 113 "Escapes a value for use in a URL" … … 136 140 Argument: number of words to wrap the text at. 137 141 """ 138 142 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) 140 144 141 145 def ljust(value, arg): 142 146 """ … … 144 148 145 149 Argument: field size 146 150 """ 147 return str(value). ljust(int(arg))151 return str(value).decode(DEFAULT_CHARSET).ljust(int(arg)).encode(DEFAULT_CHARSET) 148 152 149 153 def rjust(value, arg): 150 154 """ … … 152 156 153 157 Argument: field size 154 158 """ 155 return str(value). rjust(int(arg))159 return str(value).decode(DEFAULT_CHARSET).rjust(int(arg)).encode(DEFAULT_CHARSET) 156 160 157 161 def center(value, arg): 158 162 "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) 160 164 161 165 def cut(value, arg): 162 166 "Removes all values of arg from the given string" … … 236 240 237 241 def length(value): 238 242 "Returns the length of the value - useful for lists" 243 if isinstance(value,str): 244 value=value.decode(DEFAULT_CHARSET) 239 245 return len(value) 240 246 241 247 def length_is(value, arg): 242 248 "Returns a boolean of whether the value's length is the argument" 249 if isinstance(value,str): 250 value=value.decode(DEFAULT_CHARSET) 243 251 return len(value) == int(arg) 244 252 245 253 def random(value):