当前位置: 首页>>代码示例>>Python>>正文


Python decorators.user_passes_test方法代码示例

本文整理汇总了Python中django.contrib.auth.decorators.user_passes_test方法的典型用法代码示例。如果您正苦于以下问题:Python decorators.user_passes_test方法的具体用法?Python decorators.user_passes_test怎么用?Python decorators.user_passes_test使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.contrib.auth.decorators的用法示例。


在下文中一共展示了decorators.user_passes_test方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: login_if_required_for_dashboard

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def login_if_required_for_dashboard(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
    """
    Decorator for views that checks that the user is logged in, redirecting to the log-in page if necessary -
    but only if REQUIRE_LOGIN_FOR_DASHBOARD is set True in Constance.
    """

    def authenticated_test(u):
        if config.REQUIRE_LOGIN_FOR_DASHBOARD:
            return u.is_authenticated
        else:
            return True

    actual_decorator = user_passes_test(
        authenticated_test,
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )
    if function:
        return actual_decorator(function)
    return actual_decorator 
开发者ID:thorrak,项目名称:fermentrack,代码行数:22,代码来源:decorators.py

示例2: report_view

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def report_view(title, form_type=None):
    ''' Decorator that converts a report view function into something that
    displays a Report.

    Arguments:
        title (str):
            The title of the report.
        form_type (Optional[forms.Form]):
            A form class that can make this report display things. If not
            supplied, no form will be displayed.

    '''

    # Create & return view
    def _report(view):
        report_view = ReportView(view, title, form_type)
        report_view = user_passes_test(views._staff_only)(report_view)
        report_view = wraps(view)(report_view)

        # Add this report to the list of reports.
        _all_report_views.append(report_view)

        return report_view

    return _report 
开发者ID:chrisjrn,项目名称:registrasion,代码行数:27,代码来源:reports.py

示例3: anonymous_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def anonymous_required(function=None, redirect_url=None):
    """
    Decorator requiring the current user to be anonymous (not logged in).
    """
    if not redirect_url:
        redirect_url = settings.LOGIN_REDIRECT_URL

    actual_decorator = user_passes_test(
        is_anonymous,
        login_url=redirect_url,
        redirect_field_name=None
    )

    if function:
        return actual_decorator(function)
    return actual_decorator 
开发者ID:discolabs,项目名称:django-shopify-auth,代码行数:18,代码来源:decorators.py

示例4: login_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME,
        login_url=None):
    """
    Replacement for django's built-in login_required
    decorator that also requires user to not be a
    temporary user (must have completed signup process).

    It can be used identically to the built-in version.
    """
    actual_decorator = user_passes_test(
            lambda u: u.is_authenticated and not is_tmp_user(u),
            login_url=login_url,
            redirect_field_name=redirect_field_name
    )
    if function:
        return actual_decorator(function)
    return actual_decorator 
开发者ID:lgoodridge,项目名称:django-uniauth,代码行数:19,代码来源:decorators.py

示例5: otp_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def otp_required(view=None, redirect_field_name='next', login_url=None, if_configured=False):
    """
    Similar to :func:`~django.contrib.auth.decorators.login_required`, but
    requires the user to be :term:`verified`. By default, this redirects users
    to :setting:`OTP_LOGIN_URL`.

    :param if_configured: If ``True``, an authenticated user with no confirmed
        OTP devices will be allowed. Default is ``False``.
    :type if_configured: bool
    """
    if login_url is None:
        login_url = settings.OTP_LOGIN_URL

    def test(user):
        return user.is_verified() or (if_configured and user.is_authenticated and not user_has_device(user))

    decorator = user_passes_test(test, login_url=login_url, redirect_field_name=redirect_field_name)

    return decorator if (view is None) else decorator(view) 
开发者ID:django-otp,项目名称:django-otp,代码行数:21,代码来源:decorators.py

