Used the decorator syntax consistently to register template filters.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17052 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
345be05622
commit
52c66a21e1
|
@ -9,10 +9,9 @@ from django.utils.formats import number_format
|
||||||
from django.utils.translation import pgettext, ungettext, ugettext as _
|
from django.utils.translation import pgettext, ungettext, ugettext as _
|
||||||
from django.utils.tzinfo import LocalTimezone
|
from django.utils.tzinfo import LocalTimezone
|
||||||
|
|
||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def ordinal(value):
|
def ordinal(value):
|
||||||
"""
|
"""
|
||||||
Converts an integer to its ordinal as a string. 1 is '1st', 2 is '2nd',
|
Converts an integer to its ordinal as a string. 1 is '1st', 2 is '2nd',
|
||||||
|
@ -27,8 +26,8 @@ def ordinal(value):
|
||||||
return u"%d%s" % (value, suffixes[0])
|
return u"%d%s" % (value, suffixes[0])
|
||||||
return u"%d%s" % (value, suffixes[value % 10])
|
return u"%d%s" % (value, suffixes[value % 10])
|
||||||
ordinal.is_safe = True
|
ordinal.is_safe = True
|
||||||
register.filter(ordinal)
|
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def intcomma(value, use_l10n=True):
|
def intcomma(value, use_l10n=True):
|
||||||
"""
|
"""
|
||||||
Converts an integer to a string containing commas every three digits.
|
Converts an integer to a string containing commas every three digits.
|
||||||
|
@ -49,7 +48,6 @@ def intcomma(value, use_l10n=True):
|
||||||
else:
|
else:
|
||||||
return intcomma(new, use_l10n)
|
return intcomma(new, use_l10n)
|
||||||
intcomma.is_safe = True
|
intcomma.is_safe = True
|
||||||
register.filter(intcomma)
|
|
||||||
|
|
||||||
# A tuple of standard large number to their converters
|
# A tuple of standard large number to their converters
|
||||||
intword_converters = (
|
intword_converters = (
|
||||||
|
@ -99,6 +97,7 @@ intword_converters = (
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def intword(value):
|
def intword(value):
|
||||||
"""
|
"""
|
||||||
Converts a large integer to a friendly text representation. Works best
|
Converts a large integer to a friendly text representation. Works best
|
||||||
|
@ -131,8 +130,8 @@ def intword(value):
|
||||||
return _check_for_i18n(new_value, *converters(new_value))
|
return _check_for_i18n(new_value, *converters(new_value))
|
||||||
return value
|
return value
|
||||||
intword.is_safe = False
|
intword.is_safe = False
|
||||||
register.filter(intword)
|
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def apnumber(value):
|
def apnumber(value):
|
||||||
"""
|
"""
|
||||||
For numbers 1-9, returns the number spelled out. Otherwise, returns the
|
For numbers 1-9, returns the number spelled out. Otherwise, returns the
|
||||||
|
@ -146,7 +145,6 @@ def apnumber(value):
|
||||||
return value
|
return value
|
||||||
return (_('one'), _('two'), _('three'), _('four'), _('five'), _('six'), _('seven'), _('eight'), _('nine'))[value-1]
|
return (_('one'), _('two'), _('three'), _('four'), _('five'), _('six'), _('seven'), _('eight'), _('nine'))[value-1]
|
||||||
apnumber.is_safe = True
|
apnumber.is_safe = True
|
||||||
register.filter(apnumber)
|
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def naturalday(value, arg=None):
|
def naturalday(value, arg=None):
|
||||||
|
|
|
@ -18,6 +18,7 @@ from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def textile(value):
|
def textile(value):
|
||||||
try:
|
try:
|
||||||
import textile
|
import textile
|
||||||
|
@ -29,6 +30,7 @@ def textile(value):
|
||||||
return mark_safe(force_unicode(textile.textile(smart_str(value), encoding='utf-8', output='utf-8')))
|
return mark_safe(force_unicode(textile.textile(smart_str(value), encoding='utf-8', output='utf-8')))
|
||||||
textile.is_safe = True
|
textile.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def markdown(value, arg=''):
|
def markdown(value, arg=''):
|
||||||
"""
|
"""
|
||||||
Runs Markdown over a given value, optionally using various
|
Runs Markdown over a given value, optionally using various
|
||||||
|
@ -73,6 +75,7 @@ def markdown(value, arg=''):
|
||||||
return mark_safe(force_unicode(markdown.markdown(smart_str(value))))
|
return mark_safe(force_unicode(markdown.markdown(smart_str(value))))
|
||||||
markdown.is_safe = True
|
markdown.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def restructuredtext(value):
|
def restructuredtext(value):
|
||||||
try:
|
try:
|
||||||
from docutils.core import publish_parts
|
from docutils.core import publish_parts
|
||||||
|
@ -86,6 +89,3 @@ def restructuredtext(value):
|
||||||
return mark_safe(force_unicode(parts["fragment"]))
|
return mark_safe(force_unicode(parts["fragment"]))
|
||||||
restructuredtext.is_safe = True
|
restructuredtext.is_safe = True
|
||||||
|
|
||||||
register.filter(textile)
|
|
||||||
register.filter(markdown)
|
|
||||||
register.filter(restructuredtext)
|
|
||||||
|
|
|
@ -53,6 +53,7 @@ def stringfilter(func):
|
||||||
# STRINGS #
|
# STRINGS #
|
||||||
###################
|
###################
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def addslashes(value):
|
def addslashes(value):
|
||||||
"""
|
"""
|
||||||
|
@ -63,6 +64,7 @@ def addslashes(value):
|
||||||
return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'")
|
return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'")
|
||||||
addslashes.is_safe = True
|
addslashes.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def capfirst(value):
|
def capfirst(value):
|
||||||
"""Capitalizes the first character of the value."""
|
"""Capitalizes the first character of the value."""
|
||||||
|
@ -94,6 +96,7 @@ neg_inf = -1e200 * 1e200
|
||||||
nan = (1e200 * 1e200) // (1e200 * 1e200)
|
nan = (1e200 * 1e200) // (1e200 * 1e200)
|
||||||
special_floats = [str(pos_inf), str(neg_inf), str(nan)]
|
special_floats = [str(pos_inf), str(neg_inf), str(nan)]
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def floatformat(text, arg=-1):
|
def floatformat(text, arg=-1):
|
||||||
"""
|
"""
|
||||||
Displays a float to a specified number of decimal places.
|
Displays a float to a specified number of decimal places.
|
||||||
|
@ -171,12 +174,14 @@ def floatformat(text, arg=-1):
|
||||||
return input_val
|
return input_val
|
||||||
floatformat.is_safe = True
|
floatformat.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def iriencode(value):
|
def iriencode(value):
|
||||||
"""Escapes an IRI value for use in a URL."""
|
"""Escapes an IRI value for use in a URL."""
|
||||||
return force_unicode(iri_to_uri(value))
|
return force_unicode(iri_to_uri(value))
|
||||||
iriencode.is_safe = True
|
iriencode.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def linenumbers(value, autoescape=None):
|
def linenumbers(value, autoescape=None):
|
||||||
"""Displays text with line numbers."""
|
"""Displays text with line numbers."""
|
||||||
|
@ -194,12 +199,14 @@ def linenumbers(value, autoescape=None):
|
||||||
linenumbers.is_safe = True
|
linenumbers.is_safe = True
|
||||||
linenumbers.needs_autoescape = True
|
linenumbers.needs_autoescape = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def lower(value):
|
def lower(value):
|
||||||
"""Converts a string into all lowercase."""
|
"""Converts a string into all lowercase."""
|
||||||
return value.lower()
|
return value.lower()
|
||||||
lower.is_safe = True
|
lower.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def make_list(value):
|
def make_list(value):
|
||||||
"""
|
"""
|
||||||
|
@ -211,6 +218,7 @@ def make_list(value):
|
||||||
return list(value)
|
return list(value)
|
||||||
make_list.is_safe = False
|
make_list.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def slugify(value):
|
def slugify(value):
|
||||||
"""
|
"""
|
||||||
|
@ -222,6 +230,7 @@ def slugify(value):
|
||||||
return mark_safe(re.sub('[-\s]+', '-', value))
|
return mark_safe(re.sub('[-\s]+', '-', value))
|
||||||
slugify.is_safe = True
|
slugify.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def stringformat(value, arg):
|
def stringformat(value, arg):
|
||||||
"""
|
"""
|
||||||
Formats the variable according to the arg, a string formatting specifier.
|
Formats the variable according to the arg, a string formatting specifier.
|
||||||
|
@ -238,6 +247,7 @@ def stringformat(value, arg):
|
||||||
return u""
|
return u""
|
||||||
stringformat.is_safe = True
|
stringformat.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def title(value):
|
def title(value):
|
||||||
"""Converts a string into titlecase."""
|
"""Converts a string into titlecase."""
|
||||||
|
@ -245,6 +255,7 @@ def title(value):
|
||||||
return re.sub("\d([A-Z])", lambda m: m.group(0).lower(), t)
|
return re.sub("\d([A-Z])", lambda m: m.group(0).lower(), t)
|
||||||
title.is_safe = True
|
title.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def truncatechars(value, arg):
|
def truncatechars(value, arg):
|
||||||
"""
|
"""
|
||||||
|
@ -259,6 +270,7 @@ def truncatechars(value, arg):
|
||||||
return Truncator(value).chars(length)
|
return Truncator(value).chars(length)
|
||||||
truncatechars.is_safe = True
|
truncatechars.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def truncatewords(value, arg):
|
def truncatewords(value, arg):
|
||||||
"""
|
"""
|
||||||
|
@ -275,6 +287,7 @@ def truncatewords(value, arg):
|
||||||
return Truncator(value).words(length, truncate=' ...')
|
return Truncator(value).words(length, truncate=' ...')
|
||||||
truncatewords.is_safe = True
|
truncatewords.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def truncatewords_html(value, arg):
|
def truncatewords_html(value, arg):
|
||||||
"""
|
"""
|
||||||
|
@ -291,12 +304,14 @@ def truncatewords_html(value, arg):
|
||||||
return Truncator(value).words(length, html=True, truncate=' ...')
|
return Truncator(value).words(length, html=True, truncate=' ...')
|
||||||
truncatewords_html.is_safe = True
|
truncatewords_html.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def upper(value):
|
def upper(value):
|
||||||
"""Converts a string into all uppercase."""
|
"""Converts a string into all uppercase."""
|
||||||
return value.upper()
|
return value.upper()
|
||||||
upper.is_safe = False
|
upper.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def urlencode(value, safe=None):
|
def urlencode(value, safe=None):
|
||||||
"""
|
"""
|
||||||
|
@ -321,6 +336,7 @@ def urlize(value, autoescape=None):
|
||||||
urlize.is_safe = True
|
urlize.is_safe = True
|
||||||
urlize.needs_autoescape = True
|
urlize.needs_autoescape = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def urlizetrunc(value, limit, autoescape=None):
|
def urlizetrunc(value, limit, autoescape=None):
|
||||||
"""
|
"""
|
||||||
|
@ -334,12 +350,14 @@ def urlizetrunc(value, limit, autoescape=None):
|
||||||
urlizetrunc.is_safe = True
|
urlizetrunc.is_safe = True
|
||||||
urlizetrunc.needs_autoescape = True
|
urlizetrunc.needs_autoescape = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def wordcount(value):
|
def wordcount(value):
|
||||||
"""Returns the number of words."""
|
"""Returns the number of words."""
|
||||||
return len(value.split())
|
return len(value.split())
|
||||||
wordcount.is_safe = False
|
wordcount.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def wordwrap(value, arg):
|
def wordwrap(value, arg):
|
||||||
"""
|
"""
|
||||||
|
@ -350,6 +368,7 @@ def wordwrap(value, arg):
|
||||||
return wrap(value, int(arg))
|
return wrap(value, int(arg))
|
||||||
wordwrap.is_safe = True
|
wordwrap.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def ljust(value, arg):
|
def ljust(value, arg):
|
||||||
"""
|
"""
|
||||||
|
@ -360,6 +379,7 @@ def ljust(value, arg):
|
||||||
return value.ljust(int(arg))
|
return value.ljust(int(arg))
|
||||||
ljust.is_safe = True
|
ljust.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def rjust(value, arg):
|
def rjust(value, arg):
|
||||||
"""
|
"""
|
||||||
|
@ -370,12 +390,14 @@ def rjust(value, arg):
|
||||||
return value.rjust(int(arg))
|
return value.rjust(int(arg))
|
||||||
rjust.is_safe = True
|
rjust.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def center(value, arg):
|
def center(value, arg):
|
||||||
"""Centers the value in a field of a given width."""
|
"""Centers the value in a field of a given width."""
|
||||||
return value.center(int(arg))
|
return value.center(int(arg))
|
||||||
center.is_safe = True
|
center.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def cut(value, arg):
|
def cut(value, arg):
|
||||||
"""
|
"""
|
||||||
|
@ -400,6 +422,7 @@ def escape_filter(value):
|
||||||
return mark_for_escaping(value)
|
return mark_for_escaping(value)
|
||||||
escape_filter.is_safe = True
|
escape_filter.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def force_escape(value):
|
def force_escape(value):
|
||||||
"""
|
"""
|
||||||
|
@ -423,6 +446,7 @@ def linebreaks_filter(value, autoescape=None):
|
||||||
linebreaks_filter.is_safe = True
|
linebreaks_filter.is_safe = True
|
||||||
linebreaks_filter.needs_autoescape = True
|
linebreaks_filter.needs_autoescape = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def linebreaksbr(value, autoescape=None):
|
def linebreaksbr(value, autoescape=None):
|
||||||
"""
|
"""
|
||||||
|
@ -437,6 +461,7 @@ def linebreaksbr(value, autoescape=None):
|
||||||
linebreaksbr.is_safe = True
|
linebreaksbr.is_safe = True
|
||||||
linebreaksbr.needs_autoescape = True
|
linebreaksbr.needs_autoescape = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def safe(value):
|
def safe(value):
|
||||||
"""
|
"""
|
||||||
|
@ -445,6 +470,7 @@ def safe(value):
|
||||||
return mark_safe(value)
|
return mark_safe(value)
|
||||||
safe.is_safe = True
|
safe.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def safeseq(value):
|
def safeseq(value):
|
||||||
"""
|
"""
|
||||||
A "safe" filter for sequences. Marks each element in the sequence,
|
A "safe" filter for sequences. Marks each element in the sequence,
|
||||||
|
@ -454,6 +480,7 @@ def safeseq(value):
|
||||||
return [mark_safe(force_unicode(obj)) for obj in value]
|
return [mark_safe(force_unicode(obj)) for obj in value]
|
||||||
safeseq.is_safe = True
|
safeseq.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def removetags(value, tags):
|
def removetags(value, tags):
|
||||||
"""Removes a space separated list of [X]HTML tags from the output."""
|
"""Removes a space separated list of [X]HTML tags from the output."""
|
||||||
|
@ -466,6 +493,7 @@ def removetags(value, tags):
|
||||||
return value
|
return value
|
||||||
removetags.is_safe = True
|
removetags.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
@stringfilter
|
@stringfilter
|
||||||
def striptags(value):
|
def striptags(value):
|
||||||
"""Strips all [X]HTML tags."""
|
"""Strips all [X]HTML tags."""
|
||||||
|
@ -476,6 +504,7 @@ striptags.is_safe = True
|
||||||
# LISTS #
|
# LISTS #
|
||||||
###################
|
###################
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def dictsort(value, arg):
|
def dictsort(value, arg):
|
||||||
"""
|
"""
|
||||||
Takes a list of dicts, returns that list sorted by the property given in
|
Takes a list of dicts, returns that list sorted by the property given in
|
||||||
|
@ -484,6 +513,7 @@ def dictsort(value, arg):
|
||||||
return sorted(value, key=Variable(arg).resolve)
|
return sorted(value, key=Variable(arg).resolve)
|
||||||
dictsort.is_safe = False
|
dictsort.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def dictsortreversed(value, arg):
|
def dictsortreversed(value, arg):
|
||||||
"""
|
"""
|
||||||
Takes a list of dicts, returns that list sorted in reverse order by the
|
Takes a list of dicts, returns that list sorted in reverse order by the
|
||||||
|
@ -492,6 +522,7 @@ def dictsortreversed(value, arg):
|
||||||
return sorted(value, key=Variable(arg).resolve, reverse=True)
|
return sorted(value, key=Variable(arg).resolve, reverse=True)
|
||||||
dictsortreversed.is_safe = False
|
dictsortreversed.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def first(value):
|
def first(value):
|
||||||
"""Returns the first item in a list."""
|
"""Returns the first item in a list."""
|
||||||
try:
|
try:
|
||||||
|
@ -500,6 +531,7 @@ def first(value):
|
||||||
return u''
|
return u''
|
||||||
first.is_safe = False
|
first.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def join(value, arg, autoescape=None):
|
def join(value, arg, autoescape=None):
|
||||||
"""
|
"""
|
||||||
Joins a list with a string, like Python's ``str.join(list)``.
|
Joins a list with a string, like Python's ``str.join(list)``.
|
||||||
|
@ -515,6 +547,7 @@ def join(value, arg, autoescape=None):
|
||||||
join.is_safe = True
|
join.is_safe = True
|
||||||
join.needs_autoescape = True
|
join.needs_autoescape = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def last(value):
|
def last(value):
|
||||||
"Returns the last item in a list"
|
"Returns the last item in a list"
|
||||||
try:
|
try:
|
||||||
|
@ -523,6 +556,7 @@ def last(value):
|
||||||
return u''
|
return u''
|
||||||
last.is_safe = True
|
last.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def length(value):
|
def length(value):
|
||||||
"""Returns the length of the value - useful for lists."""
|
"""Returns the length of the value - useful for lists."""
|
||||||
try:
|
try:
|
||||||
|
@ -531,6 +565,7 @@ def length(value):
|
||||||
return ''
|
return ''
|
||||||
length.is_safe = True
|
length.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def length_is(value, arg):
|
def length_is(value, arg):
|
||||||
"""Returns a boolean of whether the value's length is the argument."""
|
"""Returns a boolean of whether the value's length is the argument."""
|
||||||
try:
|
try:
|
||||||
|
@ -539,11 +574,13 @@ def length_is(value, arg):
|
||||||
return ''
|
return ''
|
||||||
length_is.is_safe = False
|
length_is.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def random(value):
|
def random(value):
|
||||||
"""Returns a random item from the list."""
|
"""Returns a random item from the list."""
|
||||||
return random_module.choice(value)
|
return random_module.choice(value)
|
||||||
random.is_safe = True
|
random.is_safe = True
|
||||||
|
|
||||||
|
@register.filter("slice")
|
||||||
def slice_(value, arg):
|
def slice_(value, arg):
|
||||||
"""
|
"""
|
||||||
Returns a slice of the list.
|
Returns a slice of the list.
|
||||||
|
@ -565,6 +602,7 @@ def slice_(value, arg):
|
||||||
return value # Fail silently.
|
return value # Fail silently.
|
||||||
slice_.is_safe = True
|
slice_.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def unordered_list(value, autoescape=None):
|
def unordered_list(value, autoescape=None):
|
||||||
"""
|
"""
|
||||||
Recursively takes a self-nested list and returns an HTML unordered list --
|
Recursively takes a self-nested list and returns an HTML unordered list --
|
||||||
|
@ -657,6 +695,7 @@ unordered_list.needs_autoescape = True
|
||||||
# INTEGERS #
|
# INTEGERS #
|
||||||
###################
|
###################
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def add(value, arg):
|
def add(value, arg):
|
||||||
"""Adds the arg to the value."""
|
"""Adds the arg to the value."""
|
||||||
try:
|
try:
|
||||||
|
@ -668,6 +707,7 @@ def add(value, arg):
|
||||||
return ''
|
return ''
|
||||||
add.is_safe = False
|
add.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def get_digit(value, arg):
|
def get_digit(value, arg):
|
||||||
"""
|
"""
|
||||||
Given a whole number, returns the requested digit of it, where 1 is the
|
Given a whole number, returns the requested digit of it, where 1 is the
|
||||||
|
@ -692,6 +732,7 @@ get_digit.is_safe = False
|
||||||
# DATES #
|
# DATES #
|
||||||
###################
|
###################
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def date(value, arg=None):
|
def date(value, arg=None):
|
||||||
"""Formats a date according to the given format."""
|
"""Formats a date according to the given format."""
|
||||||
if not value:
|
if not value:
|
||||||
|
@ -707,6 +748,7 @@ def date(value, arg=None):
|
||||||
return ''
|
return ''
|
||||||
date.is_safe = False
|
date.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def time(value, arg=None):
|
def time(value, arg=None):
|
||||||
"""Formats a time according to the given format."""
|
"""Formats a time according to the given format."""
|
||||||
if value in (None, u''):
|
if value in (None, u''):
|
||||||
|
@ -750,11 +792,13 @@ timeuntil_filter.is_safe = False
|
||||||
# LOGIC #
|
# LOGIC #
|
||||||
###################
|
###################
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def default(value, arg):
|
def default(value, arg):
|
||||||
"""If value is unavailable, use given default."""
|
"""If value is unavailable, use given default."""
|
||||||
return value or arg
|
return value or arg
|
||||||
default.is_safe = False
|
default.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def default_if_none(value, arg):
|
def default_if_none(value, arg):
|
||||||
"""If value is None, use given default."""
|
"""If value is None, use given default."""
|
||||||
if value is None:
|
if value is None:
|
||||||
|
@ -762,11 +806,13 @@ def default_if_none(value, arg):
|
||||||
return value
|
return value
|
||||||
default_if_none.is_safe = False
|
default_if_none.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def divisibleby(value, arg):
|
def divisibleby(value, arg):
|
||||||
"""Returns True if the value is devisible by the argument."""
|
"""Returns True if the value is devisible by the argument."""
|
||||||
return int(value) % int(arg) == 0
|
return int(value) % int(arg) == 0
|
||||||
divisibleby.is_safe = False
|
divisibleby.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def yesno(value, arg=None):
|
def yesno(value, arg=None):
|
||||||
"""
|
"""
|
||||||
Given a string mapping values for true, false and (optionally) None,
|
Given a string mapping values for true, false and (optionally) None,
|
||||||
|
@ -803,6 +849,7 @@ yesno.is_safe = False
|
||||||
# MISC #
|
# MISC #
|
||||||
###################
|
###################
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def filesizeformat(bytes):
|
def filesizeformat(bytes):
|
||||||
"""
|
"""
|
||||||
Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB,
|
Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB,
|
||||||
|
@ -828,6 +875,7 @@ def filesizeformat(bytes):
|
||||||
return ugettext("%s PB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024 * 1024))
|
return ugettext("%s PB") % filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024 * 1024))
|
||||||
filesizeformat.is_safe = True
|
filesizeformat.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def pluralize(value, arg=u's'):
|
def pluralize(value, arg=u's'):
|
||||||
"""
|
"""
|
||||||
Returns a plural suffix if the value is not 1. By default, 's' is used as
|
Returns a plural suffix if the value is not 1. By default, 's' is used as
|
||||||
|
@ -878,6 +926,7 @@ def phone2numeric_filter(value):
|
||||||
return phone2numeric(value)
|
return phone2numeric(value)
|
||||||
phone2numeric_filter.is_safe = True
|
phone2numeric_filter.is_safe = True
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def pprint(value):
|
def pprint(value):
|
||||||
"""A wrapper around pprint.pprint -- for debugging, really."""
|
"""A wrapper around pprint.pprint -- for debugging, really."""
|
||||||
try:
|
try:
|
||||||
|
@ -885,54 +934,3 @@ def pprint(value):
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
return u"Error in formatting: %s" % force_unicode(e, errors="replace")
|
return u"Error in formatting: %s" % force_unicode(e, errors="replace")
|
||||||
pprint.is_safe = True
|
pprint.is_safe = True
|
||||||
|
|
||||||
# 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(filesizeformat)
|
|
||||||
register.filter(first)
|
|
||||||
register.filter(floatformat)
|
|
||||||
register.filter(force_escape)
|
|
||||||
register.filter(get_digit)
|
|
||||||
register.filter(iriencode)
|
|
||||||
register.filter(join)
|
|
||||||
register.filter(last)
|
|
||||||
register.filter(length)
|
|
||||||
register.filter(length_is)
|
|
||||||
register.filter(linebreaksbr)
|
|
||||||
register.filter(linenumbers)
|
|
||||||
register.filter(ljust)
|
|
||||||
register.filter(lower)
|
|
||||||
register.filter(make_list)
|
|
||||||
register.filter(pluralize)
|
|
||||||
register.filter(pprint)
|
|
||||||
register.filter(removetags)
|
|
||||||
register.filter(random)
|
|
||||||
register.filter(rjust)
|
|
||||||
register.filter(safe)
|
|
||||||
register.filter(safeseq)
|
|
||||||
register.filter('slice', slice_)
|
|
||||||
register.filter(slugify)
|
|
||||||
register.filter(stringformat)
|
|
||||||
register.filter(striptags)
|
|
||||||
register.filter(time)
|
|
||||||
register.filter(title)
|
|
||||||
register.filter(truncatechars)
|
|
||||||
register.filter(truncatewords)
|
|
||||||
register.filter(truncatewords_html)
|
|
||||||
register.filter(unordered_list)
|
|
||||||
register.filter(upper)
|
|
||||||
register.filter(urlencode)
|
|
||||||
register.filter(urlizetrunc)
|
|
||||||
register.filter(wordcount)
|
|
||||||
register.filter(wordwrap)
|
|
||||||
register.filter(yesno)
|
|
||||||
|
|
|
@ -3,9 +3,9 @@ from django.template import TemplateSyntaxError, Library
|
||||||
from django.utils import formats
|
from django.utils import formats
|
||||||
from django.utils.encoding import force_unicode
|
from django.utils.encoding import force_unicode
|
||||||
|
|
||||||
|
|
||||||
register = Library()
|
register = Library()
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def localize(value):
|
def localize(value):
|
||||||
"""
|
"""
|
||||||
Forces a value to be rendered as a localized value,
|
Forces a value to be rendered as a localized value,
|
||||||
|
@ -14,6 +14,7 @@ def localize(value):
|
||||||
return force_unicode(formats.localize(value, use_l10n=True))
|
return force_unicode(formats.localize(value, use_l10n=True))
|
||||||
localize.is_safe = False
|
localize.is_safe = False
|
||||||
|
|
||||||
|
@register.filter
|
||||||
def unlocalize(value):
|
def unlocalize(value):
|
||||||
"""
|
"""
|
||||||
Forces a value to be rendered as a non-localized value,
|
Forces a value to be rendered as a non-localized value,
|
||||||
|
@ -61,6 +62,3 @@ def localize_tag(parser, token):
|
||||||
nodelist = parser.parse(('endlocalize',))
|
nodelist = parser.parse(('endlocalize',))
|
||||||
parser.delete_first_token()
|
parser.delete_first_token()
|
||||||
return LocalizeNode(nodelist, use_l10n)
|
return LocalizeNode(nodelist, use_l10n)
|
||||||
|
|
||||||
register.filter(localize)
|
|
||||||
register.filter(unlocalize)
|
|
||||||
|
|
Loading…
Reference in New Issue