0
0
Flaskframework~10 mins

Permission checking in routes in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Flask module correctly.

Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Arequests
BDjango
CFlask
DReact
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated modules like Django or React.
Forgetting to import Flask causes errors when creating the app.
2fill in blank
medium

Complete the code to define a route that requires a permission check.

Flask
@app.route('/dashboard')
def dashboard():
    if not user.has_permission([1]):
        return 'Access denied', 403
    return 'Welcome to dashboard'
Drag options to blanks, or click blank then click option'
A'admin'
B'guest'
C'user'
D'anonymous'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect permission strings like 'guest' or 'anonymous'.
Not checking permissions before returning the page.
3fill in blank
hard

Fix the error in the permission check condition.

Flask
if user.has_permission([1]):
    return 'Access granted'
else:
    return 'Access denied', 403
Drag options to blanks, or click blank then click option'
Auser
B'guest'
Cadmin
D'admin'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted permission names causes NameError.
Confusing variable names with string literals.
4fill in blank
hard

Fill both blanks to create a decorator that checks for 'editor' permission before running the route function.

Flask
def permission_required([1]):
    def decorator(f):
        def wrapper(*args, **kwargs):
            if not user.has_permission([2]):
                return 'Forbidden', 403
            return f(*args, **kwargs)
        return wrapper
    return decorator
Drag options to blanks, or click blank then click option'
Apermission
Beditor
Cadmin
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong argument names in the decorator.
Checking for wrong permission strings.
5fill in blank
hard

Fill all three blanks to create a route with permission check using the decorator.

Flask
@app.route('/edit')
@permission_required([1])
def edit():
    return 'Edit page for [2]'

user = User([3])
Drag options to blanks, or click blank then click option'
A'editor'
Beditor
C'Alice'
D'admin'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted strings in decorator or user name.
Confusing permission names and user names.