本文整理汇总了Python中django.utils.functional.lazy方法的典型用法代码示例。如果您正苦于以下问题:Python functional.lazy方法的具体用法?Python functional.lazy怎么用?Python functional.lazy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.functional
的用法示例。
在下文中一共展示了functional.lazy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: csrf
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def csrf(request):
"""
Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
it has not been provided by either a view decorator or the middleware
"""
def _get_val():
token = get_token(request)
if token is None:
# In order to be able to provide debugging info in the
# case of misconfiguration, we use a sentinel value
# instead of returning an empty dict.
return 'NOTPROVIDED'
else:
return smart_text(token)
_get_val = lazy(_get_val, six.text_type)
return {'csrf_token': _get_val()}
示例2: csrf
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def csrf(request):
"""
Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
it has not been provided by either a view decorator or the middleware
"""
def _get_val():
token = get_token(request)
if token is None:
# In order to be able to provide debugging info in the
# case of misconfiguration, we use a sentinel value
# instead of returning an empty dict.
return 'NOTPROVIDED'
else:
return smart_text(token)
_get_val = lazy(_get_val, six.text_type)
return {'csrf_token': _get_val() }
示例3: __init__
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def __init__(self, **kwargs):
self.allow_blank = kwargs.pop('allow_blank', False)
self.trim_whitespace = kwargs.pop('trim_whitespace', True)
self.max_length = kwargs.pop('max_length', None)
self.min_length = kwargs.pop('min_length', None)
super(CharField, self).__init__(**kwargs)
if self.max_length is not None:
message = lazy(
self.error_messages['max_length'].format,
six.text_type)(max_length=self.max_length)
self.validators.append(
MaxLengthValidator(self.max_length, message=message))
if self.min_length is not None:
message = lazy(
self.error_messages['min_length'].format,
six.text_type)(min_length=self.min_length)
self.validators.append(
MinLengthValidator(self.min_length, message=message))
示例4: lazy_number
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def lazy_number(func, resultclass, number=None, **kwargs):
if isinstance(number, six.integer_types):
kwargs['number'] = number
proxy = lazy(func, resultclass)(**kwargs)
else:
class NumberAwareString(resultclass):
def __mod__(self, rhs):
if isinstance(rhs, dict) and number:
try:
number_value = rhs[number]
except KeyError:
raise KeyError('Your dictionary lacks key \'%s\'. '
'Please provide it, because it is required to '
'determine whether string is singular or plural.'
% number)
else:
number_value = rhs
kwargs['number'] = number_value
translated = func(**kwargs)
try:
translated = translated % rhs
except TypeError:
# String doesn't contain a placeholder for the number
pass
return translated
proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs)
return proxy
示例5: debug
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def debug(request):
"""
Returns context variables helpful for debugging.
"""
context_extras = {}
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
context_extras['debug'] = True
from django.db import connection
# Return a lazy reference that computes connection.queries on access,
# to ensure it contains queries triggered after this function runs.
context_extras['sql_queries'] = lazy(lambda: connection.queries, list)
return context_extras
示例6: lazy_number
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def lazy_number(func, resultclass, number=None, **kwargs):
if isinstance(number, int):
kwargs['number'] = number
proxy = lazy(func, resultclass)(**kwargs)
else:
original_kwargs = kwargs.copy()
class NumberAwareString(resultclass):
def __bool__(self):
return bool(kwargs['singular'])
def __mod__(self, rhs):
if isinstance(rhs, dict) and number:
try:
number_value = rhs[number]
except KeyError:
raise KeyError(
"Your dictionary lacks key '%s\'. Please provide "
"it, because it is required to determine whether "
"string is singular or plural." % number
)
else:
number_value = rhs
kwargs['number'] = number_value
translated = func(**kwargs)
try:
translated = translated % rhs
except TypeError:
# String doesn't contain a placeholder for the number.
pass
return translated
proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs)
proxy.__reduce__ = lambda: (_lazy_number_unpickle, (func, resultclass, number, original_kwargs))
return proxy
示例7: debug
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def debug(request):
"""
Return context variables helpful for debugging.
"""
context_extras = {}
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
context_extras['debug'] = True
from django.db import connections
# Return a lazy reference that computes connection.queries on access,
# to ensure it contains queries triggered after this function runs.
context_extras['sql_queries'] = lazy(
lambda: list(itertools.chain.from_iterable(connections[x].queries for x in connections)),
list
)
return context_extras
示例8: get_seo_content_types
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def get_seo_content_types(seo_models):
return lazy(_get_seo_content_types, list)(seo_models)
示例9: lazy_number
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def lazy_number(func, resultclass, number=None, **kwargs):
if isinstance(number, six.integer_types):
kwargs['number'] = number
proxy = lazy(func, resultclass)(**kwargs)
else:
original_kwargs = kwargs.copy()
class NumberAwareString(resultclass):
def __bool__(self):
return bool(kwargs['singular'])
def __nonzero__(self): # Python 2 compatibility
return type(self).__bool__(self)
def __mod__(self, rhs):
if isinstance(rhs, dict) and number:
try:
number_value = rhs[number]
except KeyError:
raise KeyError(
"Your dictionary lacks key '%s\'. Please provide "
"it, because it is required to determine whether "
"string is singular or plural." % number
)
else:
number_value = rhs
kwargs['number'] = number_value
translated = func(**kwargs)
try:
translated = translated % rhs
except TypeError:
# String doesn't contain a placeholder for the number
pass
return translated
proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs)
proxy.__reduce__ = lambda: (_lazy_number_unpickle, (func, resultclass, number, original_kwargs))
return proxy
示例10: debug
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def debug(request):
"""
Returns context variables helpful for debugging.
"""
context_extras = {}
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
context_extras['debug'] = True
from django.db import connections
# Return a lazy reference that computes connection.queries on access,
# to ensure it contains queries triggered after this function runs.
context_extras['sql_queries'] = lazy(
lambda: list(itertools.chain(*[connections[x].queries for x in connections])),
list
)
return context_extras
示例11: __call__
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def __call__(self, request):
request.user_permissions = SimpleLazyObject(lambda: self.get_user_permissions(request))
request.user_space_accesses = lazy(self.get_user_space_accesses, dict)(request)
return self.get_response(request)
示例12: test_slugify_lazy_string
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def test_slugify_lazy_string(self):
lazy_str = lazy(lambda string: string, str)
self.assertEqual(
slugify(
lazy_str(
" Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/"
)
),
"Jack-Jill-like-numbers-123-and-4-and-silly-characters-",
)
示例13: __init__
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def __init__(self, target_model, filter_name=None, **kwargs):
super(ModelChooserBlock, self).__init__(**kwargs)
self._target_model = target_model
self.filter_name = filter_name
if self.meta.icon == 'placeholder':
# Get the icon from the chooser.
# The chooser may not have been registered yet, depending upon
# import orders and things, so get the icon lazily
self.meta.icon = lazy(lambda: self.chooser.icon, str)()
示例14: _format_proxy
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def _format_proxy(proxy, *args, **kwargs):
"""
Helper function to enable string formatting of lazy translation objects.
This appears to work alright, but I'm not quite sure.
"""
return str(proxy).format(*args, **kwargs)
示例15: __init__
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import lazy [as 别名]
def __init__(self, *args, **kwargs):
super(Notification, self).__init__(*args, **kwargs)
self._meta.get_field_by_name('notification_type')[0]._choices = lazy(
get_notification_types, list)()