Conversation
| @@ -3,3 +3,13 @@ | |||
|
|
|||
There was a problem hiding this comment.
Great job completing Task List Angela! Your code looks clean and easily readable. I want to point out the good variable naming and your code is DRY. Great use of helper methods in your models! Excellent work using blue prints & creating RESTful CRUD routes for each model.
| def to_dict(self): | ||
| goal_as_dict = { | ||
| "id": self.goal_id, | ||
| "title": self.title | ||
| } | ||
|
|
||
| return goal_as_dict No newline at end of file |
There was a problem hiding this comment.
Great job creating this reusable helper function`
| goal_id = db.Column(db.Integer, db.ForeignKey('goal.goal_id'), nullable=True) | ||
| goal = db.relationship("Goal", back_populates="tasks") |
There was a problem hiding this comment.
Nice approach creating this relationship to your Goal model. Why not use lazy here?
|
|
||
| return task_as_dict | ||
|
|
||
|
No newline at end of file |
There was a problem hiding this comment.
Looks like there's some extra whitespace here
| @@ -1 +1,235 @@ | |||
| from flask import Blueprint No newline at end of file | |||
| from flask import Flask, Blueprint, jsonify, abort, make_response, request | |||
There was a problem hiding this comment.
👍🏾 These imports help a great deal when it comes to our request & response cycle
| goals_bp = Blueprint("goals", __name__, url_prefix="/goals") | ||
| tasks_bp = Blueprint("tasks", __name__, url_prefix="/tasks") | ||
|
|
| def validate_model(cls, model_id): | ||
| try: | ||
| model_id = int(model_id) | ||
| except: | ||
| abort(make_response({"message": f"{model_id} is not a valid type ({type(model_id)}). Must be an integer)"}, 400)) | ||
|
|
||
| model = cls.query.get(model_id) | ||
|
|
||
| if not model: | ||
| abort(make_response({"message": f"{cls.__name__} {model_id} does not exist"}, 404)) | ||
|
|
||
| return model |
There was a problem hiding this comment.
Nice job creating this reusable function & keeping your code DRY. Nice approach to handling an invalid model 😁
|
|
||
| ### | ||
|
|
||
| @tasks_bp.route("", methods=['POST']) |
| if "completed_at" in request_body: | ||
| new_task.completed_at=request_body["completed_at"] | ||
|
|
||
| db.session.add(new_task) |
There was a problem hiding this comment.
Great job using db.session.<method-name>. Session gives us access to the follow:
dbis our app’s instance of SQLAlchemy.sessionrepresents our active database connection.- By referencing
db.sessionwe can use SQLAlchemy’s methods to perform tasks like:- committing a change to a model
- storing a new record of a model
- deleting a record.
- By referencing
db.session.add()you are able to use the SQLAlchemy method to store a new record of the task model
| for task_id in request_body["task_ids"]: | ||
| task = validate_model(Task, task_id) | ||
| if task not in goal.tasks: | ||
| goal.tasks.append(task) | ||
|
|
||
|
|
||
| task_ids=[] | ||
| for task in goal.tasks: | ||
| task_ids.append(task.task_id) | ||
|
|
||
| db.session.commit() |
| @@ -4,19 +4,20 @@ | |||
| import os | |||
| from dotenv import load_dotenv | |||
There was a problem hiding this comment.
Great work! We need to import load_dotenv to set up environment variables in our .env file
| db = SQLAlchemy() | ||
| migrate = Migrate() | ||
| load_dotenv() |
| from dotenv import load_dotenv | ||
|
|
||
|
|
||
| db = SQLAlchemy() |
| app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False | ||
|
|
||
| if test_config is None: | ||
| app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( |
| @@ -30,5 +31,9 @@ def create_app(test_config=None): | |||
| migrate.init_app(app, db) | |||
There was a problem hiding this comment.
Great job! We need to pass our instance of our app & the instance of our SQLALchemy db to connect the db and migrate to our Flask app
| # Register Blueprints here | ||
| from .routes import goals_bp | ||
| app.register_blueprint(goals_bp) | ||
| from .routes import tasks_bp | ||
| app.register_blueprint(tasks_bp) |
There was a problem hiding this comment.
Great work registering these blueprints ✅
| assert response.status_code == 404 | ||
| assert "message" in response_body | ||
| assert response_body["message"] == "Task 1 does not exist" |
There was a problem hiding this comment.
Nice work with this test completion! This assertion is a great way to validate functionality is working as expected
| return { | ||
| "goal": new_goal.to_dict() | ||
| }, 201 |
There was a problem hiding this comment.
Usually a resource creation relates to a 201 response code 👍🏾
No description provided.