Python django.utils.functional.wraps() Examples
The following are 10
code examples of django.utils.functional.wraps().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.utils.functional
, or try the search function
.
Example #1
Source File: decorators.py From tethys with BSD 2-Clause "Simplified" License | 6 votes |
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
Example #2
Source File: auth.py From coursys with GNU General Public License v3.0 | 5 votes |
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
Example #3
Source File: safestring.py From bioforum with MIT License | 5 votes |
def _safety_decorator(safety_marker, func): @wraps(func) def wrapped(*args, **kwargs): return safety_marker(func(*args, **kwargs)) return wrapped
Example #4
Source File: rest.py From Politikon with GNU General Public License v2.0 | 5 votes |
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
Example #5
Source File: safestring.py From Hands-On-Application-Development-with-PyCharm with MIT License | 5 votes |
def _safety_decorator(safety_marker, func): @wraps(func) def wrapped(*args, **kwargs): return safety_marker(func(*args, **kwargs)) return wrapped
Example #6
Source File: safestring.py From python with Apache License 2.0 | 5 votes |
def _safety_decorator(safety_marker, func): @wraps(func) def wrapped(*args, **kwargs): return safety_marker(func(*args, **kwargs)) return wrapped
Example #7
Source File: utils.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
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
Example #8
Source File: safestring.py From python2017 with MIT License | 5 votes |
def _safety_decorator(safety_marker, func): @wraps(func) def wrapped(*args, **kwargs): return safety_marker(func(*args, **kwargs)) return wrapped
Example #9
Source File: decorators.py From tethys with BSD 2-Clause "Simplified" License | 4 votes |
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
Example #10
Source File: workspace.py From tethys with BSD 2-Clause "Simplified" License | 4 votes |
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