Skip to content
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

Amethyst - Megan G #112

Open
wants to merge 49 commits into
base: main
Choose a base branch
from
Open

Amethyst - Megan G #112

wants to merge 49 commits into from

Conversation

scrambledmegs
Copy link

No description provided.

}

@classmethod
def from_dict(cls, goal_data):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice class function!

goal = db.relationship("Goal", back_populates ="tasks")

def to_dict(self):
if self.goal_id:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You always are so good at implementing guard clauses!

def create_task():
request_body = request.get_json()

if not request_body.get("title") or not request_body.get("description"):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 True

We can pass in the request_body and a list of strings that are keys and then check to see if those keys are present.

Comment on lines +35 to +41
title_query = request.args.get("sort")
if title_query == "asc":
tasks = Task.query.order_by(Task.title).all()
elif title_query == "desc":
tasks = Task.query.order_by(Task.title.desc()).all()
else:
tasks = Task.query.all()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on having the database sort the tasks for you!

Comment on lines +62 to +63
task.title = request_body["title"]
task.description = request_body["description"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, if a user sends a request without the keys title or description 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.

"Authorization": f"Bearer {os.environ.get('SLACK_KEY')}"
}

slack_request = requests.post(slack_url, headers=headers, json=slack_data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on having your Slack post sent after the logic of marking a task complete! We don't want to send out any false positive alerts just in case our logic fails during the update!

Comment on lines +123 to +134
def validate_model(cls, model_id):
try:
model_id = int(model_id)
except:
abort(make_response({"details":f"Invalid data"}, 400))

model = cls.query.get(model_id)

if not model:
abort(make_response({"details":f"{cls.__name__} ID not found"}, 404))

return model

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure that your helper functions are in a place that is easy to find, I typically put them in the top of the file or in another folder somewhere else.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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! ✨💫🤭

Comment on lines +36 to +38
for task_id in request_body["task_ids"]:
task = validate_model(Task, task_id)
task.goal_id = goal.goal_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice for loop, Megan! You ate this! 🤭

Comment on lines +72 to +74
tasks_response = []
for task in goal.tasks:
tasks_response.append(task.to_dict())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you think your code would change if you made this logic into a class function?


request_body = request.get_json()

goal.title = request_body["title"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer to my comment about error handling, on line 62 in task_routes.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants