Mercurial > libervia-backend
comparison src/tools/common/template.py @ 2422:5425cf18929b
template: fixed the use of eval_context_filter in item_filter
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 05 Nov 2017 23:36:45 +0100 |
parents | 8b37a62336c3 |
children | d294527bd46f |
comparison
equal
deleted
inserted
replaced
2421:b7e24ce97a06 | 2422:5425cf18929b |
---|---|
425 except KeyError: | 425 except KeyError: |
426 log.debug(_(u"ignoring field \"{name}\": it doesn't exists").format(name=name)) | 426 log.debug(_(u"ignoring field \"{name}\": it doesn't exists").format(name=name)) |
427 continue | 427 continue |
428 return u' '.join(classes) or None | 428 return u' '.join(classes) or None |
429 | 429 |
430 def _item_filter(self, item, filters): | 430 @jinja2.contextfilter |
431 def _item_filter(self, ctx, item, filters): | |
431 """return item's value, filtered if suitable | 432 """return item's value, filtered if suitable |
432 | 433 |
433 @param item(object): item to filter | 434 @param item(object): item to filter |
434 value must have name and value attributes, | 435 value must have name and value attributes, |
435 mostly used for XMLUI items | 436 mostly used for XMLUI items |
444 value = item.value | 445 value = item.value |
445 filter_ = filters.get(item.name, None) | 446 filter_ = filters.get(item.name, None) |
446 if filter_ is None: | 447 if filter_ is None: |
447 return value | 448 return value |
448 elif isinstance(filter_, dict): | 449 elif isinstance(filter_, dict): |
449 filter_args = filter_.get(u'filter_args') | 450 filters_args = filter_.get(u'filters_args') |
450 for idx, f_name in enumerate(filter_.get(u'filters', [])): | 451 for idx, f_name in enumerate(filter_.get(u'filters', [])): |
451 kwargs = filter_args[idx] if filter_args is not None else {} | 452 kwargs = filters_args[idx] if filters_args is not None else {} |
452 value = self.env.filters[f_name](value, **kwargs) | 453 filter_func = self.env.filters[f_name] |
453 template = filter_.get(u'template', u'{value}') | 454 try: |
454 return template.format(value=value) | 455 eval_context_filter = filter_func.evalcontextfilter |
455 else: | 456 except AttributeError: |
456 return filter_(item.value) | 457 eval_context_filter = False |
458 | |
459 if eval_context_filter: | |
460 value = filter_func(ctx.eval_ctx, value, **kwargs) | |
461 else: | |
462 value = filter_func(value, **kwargs) | |
463 template = filter_.get(u'template') | |
464 if template: | |
465 # format will return a string, so we need to check first | |
466 # if the value is safe or not, and re-mark it after formatting | |
467 is_safe = isinstance(value, safe) | |
468 value = template.format(value=value) | |
469 if is_safe: | |
470 value = safe(value) | |
471 return value | |
457 | 472 |
458 def _adv_format(self, value, template, **kwargs): | 473 def _adv_format(self, value, template, **kwargs): |
459 """Advancer formatter | 474 """Advancer formatter |
460 | 475 |
461 like format() method, but take care or special values like None | 476 like format() method, but take care or special values like None |