2005-07-13 09:25:57 +08:00
|
|
|
"Default variable filters"
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.template import resolve_variable, Library
|
|
|
|
from django.conf import settings
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext, ungettext
|
|
|
|
from django.utils.encoding import force_unicode, smart_str, iri_to_uri
|
2005-10-15 04:10:13 +08:00
|
|
|
import re
|
2005-08-19 22:59:09 +08:00
|
|
|
import random as random_module
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
register = Library()
|
|
|
|
|
2007-02-24 02:02:51 +08:00
|
|
|
#######################
|
|
|
|
# STRING DECORATOR #
|
|
|
|
#######################
|
|
|
|
|
|
|
|
def stringfilter(func):
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Decorator for filters which should only receive unicode objects. The object
|
|
|
|
passed as the first positional argument will be converted to a unicode
|
|
|
|
object.
|
2007-02-24 02:02:51 +08:00
|
|
|
"""
|
|
|
|
def _dec(*args, **kwargs):
|
|
|
|
if args:
|
|
|
|
args = list(args)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
args[0] = force_unicode(args[0])
|
2007-02-24 02:02:51 +08:00
|
|
|
return func(*args, **kwargs)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2007-02-24 02:02:51 +08:00
|
|
|
# Include a reference to the real function (used to check original
|
|
|
|
# arguments by the template parser).
|
|
|
|
_dec._decorated_function = getattr(func, '_decorated_function', func)
|
|
|
|
return _dec
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
###################
|
|
|
|
# STRINGS #
|
|
|
|
###################
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
|
|
|
|
def addslashes(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Adds slashes - useful for passing strings to JavaScript, for example."
|
2006-09-23 16:41:09 +08:00
|
|
|
return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'")
|
2007-02-24 02:02:51 +08:00
|
|
|
addslashes = stringfilter(addslashes)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def capfirst(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Capitalizes the first character of the value"
|
|
|
|
return value and value[0].upper() + value[1:]
|
2007-02-24 02:02:51 +08:00
|
|
|
capfirst = stringfilter(capfirst)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def fix_ampersands(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Replaces ampersands with ``&`` entities"
|
|
|
|
from django.utils.html import fix_ampersands
|
|
|
|
return fix_ampersands(value)
|
2007-02-24 02:02:51 +08:00
|
|
|
fix_ampersands = stringfilter(fix_ampersands)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-01-03 13:29:34 +08:00
|
|
|
def floatformat(text, arg=-1):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-01-03 13:29:34 +08:00
|
|
|
If called without an argument, displays a floating point
|
|
|
|
number as 34.2 -- but only if there's a point to be displayed.
|
|
|
|
With a positive numeric argument, it displays that many decimal places
|
|
|
|
always.
|
|
|
|
With a negative numeric argument, it will display that many decimal
|
|
|
|
places -- but only if there's places to be displayed.
|
|
|
|
Examples:
|
2007-03-10 16:11:28 +08:00
|
|
|
|
|
|
|
* num1 = 34.23234
|
|
|
|
* num2 = 34.00000
|
|
|
|
* num1|floatformat results in 34.2
|
|
|
|
* num2|floatformat is 34
|
|
|
|
* num1|floatformat:3 is 34.232
|
|
|
|
* num2|floatformat:3 is 34.000
|
|
|
|
* num1|floatformat:-3 is 34.232
|
|
|
|
* num2|floatformat:-3 is 34
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2005-11-03 03:01:27 +08:00
|
|
|
try:
|
|
|
|
f = float(text)
|
|
|
|
except ValueError:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2007-01-03 13:29:34 +08:00
|
|
|
try:
|
|
|
|
d = int(arg)
|
|
|
|
except ValueError:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return force_unicode(f)
|
2005-10-25 09:44:14 +08:00
|
|
|
m = f - int(f)
|
2007-01-03 13:29:34 +08:00
|
|
|
if not m and d < 0:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'%d' % int(f)
|
2007-01-03 13:29:34 +08:00
|
|
|
else:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
formatstr = u'%%.%df' % abs(d)
|
2007-01-03 13:29:34 +08:00
|
|
|
return formatstr % f
|
2005-07-13 09:25:57 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def iriencode(value):
|
|
|
|
"Escapes an IRI value for use in a URL"
|
|
|
|
return force_unicode(iri_to_uri(value))
|
|
|
|
iriencode = stringfilter(iriencode)
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def linenumbers(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Displays text with line numbers"
|
|
|
|
from django.utils.html import escape
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
lines = value.split(u'\n')
|
2005-07-13 09:25:57 +08:00
|
|
|
# Find the maximum width of the line count, for use with zero padding string format command
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
width = unicode(len(unicode(len(lines))))
|
2005-07-13 09:25:57 +08:00
|
|
|
for i, line in enumerate(lines):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
lines[i] = (u"%0" + width + u"d. %s") % (i + 1, escape(line))
|
|
|
|
return u'\n'.join(lines)
|
2007-02-24 02:02:51 +08:00
|
|
|
linenumbers = stringfilter(linenumbers)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def lower(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Converts a string into all lowercase"
|
|
|
|
return value.lower()
|
2007-02-24 02:02:51 +08:00
|
|
|
lower = stringfilter(lower)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def make_list(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Returns the value turned into a list. For an integer, it's a list of
|
|
|
|
digits. For a string, it's a list of characters.
|
|
|
|
"""
|
2007-02-24 02:02:51 +08:00
|
|
|
return list(value)
|
|
|
|
make_list = stringfilter(make_list)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def slugify(value):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
"""
|
|
|
|
Normalizes string, converts to lowercase, removes non-alpha chars and
|
|
|
|
converts spaces to hyphens.
|
|
|
|
"""
|
|
|
|
import unicodedata
|
|
|
|
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
|
|
|
|
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
|
2006-05-15 12:20:04 +08:00
|
|
|
return re.sub('[-\s]+', '-', value)
|
2007-02-24 02:02:51 +08:00
|
|
|
slugify = stringfilter(slugify)
|
2005-08-10 23:40:14 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def stringformat(value, arg):
|
|
|
|
"""
|
|
|
|
Formats the variable according to the argument, a string formatting specifier.
|
|
|
|
This specifier uses Python string formating syntax, with the exception that
|
|
|
|
the leading "%" is dropped.
|
2005-08-10 23:40:14 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
See http://docs.python.org/lib/typesseq-strings.html for documentation
|
|
|
|
of Python string formatting
|
|
|
|
"""
|
|
|
|
try:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return (u"%" + unicode(arg)) % value
|
2005-07-13 09:25:57 +08:00
|
|
|
except (ValueError, TypeError):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u""
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def title(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Converts a string into titlecase"
|
|
|
|
return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title())
|
2007-02-24 02:02:51 +08:00
|
|
|
title = stringfilter(title)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def truncatewords(value, arg):
|
|
|
|
"""
|
|
|
|
Truncates a string after a certain number of words
|
|
|
|
|
|
|
|
Argument: Number of words to truncate after
|
|
|
|
"""
|
|
|
|
from django.utils.text import truncate_words
|
|
|
|
try:
|
|
|
|
length = int(arg)
|
|
|
|
except ValueError: # invalid literal for int()
|
|
|
|
return value # Fail silently.
|
|
|
|
return truncate_words(value, length)
|
2007-02-24 02:02:51 +08:00
|
|
|
truncatewords = stringfilter(truncatewords)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-02-10 10:51:27 +08:00
|
|
|
def truncatewords_html(value, arg):
|
|
|
|
"""
|
|
|
|
Truncates HTML after a certain number of words
|
|
|
|
|
|
|
|
Argument: Number of words to truncate after
|
|
|
|
"""
|
|
|
|
from django.utils.text import truncate_html_words
|
|
|
|
try:
|
|
|
|
length = int(arg)
|
|
|
|
except ValueError: # invalid literal for int()
|
|
|
|
return value # Fail silently.
|
|
|
|
return truncate_html_words(value, length)
|
2007-02-24 02:02:51 +08:00
|
|
|
truncatewords_html = stringfilter(truncatewords_html)
|
2007-02-10 10:51:27 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def upper(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Converts a string into all uppercase"
|
|
|
|
return value.upper()
|
2007-02-24 02:02:51 +08:00
|
|
|
upper = stringfilter(upper)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def urlencode(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Escapes a value for use in a URL"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.http import urlquote
|
|
|
|
return urlquote(value)
|
2007-02-24 02:02:51 +08:00
|
|
|
urlencode = stringfilter(urlencode)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def urlize(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Converts URLs in plain text into clickable links"
|
|
|
|
from django.utils.html import urlize
|
|
|
|
return urlize(value, nofollow=True)
|
2007-02-24 02:02:51 +08:00
|
|
|
urlize = stringfilter(urlize)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def urlizetrunc(value, limit):
|
|
|
|
"""
|
2006-01-02 02:37:33 +08:00
|
|
|
Converts URLs into clickable links, truncating URLs to the given character limit,
|
|
|
|
and adding 'rel=nofollow' attribute to discourage spamming.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
Argument: Length to truncate URLs to.
|
|
|
|
"""
|
|
|
|
from django.utils.html import urlize
|
|
|
|
return urlize(value, trim_url_limit=int(limit), nofollow=True)
|
2007-02-24 02:02:51 +08:00
|
|
|
urlizetrunc = stringfilter(urlizetrunc)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def wordcount(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Returns the number of words"
|
|
|
|
return len(value.split())
|
2007-02-24 02:02:51 +08:00
|
|
|
wordcount = stringfilter(wordcount)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def wordwrap(value, arg):
|
|
|
|
"""
|
|
|
|
Wraps words at specified line length
|
|
|
|
|
2006-07-15 19:47:09 +08:00
|
|
|
Argument: number of characters to wrap the text at.
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
from django.utils.text import wrap
|
2007-02-24 02:02:51 +08:00
|
|
|
return wrap(value, int(arg))
|
|
|
|
wordwrap = stringfilter(wordwrap)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def ljust(value, arg):
|
|
|
|
"""
|
|
|
|
Left-aligns the value in a field of a given width
|
|
|
|
|
|
|
|
Argument: field size
|
|
|
|
"""
|
2007-02-24 02:02:51 +08:00
|
|
|
return value.ljust(int(arg))
|
|
|
|
ljust = stringfilter(ljust)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def rjust(value, arg):
|
|
|
|
"""
|
|
|
|
Right-aligns the value in a field of a given width
|
|
|
|
|
|
|
|
Argument: field size
|
|
|
|
"""
|
2007-02-24 02:02:51 +08:00
|
|
|
return value.rjust(int(arg))
|
|
|
|
rjust = stringfilter(rjust)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def center(value, arg):
|
|
|
|
"Centers the value in a field of a given width"
|
2007-02-24 02:02:51 +08:00
|
|
|
return value.center(int(arg))
|
|
|
|
center = stringfilter(center)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def cut(value, arg):
|
|
|
|
"Removes all values of arg from the given string"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return value.replace(arg, u'')
|
2007-02-24 02:02:51 +08:00
|
|
|
cut = stringfilter(cut)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
###################
|
|
|
|
# HTML STRINGS #
|
|
|
|
###################
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def escape(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Escapes a string's HTML"
|
|
|
|
from django.utils.html import escape
|
|
|
|
return escape(value)
|
2007-02-24 02:02:51 +08:00
|
|
|
escape = stringfilter(escape)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def linebreaks(value):
|
2007-09-15 05:46:38 +08:00
|
|
|
"""
|
|
|
|
Replaces line breaks in plain text with appropriate HTML; a single
|
|
|
|
newline becomes an HTML line break (``<br />``) and a new line
|
|
|
|
followed by a blank line becomes a paragraph break (``</p>``).
|
|
|
|
"""
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.utils.html import linebreaks
|
|
|
|
return linebreaks(value)
|
2007-02-24 02:02:51 +08:00
|
|
|
linebreaks = stringfilter(linebreaks)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def linebreaksbr(value):
|
2007-09-15 05:46:38 +08:00
|
|
|
"""
|
|
|
|
Converts all newlines in a piece of plain text to HTML line breaks
|
|
|
|
(``<br />``).
|
|
|
|
"""
|
2005-07-13 09:25:57 +08:00
|
|
|
return value.replace('\n', '<br />')
|
2007-02-24 02:02:51 +08:00
|
|
|
linebreaksbr = stringfilter(linebreaksbr)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def removetags(value, tags):
|
|
|
|
"Removes a space separated list of [X]HTML tags from the output"
|
|
|
|
tags = [re.escape(tag) for tag in tags.split()]
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
tags_re = u'(%s)' % u'|'.join(tags)
|
|
|
|
starttag_re = re.compile(ur'<%s(/?>|(\s+[^>]*>))' % tags_re, re.U)
|
|
|
|
endtag_re = re.compile(u'</%s>' % tags_re)
|
|
|
|
value = starttag_re.sub(u'', value)
|
|
|
|
value = endtag_re.sub(u'', value)
|
2005-07-13 09:25:57 +08:00
|
|
|
return value
|
2007-02-24 02:02:51 +08:00
|
|
|
removetags = stringfilter(removetags)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def striptags(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Strips all [X]HTML tags"
|
|
|
|
from django.utils.html import strip_tags
|
|
|
|
return strip_tags(value)
|
2007-02-24 02:02:51 +08:00
|
|
|
striptags = stringfilter(striptags)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
###################
|
|
|
|
# LISTS #
|
|
|
|
###################
|
|
|
|
|
|
|
|
def dictsort(value, arg):
|
|
|
|
"""
|
|
|
|
Takes a list of dicts, returns that list sorted by the property given in
|
|
|
|
the argument.
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
decorated = [(resolve_variable(u'var.' + arg, {u'var' : item}), item) for item in value]
|
2005-07-13 09:25:57 +08:00
|
|
|
decorated.sort()
|
|
|
|
return [item[1] for item in decorated]
|
|
|
|
|
|
|
|
def dictsortreversed(value, arg):
|
|
|
|
"""
|
|
|
|
Takes a list of dicts, returns that list sorted in reverse order by the
|
|
|
|
property given in the argument.
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
decorated = [(resolve_variable(u'var.' + arg, {u'var' : item}), item) for item in value]
|
2005-07-13 09:25:57 +08:00
|
|
|
decorated.sort()
|
|
|
|
decorated.reverse()
|
|
|
|
return [item[1] for item in decorated]
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def first(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Returns the first item in a list"
|
|
|
|
try:
|
|
|
|
return value[0]
|
|
|
|
except IndexError:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def join(value, arg):
|
|
|
|
"Joins a list with a string, like Python's ``str.join(list)``"
|
|
|
|
try:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return arg.join(map(force_unicode, value))
|
2005-07-13 09:25:57 +08:00
|
|
|
except AttributeError: # fail silently but nicely
|
|
|
|
return value
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def length(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Returns the length of the value - useful for lists"
|
|
|
|
return len(value)
|
|
|
|
|
|
|
|
def length_is(value, arg):
|
|
|
|
"Returns a boolean of whether the value's length is the argument"
|
|
|
|
return len(value) == int(arg)
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def random(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Returns a random item from the list"
|
2005-08-19 22:59:09 +08:00
|
|
|
return random_module.choice(value)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def slice_(value, arg):
|
|
|
|
"""
|
|
|
|
Returns a slice of the list.
|
|
|
|
|
|
|
|
Uses the same syntax as Python's list slicing; see
|
|
|
|
http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
|
|
|
|
for an introduction.
|
|
|
|
"""
|
|
|
|
try:
|
2006-01-02 02:37:33 +08:00
|
|
|
bits = []
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
for x in arg.split(u':'):
|
2006-01-02 02:37:33 +08:00
|
|
|
if len(x) == 0:
|
|
|
|
bits.append(None)
|
|
|
|
else:
|
|
|
|
bits.append(int(x))
|
|
|
|
return value[slice(*bits)]
|
|
|
|
|
2005-09-23 08:12:24 +08:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
return value # Fail silently.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def unordered_list(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Recursively takes a self-nested list and returns an HTML unordered list --
|
|
|
|
WITHOUT opening and closing <ul> tags.
|
|
|
|
|
2007-08-26 09:11:20 +08:00
|
|
|
The list is assumed to be in the proper format. For example, if ``var``
|
|
|
|
contains: ``['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]``,
|
2005-07-13 09:25:57 +08:00
|
|
|
then ``{{ var|unordered_list }}`` would return::
|
|
|
|
|
|
|
|
<li>States
|
|
|
|
<ul>
|
|
|
|
<li>Kansas
|
|
|
|
<ul>
|
|
|
|
<li>Lawrence</li>
|
|
|
|
<li>Topeka</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li>Illinois</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
"""
|
2007-08-26 09:11:20 +08:00
|
|
|
def convert_old_style_list(list_):
|
|
|
|
"""
|
|
|
|
Converts old style lists to the new easier to understand format.
|
|
|
|
|
|
|
|
The old list format looked like:
|
|
|
|
['Item 1', [['Item 1.1', []], ['Item 1.2', []]]
|
|
|
|
|
|
|
|
And it is converted to:
|
|
|
|
['Item 1', ['Item 1.1', 'Item 1.2]]
|
|
|
|
"""
|
|
|
|
if not isinstance(list_, (tuple, list)) or len(list_) != 2:
|
|
|
|
return list_, False
|
|
|
|
first_item, second_item = list_
|
|
|
|
if second_item == []:
|
|
|
|
return [first_item], True
|
|
|
|
old_style_list = True
|
|
|
|
new_second_item = []
|
|
|
|
for sublist in second_item:
|
|
|
|
item, old_style_list = convert_old_style_list(sublist)
|
|
|
|
if not old_style_list:
|
|
|
|
break
|
|
|
|
new_second_item.extend(item)
|
|
|
|
if old_style_list:
|
|
|
|
second_item = new_second_item
|
|
|
|
return [first_item, second_item], old_style_list
|
|
|
|
def _helper(list_, tabs=1):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
indent = u'\t' * tabs
|
2007-08-26 09:11:20 +08:00
|
|
|
output = []
|
|
|
|
|
|
|
|
list_length = len(list_)
|
|
|
|
i = 0
|
|
|
|
while i < list_length:
|
|
|
|
title = list_[i]
|
|
|
|
sublist = ''
|
|
|
|
sublist_item = None
|
|
|
|
if isinstance(title, (list, tuple)):
|
|
|
|
sublist_item = title
|
|
|
|
title = ''
|
|
|
|
elif i < list_length - 1:
|
|
|
|
next_item = list_[i+1]
|
|
|
|
if next_item and isinstance(next_item, (list, tuple)):
|
|
|
|
# The next item is a sub-list.
|
|
|
|
sublist_item = next_item
|
|
|
|
# We've processed the next item now too.
|
|
|
|
i += 1
|
|
|
|
if sublist_item:
|
|
|
|
sublist = _helper(sublist_item, tabs+1)
|
|
|
|
sublist = '\n%s<ul>\n%s\n%s</ul>\n%s' % (indent, sublist,
|
|
|
|
indent, indent)
|
|
|
|
output.append('%s<li>%s%s</li>' % (indent, force_unicode(title),
|
|
|
|
sublist))
|
|
|
|
i += 1
|
|
|
|
return '\n'.join(output)
|
|
|
|
value, converted = convert_old_style_list(value)
|
|
|
|
return _helper(value)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
###################
|
|
|
|
# INTEGERS #
|
|
|
|
###################
|
|
|
|
|
|
|
|
def add(value, arg):
|
|
|
|
"Adds the arg to the value"
|
|
|
|
return int(value) + int(arg)
|
|
|
|
|
|
|
|
def get_digit(value, arg):
|
|
|
|
"""
|
|
|
|
Given a whole number, returns the requested digit of it, where 1 is the
|
|
|
|
right-most digit, 2 is the second-right-most digit, etc. Returns the
|
|
|
|
original value for invalid input (if input or argument is not an integer,
|
|
|
|
or if argument is less than 1). Otherwise, output is always an integer.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
arg = int(arg)
|
|
|
|
value = int(value)
|
|
|
|
except ValueError:
|
|
|
|
return value # Fail silently for an invalid argument
|
|
|
|
if arg < 1:
|
|
|
|
return value
|
|
|
|
try:
|
|
|
|
return int(str(value)[-arg])
|
|
|
|
except IndexError:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
###################
|
|
|
|
# DATES #
|
|
|
|
###################
|
|
|
|
|
2006-05-17 05:28:06 +08:00
|
|
|
def date(value, arg=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Formats a date according to the given format"
|
|
|
|
from django.utils.dateformat import format
|
2006-06-11 08:33:44 +08:00
|
|
|
if not value:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2006-05-17 05:28:06 +08:00
|
|
|
if arg is None:
|
|
|
|
arg = settings.DATE_FORMAT
|
2005-07-13 09:25:57 +08:00
|
|
|
return format(value, arg)
|
|
|
|
|
2006-05-17 05:28:06 +08:00
|
|
|
def time(value, arg=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Formats a time according to the given format"
|
|
|
|
from django.utils.dateformat import time_format
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
if value in (None, u''):
|
|
|
|
return u''
|
2006-05-17 05:28:06 +08:00
|
|
|
if arg is None:
|
|
|
|
arg = settings.TIME_FORMAT
|
2005-07-13 09:25:57 +08:00
|
|
|
return time_format(value, arg)
|
|
|
|
|
2006-06-21 14:56:08 +08:00
|
|
|
def timesince(value, arg=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
'Formats a date as the time since that date (i.e. "4 days, 6 hours")'
|
|
|
|
from django.utils.timesince import timesince
|
2006-06-11 08:33:44 +08:00
|
|
|
if not value:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2006-06-21 14:56:08 +08:00
|
|
|
if arg:
|
|
|
|
return timesince(arg, value)
|
2005-07-13 09:25:57 +08:00
|
|
|
return timesince(value)
|
|
|
|
|
2006-06-21 14:56:08 +08:00
|
|
|
def timeuntil(value, arg=None):
|
|
|
|
'Formats a date as the time until that date (i.e. "4 days, 6 hours")'
|
|
|
|
from django.utils.timesince import timesince
|
|
|
|
from datetime import datetime
|
|
|
|
if not value:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2006-06-21 14:56:08 +08:00
|
|
|
if arg:
|
|
|
|
return timesince(arg, value)
|
|
|
|
return timesince(datetime.now(), value)
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
###################
|
|
|
|
# LOGIC #
|
|
|
|
###################
|
|
|
|
|
|
|
|
def default(value, arg):
|
|
|
|
"If value is unavailable, use given default"
|
|
|
|
return value or arg
|
|
|
|
|
2005-10-14 11:48:27 +08:00
|
|
|
def default_if_none(value, arg):
|
|
|
|
"If value is None, use given default"
|
|
|
|
if value is None:
|
|
|
|
return arg
|
|
|
|
return value
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def divisibleby(value, arg):
|
|
|
|
"Returns true if the value is devisible by the argument"
|
|
|
|
return int(value) % int(arg) == 0
|
|
|
|
|
2005-12-15 23:56:26 +08:00
|
|
|
def yesno(value, arg=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Given a string mapping values for true, false and (optionally) None,
|
|
|
|
returns one of those strings accoding to the value:
|
|
|
|
|
|
|
|
========== ====================== ==================================
|
|
|
|
Value Argument Outputs
|
|
|
|
========== ====================== ==================================
|
|
|
|
``True`` ``"yeah,no,maybe"`` ``yeah``
|
|
|
|
``False`` ``"yeah,no,maybe"`` ``no``
|
|
|
|
``None`` ``"yeah,no,maybe"`` ``maybe``
|
|
|
|
``None`` ``"yeah,no"`` ``"no"`` (converts None to False
|
|
|
|
if no mapping for None is given.
|
|
|
|
========== ====================== ==================================
|
2005-12-15 23:56:26 +08:00
|
|
|
"""
|
|
|
|
if arg is None:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
arg = ugettext('yes,no,maybe')
|
|
|
|
bits = arg.split(u',')
|
2005-07-13 09:25:57 +08:00
|
|
|
if len(bits) < 2:
|
|
|
|
return value # Invalid arg.
|
|
|
|
try:
|
|
|
|
yes, no, maybe = bits
|
|
|
|
except ValueError: # unpack list of wrong size (no "maybe" value provided)
|
2005-09-01 10:22:46 +08:00
|
|
|
yes, no, maybe = bits[0], bits[1], bits[1]
|
2005-07-13 09:25:57 +08:00
|
|
|
if value is None:
|
|
|
|
return maybe
|
|
|
|
if value:
|
|
|
|
return yes
|
|
|
|
return no
|
|
|
|
|
|
|
|
###################
|
|
|
|
# MISC #
|
|
|
|
###################
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def filesizeformat(bytes):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102
|
|
|
|
bytes, etc).
|
|
|
|
"""
|
2006-11-07 12:58:10 +08:00
|
|
|
try:
|
|
|
|
bytes = float(bytes)
|
|
|
|
except TypeError:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u"0 bytes"
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
if bytes < 1024:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
|
2005-07-13 09:25:57 +08:00
|
|
|
if bytes < 1024 * 1024:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return ugettext("%.1f KB") % (bytes / 1024)
|
2005-07-13 09:25:57 +08:00
|
|
|
if bytes < 1024 * 1024 * 1024:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return ugettext("%.1f MB") % (bytes / (1024 * 1024))
|
|
|
|
return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024))
|
2005-07-13 09:25:57 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def pluralize(value, arg=u's'):
|
2006-07-04 14:18:39 +08:00
|
|
|
"""
|
|
|
|
Returns a plural suffix if the value is not 1, for '1 vote' vs. '2 votes'
|
|
|
|
By default, 's' is used as a suffix; if an argument is provided, that string
|
|
|
|
is used instead. If the provided argument contains a comma, the text before
|
|
|
|
the comma is used for the singular case.
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
if not u',' in arg:
|
|
|
|
arg = u',' + arg
|
|
|
|
bits = arg.split(u',')
|
2006-07-04 14:31:26 +08:00
|
|
|
if len(bits) > 2:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u''
|
2006-07-04 14:31:26 +08:00
|
|
|
singular_suffix, plural_suffix = bits[:2]
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
|
|
|
if int(value) != 1:
|
2006-07-04 14:18:39 +08:00
|
|
|
return plural_suffix
|
2005-07-13 09:25:57 +08:00
|
|
|
except ValueError: # invalid string that's not a number
|
|
|
|
pass
|
|
|
|
except TypeError: # value isn't a string or a number; maybe it's a list?
|
|
|
|
try:
|
|
|
|
if len(value) != 1:
|
2006-07-04 14:18:39 +08:00
|
|
|
return plural_suffix
|
2005-07-13 09:25:57 +08:00
|
|
|
except TypeError: # len() of unsized object
|
|
|
|
pass
|
2006-07-04 14:18:39 +08:00
|
|
|
return singular_suffix
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def phone2numeric(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Takes a phone number and converts it in to its numerical equivalent"
|
|
|
|
from django.utils.text import phone2numeric
|
|
|
|
return phone2numeric(value)
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def pprint(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"A wrapper around pprint.pprint -- for debugging, really"
|
|
|
|
from pprint import pformat
|
2006-05-02 09:31:56 +08:00
|
|
|
try:
|
|
|
|
return pformat(value)
|
|
|
|
except Exception, e:
|
2007-07-13 18:56:30 +08:00
|
|
|
return u"Error in formatting: %s" % force_unicode(e, errors="replace")
|
2006-05-17 05:28:06 +08:00
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
# Syntax: register.filter(name of filter, callback)
|
|
|
|
register.filter(add)
|
|
|
|
register.filter(addslashes)
|
|
|
|
register.filter(capfirst)
|
|
|
|
register.filter(center)
|
|
|
|
register.filter(cut)
|
|
|
|
register.filter(date)
|
|
|
|
register.filter(default)
|
|
|
|
register.filter(default_if_none)
|
|
|
|
register.filter(dictsort)
|
|
|
|
register.filter(dictsortreversed)
|
|
|
|
register.filter(divisibleby)
|
|
|
|
register.filter(escape)
|
|
|
|
register.filter(filesizeformat)
|
|
|
|
register.filter(first)
|
|
|
|
register.filter(fix_ampersands)
|
|
|
|
register.filter(floatformat)
|
|
|
|
register.filter(get_digit)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
register.filter(iriencode)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(join)
|
|
|
|
register.filter(length)
|
|
|
|
register.filter(length_is)
|
|
|
|
register.filter(linebreaks)
|
|
|
|
register.filter(linebreaksbr)
|
|
|
|
register.filter(linenumbers)
|
|
|
|
register.filter(ljust)
|
|
|
|
register.filter(lower)
|
|
|
|
register.filter(make_list)
|
|
|
|
register.filter(phone2numeric)
|
|
|
|
register.filter(pluralize)
|
|
|
|
register.filter(pprint)
|
|
|
|
register.filter(removetags)
|
|
|
|
register.filter(random)
|
|
|
|
register.filter(rjust)
|
2005-11-27 09:55:55 +08:00
|
|
|
register.filter('slice', slice_)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(slugify)
|
|
|
|
register.filter(stringformat)
|
|
|
|
register.filter(striptags)
|
|
|
|
register.filter(time)
|
|
|
|
register.filter(timesince)
|
2006-06-21 14:56:08 +08:00
|
|
|
register.filter(timeuntil)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(title)
|
|
|
|
register.filter(truncatewords)
|
2007-02-10 10:51:27 +08:00
|
|
|
register.filter(truncatewords_html)
|
2005-11-27 06:46:31 +08:00
|
|
|
register.filter(unordered_list)
|
|
|
|
register.filter(upper)
|
|
|
|
register.filter(urlencode)
|
|
|
|
register.filter(urlize)
|
|
|
|
register.filter(urlizetrunc)
|
|
|
|
register.filter(wordcount)
|
|
|
|
register.filter(wordwrap)
|
2005-12-15 23:56:26 +08:00
|
|
|
register.filter(yesno)
|