0
0
Flaskframework~10 mins

Why authorization matters in Flask - Test Your Understanding

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

Complete the code to import the Flask class.

Flask
from flask import [1]
app = [1](__name__)
Drag options to blanks, or click blank then click option'
ARequest
Bredirect
Crender_template
DFlask
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using render_template here
2fill in blank
medium

Complete the code to protect a route with a login check.

Flask
@app.route('/dashboard')
def dashboard():
    if not user_is_authenticated():
        return [1]('/login')
    return 'Welcome to your dashboard!'
Drag options to blanks, or click blank then click option'
Aredirect
Brender_template
Cabort
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using render_template instead of redirect
Using abort without explanation
3fill in blank
hard

Fix the error in the authorization check to abort with 403 if unauthorized.

Flask
from flask import abort

def admin_panel():
    if not current_user.is_admin:
        [1](403)
    return 'Admin content here.'
Drag options to blanks, or click blank then click option'
Alogin_required
Babort
Crender_template
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of abort
Calling login_required as a function here
4fill in blank
hard

Fill both blanks to check if user role is 'editor' and allow access.

Flask
def edit_article():
    if current_user.role [1] 'editor':
        return 'Edit page'
    else:
        return [2](403)
Drag options to blanks, or click blank then click option'
A==
Babort
C!=
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=='
Using redirect instead of abort
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension filtering users with role 'admin'.

Flask
admins = {user['[1]']: user for user in users if user['[2]'] [3] 'admin'}
Drag options to blanks, or click blank then click option'
Ausername
Brole
C==
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using email as key
Using '!=' instead of '=='