-
Notifications
You must be signed in to change notification settings - Fork 20
Sphinx-Denise and Olga #16
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 2 commits
36bf881
03def41
36bd4ad
71447d2
ed9bbab
76828e9
4dbec01
fbf10d7
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 .routes.planet_routes import planets_bp | ||
|
|
||
|
|
||
| def create_app(test_config=None): | ||
| def create_app(): | ||
| app = Flask(__name__) | ||
|
|
||
| return app | ||
| # Register Blueprints here | ||
| 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): | ||
| self.id = id | ||
| self.name = name | ||
| self.description = description | ||
|
|
||
| planets = [ | ||
| Planet(1, "Neptune", "Giant and icy planet"), | ||
| Planet(2, "Uranus", "Rotates on its side"), | ||
| Planet(3, "Saturn", "Has beautiful rings"), | ||
| Planet(4, "Jupiter", "Largest planet in the solar system"), | ||
| Planet(5, "Mars", "The red planet"), | ||
| Planet(6, "Earth", "Our home planet"), | ||
| Planet(7, "Venus", "Second planet from the sun"), | ||
| Planet(8, "Mercury", "Smallest and hottest planet"), | ||
| Planet(6, "Pluto", "The planet they declassified as a planet") | ||
| ] | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,62 @@ | ||||||||||||||||||||||||||
| from flask import Blueprint, abort, make_response, request | ||||||||||||||||||||||||||
| from app.models.planet import planets | ||||||||||||||||||||||||||
| # from ..db import db | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @planets_bp.get("") | ||||||||||||||||||||||||||
| def get_all_planets(): | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| planets_response = [] | ||||||||||||||||||||||||||
| for planet in planets: | ||||||||||||||||||||||||||
| planets_response.append( | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| "id": planet.id, | ||||||||||||||||||||||||||
| "name": planet.name, | ||||||||||||||||||||||||||
| "description": planet.description | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+34
to
+39
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. Prefer your Planet model has a to_dict method so that it can be called in your different routes instead repeating code. It would also help your routes be more concise. |
||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| return planets_response | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @planets_bp.get("/<planet_id>") | ||||||||||||||||||||||||||
| def get_one_planet(planet_id): | ||||||||||||||||||||||||||
| planet = validate_planet(planet_id) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| for planet in planets: | ||||||||||||||||||||||||||
| if planet.id == int(planet_id): | ||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||
| "id": planet.id, | ||||||||||||||||||||||||||
| "name": planet.name, | ||||||||||||||||||||||||||
| "description": planet.description, | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| for planet in planets: | |
| if planet.id == int(planet_id): | |
| return { | |
| "id": planet.id, | |
| "name": planet.name, | |
| "description": planet.description, | |
| } | |
| return { | |
| "id": planet.id, | |
| "name": planet.name, | |
| "description": planet.description, | |
| } |
Outdated
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.
We'll have a look at how the model needs to be updated in order for the POST route to work!
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?