Skip to content

Cedar: Rae Elwell & Khandice Schuhmann #8

Open
khandices wants to merge 15 commits intoAda-C16:mainfrom
khandices:main
Open

Cedar: Rae Elwell & Khandice Schuhmann #8
khandices wants to merge 15 commits intoAda-C16:mainfrom
khandices:main

Conversation

@khandices
Copy link
Copy Markdown

Part 1

Copy link
Copy Markdown

@beccaelenzil beccaelenzil left a comment

Choose a reason for hiding this comment

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

🎉 Nice work!

Comment thread app/routes.py Outdated
Comment on lines +8 to +16
self.matter = matter


planets = [Planet(1, "Mercury", "small and red", "solid"),
Planet(5, "Jupiter", "big and swirly", "gaseous"),
Planet(6, "Saturn", "rings and swirls", "gaseous")]


planets_bp = Blueprint("planets", __name__, url_prefix="/planets")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor note: to enhance readability, consider removing some blank lines and put each Planet instance on it's own line

Suggested change
self.matter = matter
planets = [Planet(1, "Mercury", "small and red", "solid"),
Planet(5, "Jupiter", "big and swirly", "gaseous"),
Planet(6, "Saturn", "rings and swirls", "gaseous")]
planets_bp = Blueprint("planets", __name__, url_prefix="/planets")
self.matter = matter
planets = [
Planet(1, "Mercury", "small and red", "solid"),
Planet(5, "Jupiter", "big and swirly", "gaseous"),
Planet(6, "Saturn", "rings and swirls", "gaseous")
]
planets_bp = Blueprint("planets", __name__, url_prefix="/planets")

Comment thread app/routes.py Outdated
def get_all_planets():
planet_list = []
for planet in planets:
planet_list.append(vars(planet))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great use of vars!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I just learned that vars will cause problems when we have a class that connects to our database. We can achieve the same effect by writing a to_json(self) instance method on the Planet class.

Comment thread app/routes.py Outdated
Comment on lines +27 to +28
if not planet_id.isdigit():
return("Not a number!")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice work handling an invalid parameter. I learned from Auberon for some strange pythonic reasons, isdigit actually shouldn't be used here, and we prefer try/except

Suggested change
if not planet_id.isdigit():
return("Not a number!")
try:
planet_id = int(planet_id)
except:
return ("Not a number!", 400)

Comment thread app/routes.py Outdated
for planet in planets:
if planet.id == planet_id:
return jsonify(vars(planet))
return ("Not Found!") No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice work handling a planet that was not found. We can return a 404 response code with this:

Suggested change
return ("Not Found!")
return ("Not Found!", 404)

Copy link
Copy Markdown

@beccaelenzil beccaelenzil left a comment

Choose a reason for hiding this comment

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

Great work on your first Flask project! You've made great use of helper functions and the instance method to_dict(). Your code is very readable. I've made a few suggestions for small refactors and ways to handle 400 level responses. Please let me know if you have any questions. 🎃

Comment thread app/routes.py
Comment on lines +31 to +33
new_planet = Planet(name=request_body["name"],
description=request_body["description"],
matter=request_body["matter"])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here's a suggestion for some input handling. If the request body is missing one of the keys, this code will raise a KeyError exception. Consider handling this way:

Suggested change
new_planet = Planet(name=request_body["name"],
description=request_body["description"],
matter=request_body["matter"])
if "name" not in request_body or "description" not in request_body or "matter" not in request_body:
return {"error": "incomplete request body"}, 400
new_planet = Planet(name=request_body["name"],
description=request_body["description"],
matter=request_body["matter"])

Comment thread app/routes.py


@planets_bp.route("/<planet_id>", methods=["PUT"])
def update_planet(planet_id):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice work separating out PUT and PATCH.

For PUT, consider how you could handle an incomplete request body (similar to the suggestion in POST)

Comment thread app/routes.py
Comment on lines +14 to +15
if make_input_valid(parameter_id) is not None:
return make_input_valid(parameter_id)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python allows to simplify this conditional to check for truthy values:

Suggested change
if make_input_valid(parameter_id) is not None:
return make_input_valid(parameter_id)
if make_input_valid(parameter_id):
return make_input_valid(parameter_id)

Comment thread app/routes.py
Comment on lines +42 to +43
if is_parameter_valid(planet_id) is not None:
return is_parameter_valid(planet_id)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor note: similar to above, we don't need the is not None

Suggested change
if is_parameter_valid(planet_id) is not None:
return is_parameter_valid(planet_id)
if is_parameter_valid(planet_id):
return is_parameter_valid(planet_id)

Comment thread app/routes.py
@planets_bp.route("/<planet_id>", methods=["DELETE"])
def delete_planet(planet_id):
if is_parameter_valid(planet_id) is not None:
return is_parameter_valid(planet_id)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great use of this helper method throughout!

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.

3 participants