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

Cedar: Rae Elwell & Khandice Schuhmann #8

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

Conversation

khandices
Copy link

Part 1

Copy link

@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!

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")

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")

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

Choose a reason for hiding this comment

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

Great use of vars!

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.

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

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)

app/routes.py Outdated
for planet in planets:
if planet.id == planet_id:
return jsonify(vars(planet))
return ("Not Found!")

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

@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 on lines +31 to +33
new_planet = Planet(name=request_body["name"],
description=request_body["description"],
matter=request_body["matter"])

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"])



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

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 on lines +14 to +15
if make_input_valid(parameter_id) is not None:
return make_input_valid(parameter_id)

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 on lines +42 to +43
if is_parameter_valid(planet_id) is not None:
return is_parameter_valid(planet_id)

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)

@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)

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