Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask class.
Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase flask instead of Flask
✗ Incorrect
The Flask class is imported from the flask module to create the app.
2fill in blank
mediumComplete the code to define a route for email verification.
Flask
@app.route('/verify_email/[1]') def verify_email(token): pass
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name in the route and function
Not including a variable in the route
✗ Incorrect
The route uses a variable part named 'token' to capture the verification token from the URL.
3fill in blank
hardFix the error in the code to generate a timed token for email verification.
Flask
from itsdangerous import URLSafeTimedSerializer def generate_token(email): serializer = URLSafeTimedSerializer(app.config['SECRET_KEY']) return serializer.[1](email, salt=app.config['SECURITY_PASSWORD_SALT'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'loads' instead of 'dumps' to create a token
Using 'sign' or 'unsign' which are not methods of URLSafeTimedSerializer
✗ Incorrect
The 'dumps' method creates a signed token from the email string.
4fill in blank
hardFill both blanks to decode the token and handle expiration.
Flask
def confirm_token(token, expiration=3600): serializer = URLSafeTimedSerializer(app.config['SECRET_KEY']) try: email = serializer.[1](token, salt=app.config['SECURITY_PASSWORD_SALT'], max_age=[2]) except Exception: return False return email
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dumps' to decode the token
Not passing the expiration time correctly
✗ Incorrect
'loads' decodes the token and 'max_age' sets the expiration time in seconds.
5fill in blank
hardFill all three blanks to send a verification email with a token link.
Flask
from flask_mail import Message def send_verification_email(user_email): token = generate_token(user_email) verify_url = url_for('verify_email', token=[1], _external=True) msg = Message('Please verify your email', sender=app.config['MAIL_DEFAULT_SENDER'], recipients=[user_email]) msg.body = f'Click the link to verify your email: [2]' mail.[3](msg)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send_message' instead of 'send'
Passing the wrong variable to url_for
Not using the verification URL in the email body
✗ Incorrect
The token variable is passed to the URL, the URL is used in the email body, and 'send' sends the email message.