0
0
Flaskframework~10 mins

Repository pattern for data access 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 define a repository class for accessing user data.

Flask
class UserRepository:
    def __init__(self, db_session):
        self.db_session = [1]
Drag options to blanks, or click blank then click option'
Adb_session
Bsession
Cdatabase
Ddb
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the constructor parameter.
Forgetting to assign the parameter to an instance variable.
2fill in blank
medium

Complete the method to add a new user object to the database session.

Flask
def add_user(self, user):
    self.db_session.[1](user)
Drag options to blanks, or click blank then click option'
Aadd
Bsave
Cinsert
Dcommit
Attempts:
3 left
💡 Hint
Common Mistakes
Using commit instead of add.
Using insert which is not a session method.
3fill in blank
hard

Fix the error in the method to retrieve a user by ID using the session's query.

Flask
def get_user_by_id(self, user_id):
    return self.db_session.query(User).[1](user_id)
Drag options to blanks, or click blank then click option'
Afilter_by
Bget
Cfilter
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter_by without calling .first().
Using a non-existent method like find.
4fill in blank
hard

Fill both blanks to update a user's email and commit the change.

Flask
def update_user_email(self, user_id, new_email):
    user = self.db_session.query(User).get(user_id)
    user.email = [1]
    self.db_session.[2]()
Drag options to blanks, or click blank then click option'
Anew_email
Bcommit
Cflush
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the wrong variable to user.email.
Calling flush instead of commit.
5fill in blank
hard

Fill all three blanks to delete a user by ID and commit the transaction.

Flask
def delete_user(self, user_id):
    user = self.db_session.query(User).[1](user_id)
    if user:
        self.db_session.[2](user)
        self.db_session.[3]()
Drag options to blanks, or click blank then click option'
Aget
Bdelete
Ccommit
Dfilter_by
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter_by without fetching the object.
Calling commit before deleting.