def test_there_is_a_correct_templatetag_library():
"""
see https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/
"""
from django_performance_testing.templatetags import djpt_limits
assert hasattr(djpt_limits, 'register')
assert isinstance(djpt_limits.register, template.Library)
assert 'djptlimit' in djpt_limits.register.tags
# The below tests are based on the registry's content. Tests will rely on the
# global defaults that are tested elsewhere in the test*registry*.py files
python类Library()的实例源码
test_template_limit_blocks.py 文件源码
项目:django-performance-testing
作者: PaesslerAG
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def get_args_kwargs(parser, token):
""" copied from
django.template.(base|library).Library.simple_tag.compile_func """
def to_limit(limit_name, **limit_kwargs):
pass
params, varargs, varkw, defaults = getargspec(to_limit)
function_name = 'djptlimit'
bits = token.split_contents()[1:]
takes_context = False
args, kwargs = parse_bits(
parser, bits, params,
varargs, varkw, defaults, takes_context, function_name)
return args, kwargs
def yes_no(bool_value, show_str):
if bool_value:
return show_str.partition('/')[0]
else:
return show_str.partition('/')[2]
#register = template.Library()
#register.filter('yes_no', yes_no)
def compile_template_with_filters(template_string, filters):
"""Compile a Django template, using additional filters.
This is like Template(template_string) except that additional
filters to be made available to the template may be specified.
Normally, one would define filters as documented in [1], but this
requires the INSTALLED_APPS settings to be set, which is not the
case in NAV[2]. This function is just a hack to get around that
limitation. The code is based on
django.template.compile_string[3].
filters should be a dictionary mapping filter names to functions.
[1]: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
[2]: https://nav.uninett.no/wiki/devel:django_introduction#settings
[3]: http://code.djangoproject.com/browser/django/trunk/django/template/__init__.py
"""
lib = template.Library()
for name in filters.keys():
lib.filter(name, filters[name])
lexer = template.Lexer(template_string, None)
parser = template.Parser(lexer.tokenize())
parser.add_library(lib)
return parser.parse()