Complete the code to import the decorator function.
from functools import [1]
The wraps function from functools is used to preserve the original function's metadata when creating decorators.
Complete the decorator definition to accept a role parameter.
def role_required([1]): def decorator(func): @wraps(func) def wrapper(*args, **kwargs): # role check logic here return func(*args, **kwargs) return wrapper return decorator
The decorator needs to accept a role parameter to check user roles.
Fix the error in accessing the current user's roles from Flask's session.
from flask import session @role_required('admin') def admin_panel(): if 'admin' in session.get([1], []): return 'Welcome admin' return 'Access denied'
get.The roles are commonly stored in the session under the key 'roles'.
Fill both blanks to complete the role check and redirect unauthorized users.
from flask import redirect, url_for def role_required(role): def decorator(func): @wraps(func) def wrapper(*args, **kwargs): if role not in session.get([1], []): return redirect(url_for([2])) return func(*args, **kwargs) return wrapper return decorator
The session key for roles is 'roles'. Unauthorized users should be redirected to the login page.
Fill all three blanks to create a complete role-required decorator with session check and redirect.
from functools import wraps from flask import session, redirect, url_for def role_required([1]): def decorator(func): @wraps(func) def wrapper(*args, **kwargs): if [2] not in session.get([3], []): return redirect(url_for('login')) return func(*args, **kwargs) return wrapper return decorator
The decorator takes role as parameter. The check uses role and the session key 'roles'.