-
Notifications
You must be signed in to change notification settings - Fork 79
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
Ihovanna + Maria Fernanda #41
base: main
Are you sure you want to change the base?
Changes from all commits
8af0f79
0bbe40c
912fc68
d040581
25ac4ab
886a2d1
4e374b7
7dc2a1c
0a19d23
252d9a1
0d4a0b4
dc978e0
53cca8a
e541508
9ee2e9e
395f104
e8568ac
b64177f
13511c4
cb05168
4bbe9e1
c04f19d
a7a1396
95b8385
fb3836f
cca6ced
f6d343b
3b1548c
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,141 +1 @@ | ||
.vscode | ||
.DS_Store | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"python.testing.pytestArgs": [ | ||
"tests" | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,31 @@ | ||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
from flask_migrate import Migrate | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
|
||
db = SQLAlchemy() | ||
migrate = Migrate() | ||
load_dotenv() | ||
|
||
def create_app(test_config=None): | ||
app = Flask(__name__) | ||
|
||
return app | ||
if not test_config: | ||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | ||
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("SQLALCHEMY_DATABASE_URI") | ||
else: | ||
app.config["TESTING"] = True | ||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False | ||
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLALCHEMY_TEST_DATABASE_URI") | ||
|
||
db.init_app(app) | ||
migrate.init_app(app, db) | ||
|
||
from app.models.planet_model import Planet | ||
|
||
from .routes import planets_bp | ||
app.register_blueprint(planets_bp) | ||
|
||
return app |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from app import db | ||
|
||
class Planet(db.Model): | ||
id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
name = db.Column(db.String) | ||
description = db.Column(db.String) | ||
|
||
|
||
''' | ||
Planet descriptions for Postman POST HTML requests bodies: | ||
{ | ||
"name": "Mercury", | ||
"description": "The smallest planet in our solar system and closest to the Sun. It is only slightly larger than Earth's Moon." | ||
} | ||
{ | ||
"name": "Venus", | ||
"description": "Spins slowly in the opposite direction from most planets. A thick atmosphere traps heat in a runaway greenhouse effect." | ||
} | ||
|
||
{ | ||
"name": "Earth", | ||
"description": "The only place that's inhabited by living things. Also, the only planet in our solar system with liquid water on the surface." | ||
} | ||
|
||
{ | ||
"name": "Mars", | ||
"description": "A dusty, cold desert world with a very thin atmosphere." | ||
} | ||
{ | ||
"name": "Jupiter", | ||
"description": "More than twice as massive than other planets in our solar system combined." | ||
} | ||
{ | ||
"name": "Saturn", | ||
"description": "Adorned with a dazzling, complex system of icy rings." | ||
} | ||
{ | ||
"name": "Uranus", | ||
"description": "Rotates at a nearly 90-degree angle from the plane of its orbit." | ||
} | ||
{ | ||
"name": "Neptune", | ||
"description": "Is dark, cold and whipped by supersonic winds. First planet located through mathematical calculations." | ||
} | ||
''' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,82 @@ | ||
from flask import Blueprint | ||
from app import db | ||
from flask import Blueprint, jsonify, make_response, abort, request | ||
from app.models.planet_model import Planet | ||
|
||
planets_bp = Blueprint('planets_bp', __name__, url_prefix='/planets') | ||
|
||
@planets_bp.route("", methods=["POST"]) | ||
def create_planet(): | ||
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. Some input validation on the request body would be appropriate here. |
||
request_body = request.get_json() | ||
new_planet = Planet( | ||
name=request_body["name"], | ||
description=request_body["description"] | ||
) | ||
|
||
db.session.add(new_planet) | ||
db.session.commit() | ||
|
||
return make_response(f"Planet {new_planet.name} has been added to the Planets database.", 201) | ||
|
||
|
||
@planets_bp.route("", methods=["GET"]) | ||
def all_planets(): | ||
planets_response = [] | ||
planets = Planet.query.all() | ||
|
||
for planet in planets: | ||
planets_response.append({ | ||
"id": planet.id, | ||
"name": planet.name, | ||
"description": planet.description | ||
}) | ||
Comment on lines
+27
to
+31
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. This would be a great place to use that helper method I listed in the Planet Class. |
||
|
||
return jsonify(planets_response) | ||
|
||
|
||
@planets_bp.route("/<planet_id>", methods=["GET"]) | ||
def handle_planet(planet_id): | ||
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. Why name this |
||
planet = validate_planet(planet_id) | ||
|
||
return { | ||
"id": planet.id, | ||
"name": planet.name, | ||
"description": planet.description | ||
} | ||
|
||
|
||
@planets_bp.route("/<planet_id>", methods=["PUT"]) | ||
def update_planet(planet_id): | ||
planet = validate_planet(planet_id) | ||
request_body = request.get_json() | ||
|
||
planet.name = request_body["name"] | ||
planet.description = request_body["description"] | ||
|
||
db.session.commit() | ||
|
||
return make_response(f"Planet {planet.name} has been updated in the Planets database.", 201) | ||
|
||
|
||
|
||
|
||
@planets_bp.route("/<planet_id>", methods=["DELETE"]) | ||
def delete_planet(planet_id): | ||
planet = validate_planet(planet_id) | ||
|
||
db.session.delete(planet) | ||
db.session.commit() | ||
|
||
return make_response(f"Planet {planet.name} has been deleted from the Planets database.", 200) | ||
|
||
|
||
def validate_planet(planet_id): | ||
try: | ||
planet_id = int(planet_id) | ||
except: | ||
abort(make_response({"message":f"Planet {planet_id} invalid"}, 400)) | ||
|
||
planet = Planet.query.get(planet_id) | ||
if not planet: | ||
abort(make_response({"message":f"Planet {planet_id} not found"}, 404)) | ||
|
||
return planet |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Generic single-database configuration. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# A generic, single database configuration. | ||
|
||
[alembic] | ||
# template used to generate migration files | ||
# file_template = %%(rev)s_%%(slug)s | ||
|
||
# set to 'true' to run the environment during | ||
# the 'revision' command, regardless of autogenerate | ||
# revision_environment = false | ||
|
||
|
||
# Logging configuration | ||
[loggers] | ||
keys = root,sqlalchemy,alembic | ||
|
||
[handlers] | ||
keys = console | ||
|
||
[formatters] | ||
keys = generic | ||
|
||
[logger_root] | ||
level = WARN | ||
handlers = console | ||
qualname = | ||
|
||
[logger_sqlalchemy] | ||
level = WARN | ||
handlers = | ||
qualname = sqlalchemy.engine | ||
|
||
[logger_alembic] | ||
level = INFO | ||
handlers = | ||
qualname = alembic | ||
|
||
[handler_console] | ||
class = StreamHandler | ||
args = (sys.stderr,) | ||
level = NOTSET | ||
formatter = generic | ||
|
||
[formatter_generic] | ||
format = %(levelname)-5.5s [%(name)s] %(message)s | ||
datefmt = %H:%M:%S |
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.
some helper methods like
Might be helpful