Skip to content

Conversation

@Yf-Monica-Bao
Copy link

No description provided.

Copy link

@kelsey-steven-ada kelsey-steven-ada 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 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
Comment on lines 21 to 22
app.config['JSON_SORT_KEYS'] = False # Don't sort keys alphabetically
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

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.

Copy link
Author

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.

Comment on lines 21 to 24
moon_names = []
for moon in self.moons:
moon_names.append(moon.name)
planet_as_dict["moons"] = moon_names

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]

Comment on lines 30 to 33
new_planet = Planet(name=planet_data["name"],
description=planet_data["description"],
gravity = planet_data['gravity'],
distance_from_earth = planet_data['distance_from_earth'])

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

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.

Comment on lines 25 to 30
result_dict = {
"id": moon_response.id,
"name": moon_response.name,
}

return make_response(jsonify(result_dict), 200)

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.


planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets")

#---------------------------------------------- Helper Functions----------------------------------------------

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

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.

Comment on lines 87 to 94
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)

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

Choose a reason for hiding this comment

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

Great consideration!

Comment on lines 449 to 453
# ---------------------------------------
# ---------------------------------------
# ---------Moon route tests--------------
# ---------------------------------------
# ---------------------------------------

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.

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.

4 participants