-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cedar - Maria O. #91
base: master
Are you sure you want to change the base?
Cedar - Maria O. #91
Conversation
… modified task model logic for calculating is_complete
…lf-written tests)
…e tasks of one goal, Wave 06 passing except for last test
…_task so that goal_id is included in response if the task is related to a goal, passing last test on Wave 06
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work on this complex Flask project! You've met the learning goals around writing tests and creating relationships with SQLAlchemy. Nice work implementing instance methods. I've left a few in-line comments on ways you may consider refactoring. Please let me know if you have any questions. Nice work!
def to_dict(self): | ||
return { | ||
"id": self.task_id, | ||
"title": self.title, | ||
"description": self.description, | ||
# "is_complete": True if self.completed_at == True else False | ||
"is_complete": bool(self.completed_at) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider how you could refactor this to include the goal_id when self.goal_id
is not null (None), then look below for one possible solution.
dict = {
"id": self.task_id,
"title": self.title,
"description": self.description,
# "is_complete": True if self.completed_at == True else False
"is_complete": bool(self.completed_at)
}
if self.goal_id:
dict["goal_id"] = self.goal_id
return dict
"is_complete": bool(self.completed_at) | ||
} | ||
|
||
def is_related_to_goal(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is related to goal! Note how the suggested refactor above negates the need for this check.
# relationship | ||
tasks = db.relationship("Task", backref="goal", lazy=True) | ||
|
||
def to_dict(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work making these instance methods
goals_bp = Blueprint('goals', __name__, url_prefix='/goals') | ||
|
||
# # Helper functions | ||
def valid_int(id): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that you have this function in goal_routes and task_routes. Consider making a separate file with shared helper functions and importing them where you need them to DRY up your code.
@goals_bp.route("", methods=["POST"], strict_slashes=False) | ||
def create_goal(): | ||
request_body = request.get_json() | ||
valid_goal(request_body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great use of a helper function. You might consider also including the creation of the goal in valid_goal
such that the function either aborts because of Invalid data or returns the goal instance.
for goal in goal_objects: | ||
response_list.append(goal.to_dict()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a nice place to use a list comprehension.
task_ids_list = request_body["task_ids"] | ||
|
||
for task_id in task_ids_list: | ||
task = Task.query.get(task_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably double check that the task ids in the request body actually correspond to existing tasks. We can do that with a get_or_404
or a check on whether the task is None.
Also note that add_tasks_to_goal_dict
does the work of creating a list of task ids. That list has already been handed in as part of the request body, so this extra work is not necessary.
@tasks_bp.route("", methods=["POST"], strict_slashes=False) | ||
def create_task(): | ||
request_body = request.get_json() | ||
valid_task(request_body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just like with goal, consider using valid_task
to return a task instance.
"title": "Updated Goal Title" | ||
} | ||
} | ||
goal = Goal.query.get(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work querying the database to make sure the change was persisted there.
No description provided.