diff options
author | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2014-11-18 05:29:59 +0100 |
---|---|---|
committer | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2014-11-19 02:15:55 +0100 |
commit | 90a8462a48bce1cda4102e37f76b0bf3bb827301 (patch) | |
tree | 12ed7092bcf5f42eed1491ed2e4ab6205d0c714b /filters.py | |
parent | 0bfe5fad665a1d75b194a49c8f8f58624a08f478 (diff) | |
download | padlite-teams-90a8462a48bce1cda4102e37f76b0bf3bb827301.tar.gz padlite-teams-90a8462a48bce1cda4102e37f76b0bf3bb827301.tar.bz2 padlite-teams-90a8462a48bce1cda4102e37f76b0bf3bb827301.zip |
filters: backport selectattr, rejectattr
We want to use jinja2 from wheezy and need to backport two template functions.
Diffstat (limited to 'filters.py')
-rw-r--r-- | filters.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/filters.py b/filters.py new file mode 100644 index 0000000..b8b0d47 --- /dev/null +++ b/filters.py @@ -0,0 +1,36 @@ +from app import app +from jinja2 import contextfilter +from jinja2.filters import make_attrgetter + +@app.template_filter('selectattr') +@contextfilter +def do_selectattr(*args, **kwargs): + return _select_or_reject(args, kwargs, lambda x: x) + +@app.template_filter('rejectattr') +@contextfilter +def do_rejectattr(*args, **kwargs): + return _select_or_reject(args, kwargs, lambda x: not x) + +def _select_or_reject(args, kwargs, modfunc): + context = args[0] + seq = args[1] + + try: + attr = args[2] + except LookupError: + raise FilterArgumentError('Missing parameter for attribute name') + transfunc = make_attrgetter(context.environment, attr) + + try: + name = args[3] + args = args[4:] + func = lambda item: context.environment.call_test( + name, item, args, kwargs) + except LookupError: + func = bool + + if seq: + for item in seq: + if modfunc(func(transfunc(item))): + yield item |