Conversation
| description=task_data["description"] | ||
| ) | ||
|
|
||
| return new_task No newline at end of file |
| try: | ||
| new_goal = Goal.from_dict(request_body) | ||
| except: | ||
| return {"details": "Invalid data"}, 400 |
There was a problem hiding this comment.
Like we validated models, could we also make a general function that validates request bodies? Here's an example:
def validate_request_body(request_body, keys):
for key in keys:
if not request_body.get(key):
abort(make_response({
'Invalid Data': f'missing key: {key}'
}, 400))
return TrueWe can pass in the request_body and a list of strings that are keys and then check to see if those keys are present.
|
|
||
| request_body = request.get_json() | ||
|
|
||
| for i in request_body["task_ids"]: |
| for goal in goals: | ||
| goals_response.append(goal.to_dict()) |
There was a problem hiding this comment.
How would your code look different if you were to implement this into a class function?
|
|
||
| request_body = request.get_json() | ||
|
|
||
| goal.title = request_body["title"] |
There was a problem hiding this comment.
Right now, if a user sends a request without the key title your server would crash. There's a couple of ways to handle this, you could call the validate_request function before you access the keys in request_body or you could implement a try/except block.
| if sorting_query == "asc": | ||
| tasks = Task.query.order_by(Task.title.asc()).all() | ||
| elif sorting_query == "desc": | ||
| tasks = Task.query.order_by(Task.title.desc()).all() | ||
| else: | ||
| tasks = Task.query.all() |
There was a problem hiding this comment.
Great work on making the database do the heavy lifting when it comes to sorting the tasks!
| "is_complete": (task.completed_at != None) | ||
| } | ||
| } | ||
| else: |
There was a problem hiding this comment.
Another way you could implement this by changing your to_dict method for Task to include a goal_id if one is present.
| "channel": "C057EA9H4G7", | ||
| "text": slack_message | ||
| } | ||
| response = requests.post("https://slack.com/api/chat.postMessage", headers=header, json=json_body) |
There was a problem hiding this comment.
Our code could possibly fail while trying to commit the change to our database and therefore sending the message to Slack could be a false positive. We should send alerts like these at the very end of our logic just in case something goes wrong.
| db.session.commit() | ||
|
|
||
| return abort(make_response({"details":f"Task {task_id} \"{task.title}\" successfully deleted"}, 200)) | ||
|
|
There was a problem hiding this comment.
Well done on this project, I didn't have much to comment on and that is a good thing! Keep up the good work! Really looking forward to what you create in the frontend! Please feel free to reach out if you have any questions about the feedback that I left! ✨💫🤭
No description provided.