本文整理汇总了Python中django.utils.decorators.available_attrs方法的典型用法代码示例。如果您正苦于以下问题:Python decorators.available_attrs方法的具体用法?Python decorators.available_attrs怎么用?Python decorators.available_attrs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.decorators
的用法示例。
在下文中一共展示了decorators.available_attrs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: user_passes_test
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def user_passes_test(test_func: Callable[[HttpResponse], bool], login_url: Optional[str]=None,
redirect_field_name: str=REDIRECT_FIELD_NAME) -> Callable[[ViewFuncT], ViewFuncT]:
"""
Decorator for views that checks that the user passes the given test,
redirecting to the log-in page if necessary. The test should be a callable
that takes the user object and returns True if the user passes.
"""
def decorator(view_func: ViewFuncT) -> ViewFuncT:
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request: HttpRequest, *args: object, **kwargs: object) -> HttpResponse:
if test_func(request):
return view_func(request, *args, **kwargs)
path = request.build_absolute_uri()
resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = urllib.parse.urlparse(resolved_login_url)[:2]
current_scheme, current_netloc = urllib.parse.urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
return redirect_to_login(
path, resolved_login_url, redirect_field_name)
return cast(ViewFuncT, _wrapped_view) # https://github.com/python/mypy/issues/1927
return decorator
示例2: vary_on_headers
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def vary_on_headers(*headers):
"""
A view decorator that adds the specified headers to the Vary header of the
response. Usage:
@vary_on_headers('Cookie', 'Accept-language')
def index(request):
...
Note that the header names are not case-sensitive.
"""
def decorator(func):
@wraps(func, assigned=available_attrs(func))
def inner_func(*args, **kwargs):
response = func(*args, **kwargs)
patch_vary_headers(response, headers)
return response
return inner_func
return decorator
示例3: xframe_options_deny
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def xframe_options_deny(view_func):
"""
Modifies a view function so its response has the X-Frame-Options HTTP
header set to 'DENY' as long as the response doesn't already have that
header set.
e.g.
@xframe_options_deny
def some_view(request):
...
"""
def wrapped_view(*args, **kwargs):
resp = view_func(*args, **kwargs)
if resp.get('X-Frame-Options', None) is None:
resp['X-Frame-Options'] = 'DENY'
return resp
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)
示例4: xframe_options_sameorigin
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def xframe_options_sameorigin(view_func):
"""
Modifies a view function so its response has the X-Frame-Options HTTP
header set to 'SAMEORIGIN' as long as the response doesn't already have
that header set.
e.g.
@xframe_options_sameorigin
def some_view(request):
...
"""
def wrapped_view(*args, **kwargs):
resp = view_func(*args, **kwargs)
if resp.get('X-Frame-Options', None) is None:
resp['X-Frame-Options'] = 'SAMEORIGIN'
return resp
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)
示例5: xframe_options_exempt
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def xframe_options_exempt(view_func):
"""
Modifies a view function by setting a response variable that instructs
XFrameOptionsMiddleware to NOT set the X-Frame-Options HTTP header.
e.g.
@xframe_options_exempt
def some_view(request):
...
"""
def wrapped_view(*args, **kwargs):
resp = view_func(*args, **kwargs)
resp.xframe_options_exempt = True
return resp
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)
示例6: is_owner
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def is_owner(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
# assume username is first arg
if request.user.is_authenticated():
if request.user.username == kwargs['username']:
return view_func(request, *args, **kwargs)
protocol = "https" if request.is_secure() else "http"
return HttpResponseRedirect("%s://%s" % (protocol,
request.get_host()))
path = request.build_absolute_uri()
login_url = request.build_absolute_uri(settings.LOGIN_URL)
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = urlparse.urlparse(login_url)[:2]
current_scheme, current_netloc = urlparse.urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(path, None, REDIRECT_FIELD_NAME)
return _wrapped_view
示例7: xframe_options_deny
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def xframe_options_deny(view_func):
"""
Modifies a view function so its response has the X-Frame-Options HTTP
header set to 'DENY' as long as the response doesn't already have that
header set.
e.g.
@xframe_options_deny
def some_view(request):
...
"""
def wrapped_view(*args, **kwargs):
resp = view_func(*args, **kwargs)
if resp.get('X-Frame-Options') is None:
resp['X-Frame-Options'] = 'DENY'
return resp
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)
示例8: xframe_options_sameorigin
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def xframe_options_sameorigin(view_func):
"""
Modifies a view function so its response has the X-Frame-Options HTTP
header set to 'SAMEORIGIN' as long as the response doesn't already have
that header set.
e.g.
@xframe_options_sameorigin
def some_view(request):
...
"""
def wrapped_view(*args, **kwargs):
resp = view_func(*args, **kwargs)
if resp.get('X-Frame-Options') is None:
resp['X-Frame-Options'] = 'SAMEORIGIN'
return resp
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)
示例9: xframe_options_exempt
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def xframe_options_exempt(view_func):
"""
Modifies a view function by setting a response variable that instructs
XFrameOptionsMiddleware to NOT set the X-Frame-Options HTTP header.
e.g.
@xframe_options_exempt
def some_view(request):
...
"""
def wrapped_view(*args, **kwargs):
resp = view_func(*args, **kwargs)
resp.xframe_options_exempt = True
return resp
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view)
示例10: lti_role_required
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def lti_role_required(
allowed_roles: List[str],
redirect_url: str = reverse_lazy('not_authorized'),
raise_exception: Optional[bool] = False,
) -> Callable:
def decorator(view_func: Callable) -> Callable:
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if is_allowed(request, allowed_roles, raise_exception):
return view_func(request, *args, **kwargs)
return redirect(redirect_url)
return _wrapped_view
return decorator
示例11: login_required
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def login_required(func=None, redirect_field_name="next", login_url=None):
"""
Decorator for views that checks that the user is logged in, redirecting
to the log in page if necessary.
"""
def decorator(view_func):
@functools.wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated():
return view_func(request, *args, **kwargs)
return handle_redirect_to_login(
request,
redirect_field_name=redirect_field_name,
login_url=login_url
)
return _wrapped_view
if func:
return decorator(func)
return decorator
示例12: catch_exception
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def catch_exception(fun):
"""
Used as decorator to catch all exceptions and log them without breaking the inner function.
Can be disabled by using the fail_silently keyword argument, which won't be passed to inner function.
"""
@wraps(fun, assigned=available_attrs(fun))
def wrap(*args, **kwargs):
if kwargs.pop('fail_silently', True):
try:
return fun(*args, **kwargs)
except Exception as e:
logger.exception(e)
logger.error('Got exception when running %s(%s, %s): %s.', fun.__name__, args, kwargs, e)
else:
return fun(*args, **kwargs)
return wrap
示例13: catch_api_exception
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def catch_api_exception(fun):
"""
Like catch_exception above, but it logs the exception caught.
"""
from api.task.utils import task_log_exception # circular imports
@wraps(fun, assigned=available_attrs(fun))
def wrap(*args, **kwargs):
try:
return fun(*args, **kwargs)
except Exception as e:
logger.exception(e)
logger.error('Got exception when running %s(%s, %s): %s.', fun.__name__, args, kwargs, e)
for arg in args:
if is_request(arg):
try:
task_log_exception(arg, e, task_id=getattr(e, 'task_id', None))
except Exception as exc:
logger.exception(exc)
break
else:
logger.warning('API exception could not be logged into task log')
return wrap
示例14: cache_on_auth
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def cache_on_auth(timeout):
"""Cache the response based on whether or not the user is authenticated.
Do NOT use on views that include user-specific information, e.g., CSRF tokens.
"""
# https://stackoverflow.com/questions/11661503/django-caching-for-authenticated-users-only
def _decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
key_prefix = "_auth_%s_" % request.user.is_authenticated
return cache_page(timeout, key_prefix=key_prefix)(view_func)(request, *args, **kwargs)
return _wrapped_view
return _decorator
示例15: cache_if_anon
# 需要导入模块: from django.utils import decorators [as 别名]
# 或者: from django.utils.decorators import available_attrs [as 别名]
def cache_if_anon(timeout):
"""Cache the view if the user is not authenticated and there are no messages to display."""
# https://stackoverflow.com/questions/11661503/django-caching-for-authenticated-users-only
def _decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated or messages.get_messages(request):
return view_func(request, *args, **kwargs)
else:
return cache_page(timeout)(view_func)(request, *args, **kwargs)
return _wrapped_view
return _decorator