0
0
Flaskframework~10 mins

Admin panel protection 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 login manager.

Flask
from flask_login import [1]
Drag options to blanks, or click blank then click option'
Alogin_required
BLoginManager
CUserMixin
Dcurrent_user
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the decorator 'login_required' instead of the manager class.
Confusing 'UserMixin' with the login manager.
2fill in blank
medium

Complete the code to protect the admin route so only logged-in users can access it.

Flask
@app.route('/admin')
@[1]
def admin_panel():
    return 'Welcome to admin panel'
Drag options to blanks, or click blank then click option'
Alogin_manager.login_view
BUserMixin
Clogin_required
Dcurrent_user.is_authenticated
Attempts:
3 left
💡 Hint
Common Mistakes
Using login_manager.login_view which is a configuration, not a decorator.
Checking current_user.is_authenticated without a decorator.
3fill in blank
hard

Fix the error in the user loader function to load a user by ID.

Flask
@login_manager.user_loader
def load_user([1]):
    return User.query.get(int(user_id))
Drag options to blanks, or click blank then click option'
Auser_id
Buser
Cid
Dcurrent_user
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from user_id causes errors.
Using current_user as a parameter.
4fill in blank
hard

Fill both blanks to check if the current user is authenticated and has admin role.

Flask
if current_user.[1] and current_user.[2] == 'admin':
    return 'Access granted'
else:
    return 'Access denied'
Drag options to blanks, or click blank then click option'
Ais_authenticated
Brole
Cusername
Dis_active
Attempts:
3 left
💡 Hint
Common Mistakes
Using username instead of role to check admin rights.
Checking is_active instead of is_authenticated.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps usernames to emails for users with admin role.

Flask
admin_users = {user.[1]: user.[2] for user in users if user.[3] == 'admin'}
Drag options to blanks, or click blank then click option'
Ausername
Bemail
Crole
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using id instead of username as dictionary keys.
Filtering by email instead of role.