Replies: 1 comment 1 reply
-
well I simplified a bit your version to pass role as parameters - and it works: ROLES = {
"user": 1,
"limited": 2,
"admin": 3,
"superadmin": 4,
}
NOT_AUTHORISED = 9999
def min_role(role):
def decorator(func):
@wraps(func)
def wrapper(request, *args, **kwargs):
user_role = int(request.GET.get('role', 0))
if user_role < ROLES.get(role, NOT_AUTHORISED):
return 403, None
return func(request, *args, **kwargs)
return wrapper
return decorator
@api.get(
"/locations",
url_name="location-create",
response={201: str, 403: None},
)
@min_role("superadmin")
def create_location(request, location: int, role: int):
return 201, 'success'
Maybe it's from functools import wraps |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there!
I'm trying to make a custom decorator, but for some reason the code never enters the wrapper function. Can you see an obvious mistake I making here?
Beta Was this translation helpful? Give feedback.
All reactions