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
|
2005-12-15 23:56:26 +08:00
|
|
|
from django.utils.translation import gettext
|
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()
|
|
|
|
|
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("'", "\\'")
|
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"
|
|
|
|
value = str(value)
|
|
|
|
return value and value[0].upper() + value[1:]
|
|
|
|
|
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)
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def floatformat(text):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2005-10-25 09:44:14 +08:00
|
|
|
Displays a floating point number as 34.2 (with one decimal place) -- but
|
2005-07-13 09:25:57 +08:00
|
|
|
only if there's a point to be displayed
|
|
|
|
"""
|
2005-11-03 03:01:27 +08:00
|
|
|
try:
|
|
|
|
f = float(text)
|
|
|
|
except ValueError:
|
|
|
|
return ''
|
2005-10-25 09:44:14 +08:00
|
|
|
m = f - int(f)
|
|
|
|
if m:
|
|
|
|
return '%.1f' % f
|
|
|
|
else:
|
|
|
|
return '%d' % int(f)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
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
|
|
|
|
lines = value.split('\n')
|
|
|
|
# Find the maximum width of the line count, for use with zero padding string format command
|
|
|
|
width = str(len(str(len(lines))))
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
lines[i] = ("%0" + width + "d. %s") % (i + 1, escape(line))
|
|
|
|
return '\n'.join(lines)
|
|
|
|
|
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()
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
return list(str(value))
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def slugify(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Converts to lowercase, removes non-alpha chars and converts spaces to hyphens"
|
2005-08-10 23:40:14 +08:00
|
|
|
value = re.sub('[^\w\s-]', '', value).strip().lower()
|
2006-05-15 12:20:04 +08:00
|
|
|
return re.sub('[-\s]+', '-', value)
|
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:
|
|
|
|
return ("%" + arg) % value
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
return ""
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
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.
|
|
|
|
if not isinstance(value, basestring):
|
|
|
|
value = str(value)
|
|
|
|
return truncate_words(value, length)
|
|
|
|
|
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()
|
|
|
|
|
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"
|
|
|
|
import urllib
|
|
|
|
return urllib.quote(value)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
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
|
2006-01-09 10:08:29 +08:00
|
|
|
return wrap(str(value), int(arg))
|
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
|
|
|
|
"""
|
|
|
|
return str(value).ljust(int(arg))
|
|
|
|
|
|
|
|
def rjust(value, arg):
|
|
|
|
"""
|
|
|
|
Right-aligns the value in a field of a given width
|
|
|
|
|
|
|
|
Argument: field size
|
|
|
|
"""
|
|
|
|
return str(value).rjust(int(arg))
|
|
|
|
|
|
|
|
def center(value, arg):
|
|
|
|
"Centers the value in a field of a given width"
|
|
|
|
return str(value).center(int(arg))
|
|
|
|
|
|
|
|
def cut(value, arg):
|
|
|
|
"Removes all values of arg from the given string"
|
|
|
|
return value.replace(arg, '')
|
|
|
|
|
|
|
|
###################
|
|
|
|
# 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)
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def linebreaks(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Converts newlines into <p> and <br />s"
|
|
|
|
from django.utils.html import linebreaks
|
|
|
|
return linebreaks(value)
|
|
|
|
|
2005-11-27 06:46:31 +08:00
|
|
|
def linebreaksbr(value):
|
2005-07-13 09:25:57 +08:00
|
|
|
"Converts newlines into <br />s"
|
|
|
|
return value.replace('\n', '<br />')
|
|
|
|
|
|
|
|
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()]
|
|
|
|
tags_re = '(%s)' % '|'.join(tags)
|
2005-10-28 11:45:51 +08:00
|
|
|
starttag_re = re.compile(r'<%s(/?>|(\s+[^>]*>))' % tags_re)
|
2005-07-13 09:25:57 +08:00
|
|
|
endtag_re = re.compile('</%s>' % tags_re)
|
|
|
|
value = starttag_re.sub('', value)
|
|
|
|
value = endtag_re.sub('', value)
|
|
|
|
return value
|
|
|
|
|
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
|
|
|
|
if not isinstance(value, basestring):
|
|
|
|
value = str(value)
|
|
|
|
return strip_tags(value)
|
|
|
|
|
|
|
|
###################
|
|
|
|
# LISTS #
|
|
|
|
###################
|
|
|
|
|
|
|
|
def dictsort(value, arg):
|
|
|
|
"""
|
|
|
|
Takes a list of dicts, returns that list sorted by the property given in
|
|
|
|
the argument.
|
|
|
|
"""
|
2005-10-15 04:10:13 +08:00
|
|
|
decorated = [(resolve_variable('var.' + arg, {'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.
|
|
|
|
"""
|
2005-10-15 04:10:13 +08:00
|
|
|
decorated = [(resolve_variable('var.' + arg, {'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:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def join(value, arg):
|
|
|
|
"Joins a list with a string, like Python's ``str.join(list)``"
|
|
|
|
try:
|
|
|
|
return arg.join(map(str, value))
|
|
|
|
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 = []
|
|
|
|
for x in arg.split(':'):
|
|
|
|
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.
|
|
|
|
|
|
|
|
The list is assumed to be in the proper format. For example, if ``var`` contains
|
|
|
|
``['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]``,
|
|
|
|
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>
|
|
|
|
"""
|
|
|
|
def _helper(value, tabs):
|
|
|
|
indent = '\t' * tabs
|
|
|
|
if value[1]:
|
|
|
|
return '%s<li>%s\n%s<ul>\n%s\n%s</ul>\n%s</li>' % (indent, value[0], indent,
|
2005-11-28 03:38:07 +08:00
|
|
|
'\n'.join([_helper(v, tabs+1) for v in value[1]]), indent, indent)
|
2005-07-13 09:25:57 +08:00
|
|
|
else:
|
|
|
|
return '%s<li>%s</li>' % (indent, value[0])
|
|
|
|
return _helper(value, 1)
|
|
|
|
|
|
|
|
###################
|
|
|
|
# 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:
|
|
|
|
return ''
|
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
|
2006-08-12 13:09:12 +08:00
|
|
|
if value in (None, ''):
|
2006-06-11 08:33:44 +08:00
|
|
|
return ''
|
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:
|
|
|
|
return ''
|
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:
|
|
|
|
return ''
|
|
|
|
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:
|
|
|
|
arg = gettext('yes,no,maybe')
|
2005-07-13 09:25:57 +08:00
|
|
|
bits = arg.split(',')
|
|
|
|
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).
|
|
|
|
"""
|
|
|
|
bytes = float(bytes)
|
|
|
|
if bytes < 1024:
|
|
|
|
return "%d byte%s" % (bytes, bytes != 1 and 's' or '')
|
|
|
|
if bytes < 1024 * 1024:
|
|
|
|
return "%.1f KB" % (bytes / 1024)
|
|
|
|
if bytes < 1024 * 1024 * 1024:
|
|
|
|
return "%.1f MB" % (bytes / (1024 * 1024))
|
|
|
|
return "%.1f GB" % (bytes / (1024 * 1024 * 1024))
|
|
|
|
|
2006-07-04 14:18:39 +08:00
|
|
|
def pluralize(value, arg='s'):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2006-08-12 13:09:12 +08:00
|
|
|
if not ',' in arg:
|
2006-07-22 04:48:17 +08:00
|
|
|
arg = ',' + arg
|
2006-07-04 14:18:39 +08:00
|
|
|
bits = arg.split(',')
|
2006-07-04 14:31:26 +08:00
|
|
|
if len(bits) > 2:
|
2006-07-04 14:18:39 +08:00
|
|
|
return ''
|
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:
|
|
|
|
return "Error in formatting:%s" % e
|
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)
|
|
|
|
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)
|
|
|
|
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)
|