本文整理匯總了Python中django.utils.functional.wraps方法的典型用法代碼示例。如果您正苦於以下問題:Python functional.wraps方法的具體用法?Python functional.wraps怎麽用?Python functional.wraps使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.utils.functional
的用法示例。
在下文中一共展示了functional.wraps方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: login_required
# 需要導入模塊: from django.utils import functional [as 別名]
# 或者: from django.utils.functional import wraps [as 別名]
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
"""
Decorator for Tethys App controllers that checks whether a user has a permission.
"""
def decorator(controller_func):
def wrapper(request, *args, **kwargs):
if not getattr(settings, 'ENABLE_OPEN_PORTAL', False):
from django.contrib.auth.decorators import login_required as lr
dec = lr(function=function, redirect_field_name=redirect_field_name, login_url=login_url)
controller = dec(controller_func)
return controller(request, *args, **kwargs)
else:
return controller_func(request, *args, **kwargs)
return wraps(controller_func)(wrapper)
return decorator
示例2: user_passes_test
# 需要導入模塊: from django.utils import functional [as 別名]
# 或者: from django.utils.functional import wraps [as 別名]
def user_passes_test(test_func, login_url=None,
redirect_field_name=REDIRECT_FIELD_NAME, force_privacy=False):
"""Replacement for django.contrib.auth.decorators.user_passes_test that
returns 403 Forbidden if the user is already logged in.
"""
if not login_url:
login_url = settings.LOGIN_URL
def decorator(view_func):
@wraps(view_func)
def wrapper(request, *args, **kwargs):
if test_func(request, **kwargs):
if needs_privacy_signature(request, only_relevant_roles=not force_privacy):
# logic there: usually only check for the admin roles we know have a privacy implication. If we're
# passed force_privacy, then views must have the privacy agreement.
return privacy_redirect(request)
elif needs_privacy_signature_da(request):
return privacy_da_redirect(request)
else:
return view_func(request, *args, **kwargs)
elif request.user.is_authenticated:
return ForbiddenResponse(request)
else:
path = '%s?%s=%s' % (login_url, redirect_field_name,
urlquote(request.get_full_path()))
return HttpResponseRedirect(path)
return wrapper
return decorator
示例3: _safety_decorator
# 需要導入模塊: from django.utils import functional [as 別名]
# 或者: from django.utils.functional import wraps [as 別名]
def _safety_decorator(safety_marker, func):
@wraps(func)
def wrapped(*args, **kwargs):
return safety_marker(func(*args, **kwargs))
return wrapped
示例4: rest_api_login_required
# 需要導入模塊: from django.utils import functional [as 別名]
# 或者: from django.utils.functional import wraps [as 別名]
def rest_api_login_required(view):
@wraps(view)
def inner(request, *args, **kwargs):
if not request.user.is_authenticated():
return HttpResponseUnauthorized()
return view(request, *args, **kwargs)
return inner
示例5: __call__
# 需要導入模塊: from django.utils import functional [as 別名]
# 或者: from django.utils.functional import wraps [as 別名]
def __call__(self, test_func):
from django.test import SimpleTestCase
if isinstance(test_func, type):
if not issubclass(test_func, SimpleTestCase):
raise Exception(
"Only subclasses of Django SimpleTestCase can be decorated "
"with override_settings")
original_pre_setup = test_func._pre_setup
original_post_teardown = test_func._post_teardown
def _pre_setup(innerself):
self.enable()
original_pre_setup(innerself)
def _post_teardown(innerself):
original_post_teardown(innerself)
self.disable()
test_func._pre_setup = _pre_setup
test_func._post_teardown = _post_teardown
return test_func
else:
@wraps(test_func)
def inner(*args, **kwargs):
with self:
return test_func(*args, **kwargs)
return inner
示例6: enforce_quota
# 需要導入模塊: from django.utils import functional [as 別名]
# 或者: from django.utils.functional import wraps [as 別名]
def enforce_quota(codename):
"""
Decorator to enforce custom quotas
Args:
codename (string): codename of quota to enforce
""" # noqa: E501
def decorator(controller):
def wrapper(*args, **kwargs):
try:
request = None
for index, arg in enumerate(args):
if isinstance(arg, HttpRequest):
request = arg
break
if request is None:
raise ValueError('Invalid request')
rq = ResourceQuota.objects.get(codename=codename)
if rq.applies_to == 'django.contrib.auth.models.User':
entity = request.user
elif rq.applies_to == 'tethys_apps.models.TethysApp':
entity = get_active_app(request)
if not entity:
raise ValueError('Request could not be used to find app')
else:
raise ValueError('ResourceQuota that applies_to {} is not supported'.format(rq.applies_to))
if not passes_quota(entity, codename):
raise PermissionDenied(rq.help)
except ValueError as e:
log.warning(str(e))
except ResourceQuota.DoesNotExist:
log.warning('ResourceQuota with codename {} does not exist.'.format(codename))
return controller(*args, **kwargs)
return wraps(controller)(wrapper)
return decorator
示例7: app_workspace
# 需要導入模塊: from django.utils import functional [as 別名]
# 或者: from django.utils.functional import wraps [as 別名]
def app_workspace(controller):
"""
**Decorator:** Get the file workspace (directory) for the app.
Returns:
tethys_apps.base.TethysWorkspace: An object representing the workspace.
**Example:**
::
import os
from my_first_app.app import MyFirstApp as app
from tethys_sdk.workspaces import app_workspace
@app_workspace
def a_controller(request, app_workspace):
\"""
Example controller that uses @app_workspace() decorator.
\"""
new_file_path = os.path.join(app_workspace.path, 'new_file.txt')
with open(new_file_path, 'w') as a_file:
a_file.write('...')
context = {}
return render(request, 'my_first_app/template.html', context)
"""
@wraps(controller)
def wrapper(*args, **kwargs):
from tethys_quotas.models import ResourceQuota
from tethys_apps.utilities import get_active_app
request = None
for index, arg in enumerate(args):
if isinstance(arg, HttpRequest):
request = arg
break
if request is None:
raise ValueError('No request given. The app_workspace decorator only works on controllers.')
try:
codename = 'app_workspace_quota'
rq = ResourceQuota.objects.get(codename=codename)
except ResourceQuota.DoesNotExist:
log.warning('ResourceQuota with codename {} does not exist.'.format(codename))
# Get the active app
app = get_active_app(request, get_class=True)
if not passes_quota(app, codename):
raise PermissionDenied(rq.help)
the_workspace = _get_app_workspace(app)
return controller(*args, the_workspace, **kwargs)
return wrapper