當前位置: 首頁>>代碼示例>>Python>>正文


Python decorators.available_attrs方法代碼示例

本文整理匯總了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 
開發者ID:zulip,項目名稱:zulip,代碼行數:27,代碼來源:decorator.py

示例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 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:21,代碼來源:vary.py

示例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) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:21,代碼來源:clickjacking.py

示例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) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:21,代碼來源:clickjacking.py

示例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) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:19,代碼來源:clickjacking.py

示例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 
開發者ID:awemulya,項目名稱:kobo-predict,代碼行數:24,代碼來源:decorators.py

示例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) 
開發者ID:Yeah-Kun,項目名稱:python,代碼行數:20,代碼來源:clickjacking.py

示例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) 
開發者ID:Yeah-Kun,項目名稱:python,代碼行數:20,代碼來源:clickjacking.py

示例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) 
開發者ID:Yeah-Kun,項目名稱:python,代碼行數:18,代碼來源:clickjacking.py

示例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 
開發者ID:abelardopardo,項目名稱:ontask_b,代碼行數:18,代碼來源:decorators.py

示例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 
開發者ID:madre,項目名稱:devops,代碼行數:21,代碼來源:decorators.py

示例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 
開發者ID:erigones,項目名稱:esdc-ce,代碼行數:19,代碼來源:decorators.py

示例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 
開發者ID:erigones,項目名稱:esdc-ce,代碼行數:27,代碼來源:decorators.py

示例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 
開發者ID:twschiller,項目名稱:open-synthesis,代碼行數:15,代碼來源:decorators.py

示例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 
開發者ID:twschiller,項目名稱:open-synthesis,代碼行數:14,代碼來源:decorators.py


注:本文中的django.utils.decorators.available_attrs方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。