示例6: account_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def account_required(func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
    """Require that the (1) the user is logged in, or (2) that an account is not required to view the page.

    If the user fails the test, redirect the user to the log-in page. See also
    django.contrib.auth.decorators.login_required
    """
    req = getattr(settings, 'ACCOUNT_REQUIRED', False)
    actual_decorator = user_passes_test(
        lambda u: not req or u.is_authenticated,
        login_url=login_url,
        redirect_field_name=redirect_field_name,
    )
    if func:
        return actual_decorator(func)
    return actual_decorator 
开发者ID:twschiller,项目名称:open-synthesis,代码行数:17,代码来源:decorators.py

示例7: group_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def group_required(*group_names):
    """
    Requires user membership in at least one of the groups passed in.

    """

    def in_groups(u):
        if u.is_authenticated:
            if u.is_superuser or bool(u.groups.filter(name__in=group_names)):
                return True
        return False

    return user_passes_test(in_groups) 
开发者ID:archesproject,项目名称:arches,代码行数:15,代码来源:decorators.py

示例8: can_read_concept

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def can_read_concept():
    """
    Requires that a user be able to edit or delete a single nodegroup of a resource

    """

    return user_passes_test(user_can_read_concepts) 
开发者ID:archesproject,项目名称:arches,代码行数:9,代码来源:decorators.py

示例9: staff_member_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def staff_member_required(view_func, redirect_field_name=REDIRECT_FIELD_NAME, login_url='admin:login'):
    """
    Decorator for views that checks that the user is logged in and is a staff
    member, displaying the login page if necessary.
    """
    return user_passes_test(
        lambda u: u.is_active and u.is_staff,
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )(view_func) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:12,代码来源:decorators.py

示例10: staff_member_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def staff_member_required(view_func=None, redirect_field_name=REDIRECT_FIELD_NAME,
                          login_url='admin:login'):
    """
    Decorator for views that checks that the user is logged in and is a staff
    member, redirecting to the login page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_staff,
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )
    if view_func:
        return actual_decorator(view_func)
    return actual_decorator 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:16,代码来源:decorators.py

示例11: deleteResearcher

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def deleteResearcher(request, pk):
    """
    Deletes a researcher (from a department). Their papers are
    left as they are.
    """
    researcher = get_object_or_404(Researcher, pk=pk)
    dept = researcher.department
    researcher.delete()
    if dept:
        dept.update_stats()
    return HttpResponse('OK', content_type='text/plain')

# paper management
#@user_passes_test(is_admin)
# def changepaper(request):
#    allowedFields = ['title']
#    return process_ajax_change(request, Paper, allowedFields)

# department management
#@user_passes_test(is_admin)
# def changedepartment(request):
#    allowedFields = ['name']
#    return process_ajax_change(request, Department, allowedFields)

# researcher management
#@user_passes_test(is_admin)
# def changeresearcher(request):
#    allowedFields = ['role']
#    return process_ajax_change(request, Researcher, allowedFields) 
开发者ID:dissemin,项目名称:dissemin,代码行数:31,代码来源:ajax.py

示例12: zulip_otp_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def zulip_otp_required(
    redirect_field_name: str='next',
    login_url: str=settings.HOME_NOT_LOGGED_IN,
) -> Callable[[ViewFuncT], ViewFuncT]:
    """
    The reason we need to create this function is that the stock
    otp_required decorator doesn't play well with tests. We cannot
    enable/disable if_configured parameter during tests since the decorator
    retains its value due to closure.

    Similar to :func:`~django.contrib.auth.decorators.login_required`, but
    requires the user to be :term:`verified`. By default, this redirects users
    to :setting:`OTP_LOGIN_URL`.
    """

    def test(user: UserProfile) -> bool:
        """
        :if_configured: If ``True``, an authenticated user with no confirmed
        OTP devices will be allowed. Default is ``False``. If ``False``,
        2FA will not do any authentication.
        """
        if_configured = settings.TWO_FACTOR_AUTHENTICATION_ENABLED
        if not if_configured:
            return True

        return user.is_verified() or (user.is_authenticated
                                      and not user_has_device(user))

    decorator = django_user_passes_test(test,
                                        login_url=login_url,
                                        redirect_field_name=redirect_field_name)

    return decorator 
开发者ID:zulip,项目名称:zulip,代码行数:35,代码来源:decorator.py

示例13: student_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def student_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='login'):
    '''
    Decorator for views that checks that the logged in user is a student,
    redirects to the log-in page if necessary.
    '''
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_student,
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )
    if function:
        return actual_decorator(function)
    return actual_decorator 
开发者ID:sibtc,项目名称:django-multiple-user-types-example,代码行数:15,代码来源:decorators.py

示例14: teacher_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def teacher_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='login'):
    '''
    Decorator for views that checks that the logged in user is a teacher,
    redirects to the log-in page if necessary.
    '''
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_teacher,
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )
    if function:
        return actual_decorator(function)
    return actual_decorator 
开发者ID:sibtc,项目名称:django-multiple-user-types-example,代码行数:15,代码来源:decorators.py

示例15: instance_manager_required

# 需要导入模块: from django.contrib.auth import decorators [as 别名]
# 或者: from django.contrib.auth.decorators import user_passes_test [as 别名]
def instance_manager_required(function=None, redirect_to=None, raise_exception=False):
    """
    View decorator that checks whether the user is an InstanceManager, i.e.
      has the permission to browse their own instances or all instances.

    Modeled on django.contrib.auth.decorators.permission_required().

    :param function: view function to wrap
    :param redirect_to: URL to redirect to if user is not an InstanceManager user
    :param raise_exception: if set, will raise PermissionDenied if user is not an InstanceManager user.
    """
    def check_perm(user):
        """Checks if the user is an instance manager"""
        if InstanceReference.can_manage(user):
            return True
        # In case the 403 handler should be called raise the exception
        if raise_exception:
            raise PermissionDenied
        # Or, show login form.
        return False

    # Use the user_passes_test view decorator to handle redirect.
    actual_decorator = user_passes_test(
        check_perm,
        login_url=redirect_to,
        redirect_field_name=None
    )
    if function:
        return actual_decorator(function)
    return actual_decorator 
开发者ID:open-craft,项目名称:opencraft,代码行数:32,代码来源:decorators.py


注:本文中的django.contrib.auth.decorators.user_passes_test方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。