Ticket #924: 924.3.diff
File 924.3.diff, 3.5 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 from django.utils.translation import gettext 6 6 import re 7 7 import random as random_module … … 19 19 20 20 def capfirst(value): 21 21 "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) 24 28 25 29 def fix_ampersands(value): 26 30 "Replaces ampersands with ``&`` entities" … … 54 58 55 59 def lower(value): 56 60 "Converts a string into all lowercase" 57 return value.lower()61 return str(value).decode(DEFAULT_CHARSET).lower().encode(DEFAULT_CHARSET) 58 62 59 63 def make_list(value): 60 64 """ … … 84 88 85 89 def title(value): 86 90 "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) 88 92 89 93 def truncatewords(value, arg): 90 94 """ … … 97 101 length = int(arg) 98 102 except ValueError: # invalid literal for int() 99 103 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) 103 105 104 106 def upper(value): 105 107 "Converts a string into all uppercase" 106 return value.upper()108 return str(value).decode(DEFAULT_CHARSET).upper().encode(DEFAULT_CHARSET) 107 109 108 110 def urlencode(value): 109 111 "Escapes a value for use in a URL" … … 136 138 Argument: number of words to wrap the text at. 137 139 """ 138 140 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) 140 142 141 143 def ljust(value, arg): 142 144 """ … … 144 146 145 147 Argument: field size 146 148 """ 147 return str(value). ljust(int(arg))149 return str(value).decode(DEFAULT_CHARSET).ljust(int(arg)).encode(DEFAULT_CHARSET) 148 150 149 151 def rjust(value, arg): 150 152 """ … … 152 154 153 155 Argument: field size 154 156 """ 155 return str(value). rjust(int(arg))157 return str(value).decode(DEFAULT_CHARSET).rjust(int(arg)).encode(DEFAULT_CHARSET) 156 158 157 159 def center(value, arg): 158 160 "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) 160 162 161 163 def cut(value, arg): 162 164 "Removes all values of arg from the given string" … … 236 238 237 239 def length(value): 238 240 "Returns the length of the value - useful for lists" 241 if isinstance(value,str): 242 value=value.decode(DEFAULT_CHARSET) 239 243 return len(value) 240 244 241 245 def length_is(value, arg): 242 246 "Returns a boolean of whether the value's length is the argument" 247 if isinstance(value,str): 248 value=value.decode(DEFAULT_CHARSET) 243 249 return len(value) == int(arg) 244 250 245 251 def random(value):