本文整理汇总了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
示例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
示例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
示例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
示例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)
示例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
示例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)
示例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)
示例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)
示例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
示例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)
示例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
示例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
示例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
示例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