Conversation
beccaelenzil
left a comment
There was a problem hiding this comment.
Great work on your first Flask project. Your code is clear and logical. Your consistent spacing and indentation made it a pleasure to read. Good use of instance methods on the Task and Goal model to DRY up your code. I've left a few inline comments on ways you could consider refactoring. Please let me know if you have any questions.
| return self.completed_at != None | ||
|
|
||
|
|
||
| def make_json(self): |
There was a problem hiding this comment.
Good use of a helper method. Consider DRYing up your code by building the dictionary
json =
{"id": self.task_id,
"title": self.title,
"description": self.description,
"is_complete": self.is_complete()}
and conditionally adding the goal_id like this:
if self.goal_id:
json["goal_id] = self.goal_id
and then returning {"task": json}
| "id": task.task_id, | ||
| "title": task.title, | ||
| "description": task.description, | ||
| "is_complete": task.is_complete() |
There was a problem hiding this comment.
We could use the task instance method here task.make_json to DRY up the code and reduce room for errors.
| }) | ||
| return jsonify(tasks_response) | ||
|
|
||
| elif request.method == "POST": |
There was a problem hiding this comment.
With regards to the point in separating out the functions for each route (that you noted in Learn), this way of organizing your code can increase readability by reducing indentation and making meaningful function names. For instance, if we separate out GET and POST, we might call one function def all_tasks() and the other def create_task().
| task = Task.query.get(task_id) | ||
|
|
||
| if task is None: | ||
| return make_response("", 404) |
There was a problem hiding this comment.
We could use get_or_404() method here
https://flask-sqlalchemy.palletsprojects.com/en/2.x/api/#flask_sqlalchemy.BaseQuery.get_or_404
| API_KEY = os.environ.get("API_KEY") | ||
| PATH = "https://slack.com/api/chat.postMessage" | ||
| query_params = { | ||
| "channel": "task-notifications", | ||
| "text": f"Someone just completed the task {task.title}." | ||
| } | ||
| task.completed_at = datetime.utcnow() | ||
| db.session.commit() | ||
| requests.post(PATH, data=query_params, headers={"Authorization":API_KEY}) |
There was a problem hiding this comment.
Consider moving this functionality into a helper function increase readability and changeability.
No description provided.