-
Notifications
You must be signed in to change notification settings - Fork 11
Ocelots --- Monica Bao & Jennifer Dai #13
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Jewelhae <[email protected]>
kelsey-steven-ada
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work Monica & Jennifer! I've left a few comments & questions, feel free to reply here or message me on Slack if you have questions on the feedback 🙂
app/__init__.py
Outdated
| app.config['JSON_SORT_KEYS'] = False # Don't sort keys alphabetically | ||
| app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lines app.config['JSON_SORT_KEYS'] = False and app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False is duplicated in both parts of the if/else. We could move those lines to either above or below the if/else, so they only need to be written once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello Kelsey, Thank you for all of this helpful feedback. I have fixed the code based on your comments.
app/models/planet.py
Outdated
| moon_names = [] | ||
| for moon in self.moons: | ||
| moon_names.append(moon.name) | ||
| planet_as_dict["moons"] = moon_names |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is the way I grabbed cat names during the class livecode, but this is a great place for a list comprehension!
planet_as_dict["moons"] = [moon.name for moon in self.moons]
app/models/planet.py
Outdated
| new_planet = Planet(name=planet_data["name"], | ||
| description=planet_data["description"], | ||
| gravity = planet_data['gravity'], | ||
| distance_from_earth = planet_data['distance_from_earth']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a completely valid way to break the line up, but I wanted to share another format that's common across languages. This involves dropping all the arguments to their own lines. The benefit of this is making the arguments very clear from other statements and the use of indentation to show we're inside something spanning multiple lines:
# Original code
new_planet = Planet(name=planet_data["name"],
description=planet_data["description"],
gravity = planet_data['gravity'],
distance_from_earth = planet_data['distance_from_earth'])
# Alternate formatting
new_planet = Planet(
name=planet_data["name"],
description=planet_data["description"],
gravity = planet_data['gravity'],
distance_from_earth = planet_data['distance_from_earth']
)| @@ -0,0 +1,69 @@ | |||
| from flask import Blueprint, jsonify, abort, make_response, request | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the route files are loose in the app folder. To keep a project organized as it grows, I recommend creating a routes folder inside of app to place these in.
app/moon_routes.py
Outdated
| result_dict = { | ||
| "id": moon_response.id, | ||
| "name": moon_response.name, | ||
| } | ||
|
|
||
| return make_response(jsonify(result_dict), 200) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Moon has a to_dict function we could reduce similar code by calling that function here to get the dictionary to return to the user.
app/planet_routes.py
Outdated
|
|
||
| planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") | ||
|
|
||
| #---------------------------------------------- Helper Functions---------------------------------------------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specific teams might have their own style guides, but it is a common practice to place helper functions at the end of a file rather than the beginning. An often-used analogy is to lay out code like a newspaper article. Headlines and the most important information comes first - Functions that use other functions in the file go at the top. That gets followed by the details and extra information in an article, or in our code, all the helper functions that support the higher up functions.
|
|
||
| split_sort = is_sort.split(":") | ||
|
|
||
| if len(split_sort) == 2: # Case: ?sort=attribute:asc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the flexibility here for sorting and filtering. As Ilana shared in the wrap-up session, in practice we would usually change this into 2 separate request arguments rather than splitting the attribute from the sort direction on the backend.
app/planet_routes.py
Outdated
| if attribute == "name": | ||
| planet_query = sort_helper(planet_query, Planet.name, sort_method) | ||
| elif attribute == "distance_from_earth": | ||
| planet_query = sort_helper(planet_query, Planet.distance_from_earth, sort_method) | ||
| elif attribute == "gravity": | ||
| planet_query = sort_helper(planet_query, Planet.gravity, sort_method) | ||
| else: # If user don't specify any attribute, we would sort by name | ||
| planet_query = sort_helper(planet_query, Planet.name, sort_method) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first and last case duplicates code. If name is our default, we could remove the explicit check for attribute == "name" since if the user didn't specifically choose distance_from_earth or gravity, name will be used.
if attribute == "distance_from_earth":
planet_query = sort_helper(planet_query, Planet.distance_from_earth, sort_method)
elif attribute == "gravity":
planet_query = sort_helper(planet_query, Planet.gravity, sort_method)
else: # If user don't specify any attribute, we would sort by name
planet_query = sort_helper(planet_query, Planet.name, sort_method)| def delete_planet_by_id(planet_id): | ||
| planet = validate_model(Planet, planet_id) | ||
|
|
||
| # Delete moon when we delete the planet they associate to to avoid having dangling nodes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great consideration!
tests/test_routes.py
Outdated
| # --------------------------------------- | ||
| # --------------------------------------- | ||
| # ---------Moon route tests-------------- | ||
| # --------------------------------------- | ||
| # --------------------------------------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the length of this file, I would suggest splitting out the tests for moon routes into their own file.
No description provided.