-
Notifications
You must be signed in to change notification settings - Fork 20
C22 Sphinx Priyanka #2
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?
Changes from all commits
f726d95
6a59b6c
5004593
5be63d9
65cf83c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| from flask import Flask | ||
| from app.routes.planet_routes import planets_bp | ||
|
|
||
|
|
||
| def create_app(test_config=None): | ||
| app = Flask(__name__) | ||
|
|
||
| app.register_blueprint(planets_bp) | ||
|
|
||
| return app |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | ||||||
| class Planet: | ||||||
| def __init__(self, id, name, description, distance_from_sun): | ||||||
| self.id = id | ||||||
| self.name = name | ||||||
| self.description = description | ||||||
| self.distance_from_sun = distance_from_sun | ||||||
|
|
||||||
| planets =[ | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Nit: missing whitespace after = |
||||||
| Planet(1, "Mercury", "smallest and hottest planet", 57.9), | ||||||
| Planet(2, "Venus", "second planet from the sun", 108.2), | ||||||
| Planet(3, "Earth", "our home planet", 149.6), | ||||||
| Planet(4, "Mars", "the red planet", 227.9), | ||||||
| Planet(5, "Jupiter", "largest planet in the solar system", 778.3), | ||||||
| Planet(6, "Saturn", "has beautiful rings", 1437.0), | ||||||
| Planet(7, "Uranus", "rotates on its side", 2871.0), | ||||||
| Planet(8, "Neptune", "farthest planet from the sun", 4497.1) | ||||||
| ] | ||||||
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,50 @@ | ||||||||||||||||||||||||||||||
| from flask import Blueprint, abort, make_response | ||||||||||||||||||||||||||||||
| from app.models.planet import planets | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @planets_bp.get("") | ||||||||||||||||||||||||||||||
| def get_all_planets(): | ||||||||||||||||||||||||||||||
| result_planets = [] | ||||||||||||||||||||||||||||||
| for planet in planets: | ||||||||||||||||||||||||||||||
| result_planets.append( | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| "id" : planet.id, | ||||||||||||||||||||||||||||||
| "name": planet.name, | ||||||||||||||||||||||||||||||
| "description" : planet.description, | ||||||||||||||||||||||||||||||
| "distance_from_sun" : planet.distance_from_sun | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+11
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent white spaces
Suggested change
|
||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| return result_planets | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Helper function for error handling | ||||||||||||||||||||||||||||||
| def validate_planet(id): | ||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||
| input_id = int(id) | ||||||||||||||||||||||||||||||
| except: | ||||||||||||||||||||||||||||||
| abort(make_response(f"Planet id: '{id}' is invalid. Enter a valid planet number from 1 to 8.", 400)) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for planet in planets: | ||||||||||||||||||||||||||||||
| if planet.id == input_id: | ||||||||||||||||||||||||||||||
| return planet | ||||||||||||||||||||||||||||||
| abort(make_response(f"Planet id: '{id}' not found. Enter planet id from 1 to 8", 404)) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @planets_bp.get("/<id>") | ||||||||||||||||||||||||||||||
| def get_one_planet(id): | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| planet = validate_planet(id) | ||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||
| "id" : planet.id, | ||||||||||||||||||||||||||||||
| "name" : planet.name, | ||||||||||||||||||||||||||||||
| "description" : planet.description, | ||||||||||||||||||||||||||||||
| "distance_from_sun" : planet.distance_from_sun | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+40
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs one more level of indentation
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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.
How would you write the
to_dictinstance method so you can take an instance of Planet and turn it into a dictionary that will be returned as a response to a client's request?