summaryrefslogtreecommitdiffstats
path: root/utils/filters.py
diff options
context:
space:
mode:
authorAlexander Sulfrian <alex@spline.inf.fu-berlin.de>2016-01-10 05:08:36 +0100
committerAlexander Sulfrian <alex@spline.inf.fu-berlin.de>2016-01-10 05:08:36 +0100
commit1ec270de4390f215f874e8fad23736ce978c1bbd (patch)
treef56ebd30ec7648f785b558e499148d424bc55147 /utils/filters.py
parent915c05c05a5b510d53042944582dc62c7d3f28d1 (diff)
downloadpadlite-teams-1ec270de4390f215f874e8fad23736ce978c1bbd.tar.gz
padlite-teams-1ec270de4390f215f874e8fad23736ce978c1bbd.tar.bz2
padlite-teams-1ec270de4390f215f874e8fad23736ce978c1bbd.zip
Use sqlalchemy, flask-migrate, flask-login and flask-script
No peewee anymore. All dependencies are available as debian packages now.
Diffstat (limited to 'utils/filters.py')
-rw-r--r--utils/filters.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/utils/filters.py b/utils/filters.py
new file mode 100644
index 0000000..eb0e1c8
--- /dev/null
+++ b/utils/filters.py
@@ -0,0 +1,39 @@
+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