1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

unicode: Made various changes to prevent actual and potential Python 2.3

compatibility problems. Refs #3582.


git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5223 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-05-14 07:04:31 +00:00
parent 28f66bb097
commit 6c99a60e0d
5 changed files with 17 additions and 15 deletions

View File

@ -84,7 +84,7 @@ class Model(object):
return getattr(self, self._meta.pk.attname)
def __repr__(self):
return smart_str(u'<%s: %s>' % (self.__class__.__name__, self))
return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self)))
def __str__(self):
if hasattr(self, '__unicode__'):

View File

@ -18,10 +18,10 @@ class ErrorDict(dict):
def as_ul(self):
if not self: return u''
return u'<ul class="errorlist">%s</ul>' % ''.join([u'<li>%s%s</li>' % (k, v) for k, v in self.items()])
return u'<ul class="errorlist">%s</ul>' % ''.join([u'<li>%s%s</li>' % (k, smart_unicode(v)) for k, v in self.items()])
def as_text(self):
return u'\n'.join([u'* %s\n%s' % (k, u'\n'.join([u' * %s' % i for i in v])) for k, v in self.items()])
return u'\n'.join([u'* %s\n%s' % (k, u'\n'.join([u' * %s' % smart_unicode(i) for i in v])) for k, v in self.items()])
class ErrorList(list):
"""
@ -32,11 +32,11 @@ class ErrorList(list):
def as_ul(self):
if not self: return u''
return u'<ul class="errorlist">%s</ul>' % ''.join([u'<li>%s</li>' % e for e in self])
return u'<ul class="errorlist">%s</ul>' % ''.join([u'<li>%s</li>' % smart_unicode(e) for e in self])
def as_text(self):
if not self: return u''
return u'\n'.join([u'* %s' % e for e in self])
return u'\n'.join([u'* %s' % smart_unicode(e) for e in self])
class ValidationError(Exception):
def __init__(self, message):

View File

@ -253,7 +253,7 @@ class RadioFieldRenderer(StrAndUnicode):
def __unicode__(self):
"Outputs a <ul> for this set of radio fields."
return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>' % w for w in self])
return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>' % smart_unicode(w) for w in self])
class RadioSelect(Select):
def render(self, name, value, attrs=None, choices=()):

View File

@ -170,7 +170,7 @@ upper = stringfilter(upper)
def urlencode(value):
"Escapes a value for use in a URL"
import urllib
return urllib.quote(value).decode('utf-8')
return smart_unicode(urllib.quote(value))
urlencode = stringfilter(urlencode)
def urlize(value):
@ -364,10 +364,10 @@ def unordered_list(value):
def _helper(value, tabs):
indent = u'\t' * tabs
if value[1]:
return u'%s<li>%s\n%s<ul>\n%s\n%s</ul>\n%s</li>' % (indent, value[0], indent,
return u'%s<li>%s\n%s<ul>\n%s\n%s</ul>\n%s</li>' % (indent, smart_unicode(value[0]), indent,
u'\n'.join([_helper(v, tabs+1) for v in value[1]]), indent, indent)
else:
return u'%s<li>%s</li>' % (indent, value[0])
return u'%s<li>%s</li>' % (indent, smart_unicode(value[0]))
return _helper(value, 1)
###################
@ -546,7 +546,7 @@ def pprint(value):
try:
return pformat(value)
except Exception, e:
return u"Error in formatting:%s" % e
return u"Error in formatting:%s" % smart_unicode(e)
# Syntax: register.filter(name of filter, callback)
register.filter(add)

View File

@ -32,7 +32,7 @@ def linebreaks(value):
"Converts newlines into <p> and <br />s"
value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
paras = re.split('\n{2,}', value)
paras = ['<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]
paras = [u'<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]
return u'\n\n'.join(paras)
def strip_tags(value):
@ -53,14 +53,16 @@ def fix_ampersands(value):
def urlize(text, trim_url_limit=None, nofollow=False):
"""
Converts any URLs in text into clickable links. Works on http://, https:// and
www. links. Links can have trailing punctuation (periods, commas, close-parens)
and leading punctuation (opening parens) and it'll still do the right thing.
Converts any URLs in text into clickable links. Works on http://, https://
and www. links. Links can have trailing punctuation (periods, commas,
close-parens) and leading punctuation (opening parens) and it'll still do
the right thing.
If trim_url_limit is not None, the URLs in link text will be limited to
trim_url_limit characters.
If nofollow is True, the URLs in link text will get a rel="nofollow" attribute.
If nofollow is True, the URLs in link text will get a rel="nofollow"
attribute.
"""
trim_url = lambda x, limit=trim_url_limit: limit is not None and (x[:limit] + (len(x) >=limit and '...' or '')) or x
words = word_split_re.split(text)