Studio: FlickList 8
This studio has a different structure than past Flicklist studios. Instead of watching an instructor-led walkthrough and then tackling a problem on your own, you will be given tasks to add new features to the application straightaway. This will allow you to apply more of the concepts you've learned in class on your own.
Our app at this point has user accounts and login, but users are still collaboratively editing the same list of movies. We need to fix that:
- We'll add relationships between the
Userclasses, so that each movie "belongs to" a specific user.
- We'll refactor the code so that it is cleaner and more reusable, in line with the MVC pattern.
- We will update the
/ratingspage so that a user only sees a list of movies of which she is the owner.
Checking Out the Studio code
Follow the instructions for getting the code in order to get the starter code for
Notice that we are no longer littering your studio code with
TODOs. Your instructions for the tasks you need to fulfill will be here in this section.
Add a column to the
Userclass representing its relationship to the
Movieclass. And add a property to the
Movieclass that holds the id of the owner. Review the relevant Get it Done! video lesson if you need a reminder of the process. Remember that you will have to drop and re-create tables after making these changes. While you're in the python session that does this, make sure to manually add a user to the database (since you lost all the records you added previously after dropping the tables).
Given what you learned about the MVC pattern, refactor your code accordingly:
- Model: Move the
Movieclasses to a file named
models.py. Then refactor out the app and database configuration code to a file named
app.py. Specifically, the
app.pyfile will have these 7 lines:
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['DEBUG'] = True # displays runtime errors in the browser, too app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://flicklist:[email protected]:8889/flicklist' app.config['SQLALCHEMY_ECHO'] = True db = SQLAlchemy(app)
- View: Your view code is already contained in your template files; no need to do anything further.
- Controller: Now the code that remains in your
main.pyfile is the controller code.
- Add import code: You will need to import
models.pyfile in order for the code in that file to function. And now, in order for
main.pyto utilize the
Movieclasses, you will need to import them into
main.py. Finally, you will need to import
- Model: Move the
- When the user visits the
/ratingspages showing the unwatched and watched movies, she still sees everybody's movies. Fix it so that she only sees her own unwatched and watched movies. Also, you'll need to change the code to add a movie to the watchlist so that you assign an