2012-04-28 18:16:05 +02:00
|
|
|
from django.template import Library
|
2011-04-14 19:28:38 +02:00
|
|
|
import math
|
|
|
|
|
|
|
|
register = Library()
|
|
|
|
|
|
|
|
# taken from http://lybniz2.sourceforge.net/safeeval.html
|
|
|
|
# make a list of safe functions
|
2012-04-28 18:16:05 +02:00
|
|
|
math_safe_list = ['acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh',
|
|
|
|
'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp',
|
|
|
|
'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow',
|
|
|
|
'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
|
2011-04-14 19:28:38 +02:00
|
|
|
|
|
|
|
# use the list to filter the local namespace
|
|
|
|
math_safe_dict = dict([(k, getattr(math, k)) for k in math_safe_list])
|
|
|
|
|
|
|
|
# add any needed builtins back in.
|
|
|
|
math_safe_dict['abs'] = abs
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
|
2011-04-14 19:28:38 +02:00
|
|
|
@register.filter('math')
|
|
|
|
def math_(lopr, expr):
|
|
|
|
"""Evals a math expression and returns it's value.
|
|
|
|
|
2012-04-28 18:16:05 +02:00
|
|
|
"$1" is a placeholder. Insert "$1" in the expression where the value is
|
|
|
|
to be used. All math functions such as abs, sin, cos, floor are supported.
|
2011-04-14 19:28:38 +02:00
|
|
|
Example,
|
|
|
|
a. You will be redirected in {{ seconds|math:"$1 / 60.0" }} minutes
|
|
|
|
b. Square of {{ x }} is {{ x|math:"$1 * $1" }}
|
|
|
|
c. Square root of {{ x }} is {{ x|math:"sqrt($1)" }}
|
|
|
|
d. Given x = {{ x }}, (2 + x) * 6 = {{ x|math:"(2 + $1) * 6" }}
|
|
|
|
"""
|
|
|
|
if lopr:
|
2012-04-28 18:16:05 +02:00
|
|
|
return eval(expr.replace('$1', str(lopr)), {"__builtins__": None},
|
|
|
|
math_safe_dict)
|
2011-04-14 19:28:38 +02:00
|
|
|
return ''
|