From 3e0b4c00cc813074a0395932afcfb32fc16c8550 Mon Sep 17 00:00:00 2001 From: louloera Date: Thu, 20 Apr 2023 11:55:59 -0700 Subject: [PATCH 01/26] Instances of planets --- app/routes.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..b8a2c82d2 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,19 @@ from flask import Blueprint +class Planet(): + + def __init__(self, id, name, description, place): + self.id = id + self.name = name + self.description = description + self.place = place + +Mercury = Planet(1, "Mercury", "filler", "First") +Venus = Planet(2, "Venus", "Filler", "Second") +Earth = Planet (3, "Earth", "Filler", "Third") +Mars = Planet(4, "Mars", "Filler", "Forth") +Jupiter = Planet (5, "Jupiter", "Filler", "Fifth") +Saturn= Planet (6, "Saturn", "Filler", "Sixth") +Uranus= Planet (7, "Uranus", "Filler", "Seventh") +Neptune = Planet(8, "Neptune", "Filler", "Eighth") + From 938e61eb2815f801eb14af271d12a2b2f7b725be Mon Sep 17 00:00:00 2001 From: Charlie Date: Thu, 20 Apr 2023 12:02:18 -0700 Subject: [PATCH 02/26] created list of planets --- app/routes.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/routes.py b/app/routes.py index b8a2c82d2..83f845dd0 100644 --- a/app/routes.py +++ b/app/routes.py @@ -17,3 +17,8 @@ def __init__(self, id, name, description, place): Uranus= Planet (7, "Uranus", "Filler", "Seventh") Neptune = Planet(8, "Neptune", "Filler", "Eighth") +Planets = [Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune] +#...to get all existing planets, +# so that I can see a list of planets, +# with their id, name, description, +# and other data of the planet. \ No newline at end of file From 875405f5b644dcddb538b3fd035966e77ee0f87e Mon Sep 17 00:00:00 2001 From: Charlie Date: Thu, 20 Apr 2023 12:53:57 -0700 Subject: [PATCH 03/26] fixed listed instances of planets --- app/routes.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app/routes.py b/app/routes.py index 83f845dd0..24d1ac65b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -8,16 +8,17 @@ def __init__(self, id, name, description, place): self.description = description self.place = place -Mercury = Planet(1, "Mercury", "filler", "First") -Venus = Planet(2, "Venus", "Filler", "Second") -Earth = Planet (3, "Earth", "Filler", "Third") -Mars = Planet(4, "Mars", "Filler", "Forth") -Jupiter = Planet (5, "Jupiter", "Filler", "Fifth") -Saturn= Planet (6, "Saturn", "Filler", "Sixth") -Uranus= Planet (7, "Uranus", "Filler", "Seventh") -Neptune = Planet(8, "Neptune", "Filler", "Eighth") +Planets = [ + Planet(1, "Mercury", "filler", "First"), + Planet(2, "Venus", "Filler", "Second"), + Planet(3, "Earth", "Filler", "Third"), + Planet(4, "Mars", "Filler", "Forth"), + Planet(5, "Jupiter", "Filler", "Fifth"), + Planet(6, "Saturn", "Filler", "Sixth"), + Planet(7, "Uranus", "Filler", "Seventh"), + Planet(8, "Neptune", "Filler", "Eighth") +] -Planets = [Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune] #...to get all existing planets, # so that I can see a list of planets, # with their id, name, description, From 9b598546da4d4d231d4420b987b41ec2ce11b2ba Mon Sep 17 00:00:00 2001 From: louloera Date: Fri, 21 Apr 2023 11:31:29 -0700 Subject: [PATCH 04/26] Created blueprint for planets --- app/__init__.py | 5 +++++ app/routes.py | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..2be9274be 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,4 +4,9 @@ def create_app(test_config=None): app = Flask(__name__) + from.routes import planets + app.register_blueprint(planets) + return app + + diff --git a/app/routes.py b/app/routes.py index 24d1ac65b..f93ab1d9b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -from flask import Blueprint +from flask import Blueprint, jsonify, abort, make_response class Planet(): @@ -19,6 +19,15 @@ def __init__(self, id, name, description, place): Planet(8, "Neptune", "Filler", "Eighth") ] + +planets = Blueprint ("planets", __name__, url_prefix="/planets") + +@planets.route("", methods=["GET"]) + +def return_planets(): + return "planets" + + #...to get all existing planets, # so that I can see a list of planets, # with their id, name, description, From 91368e339cfc0c98b90f373049c2b1ba5d2d5fe1 Mon Sep 17 00:00:00 2001 From: Charlie Date: Fri, 21 Apr 2023 11:59:43 -0700 Subject: [PATCH 05/26] endpoint to return list of planets --- app/routes.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index f93ab1d9b..0e0ffc3c3 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,5 @@ from flask import Blueprint, jsonify, abort, make_response +import json class Planet(): @@ -25,7 +26,21 @@ def __init__(self, id, name, description, place): @planets.route("", methods=["GET"]) def return_planets(): - return "planets" + planet_list = [] + + for planet in Planets: + planet_dict = { + "id": planet.id, + "name": planet.name, + "description": planet.description, + "place": planet.place + } + planet_list.append(planet_dict) + + + planets_json = json.dumps(planet_list) + return planets_json + #...to get all existing planets, From 172f5884449d1d65180ab903779af55b78fbe167 Mon Sep 17 00:00:00 2001 From: louloera Date: Fri, 21 Apr 2023 12:04:30 -0700 Subject: [PATCH 06/26] Made endpoint for specific planet --- app/routes.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index f93ab1d9b..d4bc51801 100644 --- a/app/routes.py +++ b/app/routes.py @@ -28,7 +28,28 @@ def return_planets(): return "planets" -#...to get all existing planets, -# so that I can see a list of planets, -# with their id, name, description, -# and other data of the planet. \ No newline at end of file + + + +@planets.route("/", methods=["GET"]) + +def returns_one_planet_info(planet_id): + return vars([Planets[1]]),200 + +# for planet in planets: + +# if planet_id == planet.id: +# return "jupiter" +# #return vars(planet), 200 +# # return jsonify({ +# # "name" : planet.name +# # "id" : planet.id +# # "description" : planet.description +# # "place" : planet.place +# # }) + + +# #...to get all existing planets, +# # so that I can see a list of planets, +# # with their id, name, description, +# # and other data of the planet. \ No newline at end of file From 40908b522eb2eabe71a4080ff52124d5e3c2ee0b Mon Sep 17 00:00:00 2001 From: louloera Date: Fri, 21 Apr 2023 12:15:26 -0700 Subject: [PATCH 07/26] Logic to return a specific planet info in / endpoint --- app/routes.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/app/routes.py b/app/routes.py index 89ac05841..449267710 100644 --- a/app/routes.py +++ b/app/routes.py @@ -49,12 +49,9 @@ def return_planets(): @planets.route("/", methods=["GET"]) def returns_one_planet_info(planet_id): - return vars([Planets[1]]),200 - -# for planet in planets: - -# if planet_id == planet.id: -# return "jupiter" + for planet in Planets: + if int(planet_id) == planet.id: + return vars(planet), 200 # #return vars(planet), 200 # # return jsonify({ # # "name" : planet.name @@ -64,7 +61,3 @@ def returns_one_planet_info(planet_id): # # }) -# #...to get all existing planets, -# # so that I can see a list of planets, -# # with their id, name, description, -# # and other data of the planet. \ No newline at end of file From 2d40007431e5458f64443fe8e774c613a6b4b1d8 Mon Sep 17 00:00:00 2001 From: Charlie Date: Sun, 23 Apr 2023 11:05:58 -0700 Subject: [PATCH 08/26] added handling for invalid format and nonexisting planets --- app/routes.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index 449267710..d689cc247 100644 --- a/app/routes.py +++ b/app/routes.py @@ -25,9 +25,16 @@ def __init__(self, id, name, description, place): @planets.route("", methods=["GET"]) +#to get all existing planets +#see a list of planets and their attributes +# def request_planets(): +# all_planets = Planets[id(name,description,place)] +# return jsonify(all_planets) + +# returns a dictionary def return_planets(): planet_list = [] - + for planet in Planets: planet_dict = { "id": planet.id, @@ -43,15 +50,18 @@ def return_planets(): - - - @planets.route("/", methods=["GET"]) def returns_one_planet_info(planet_id): + try: + planet_id = int(planet_id) + except: + return {"message":f"planet {planet_id} invalid"}, 400 for planet in Planets: if int(planet_id) == planet.id: return vars(planet), 200 + return {"message":f"planet {planet_id} not found"}, 404 + # #return vars(planet), 200 # # return jsonify({ # # "name" : planet.name From 4d5b1cb2b28c27d795360e5a44e142d25d73e8c7 Mon Sep 17 00:00:00 2001 From: louloera Date: Thu, 27 Apr 2023 13:20:27 -0700 Subject: [PATCH 09/26] Created bones of databas --- app/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 2be9274be..3944d50c3 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,9 +1,21 @@ from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate + +db = SQLAlchemy() +migrate = Migrate() def create_app(test_config=None): app = Flask(__name__) + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' + + db.init_app(app) + migrate.init_app(app, db) + + from.routes import planets app.register_blueprint(planets) From 025c5006550e836477bdb3a318df03f5d317e3da Mon Sep 17 00:00:00 2001 From: Charlie Date: Thu, 27 Apr 2023 13:27:58 -0700 Subject: [PATCH 10/26] added model and created Planet object made visible in init --- app/__init__.py | 2 ++ app/models/Planet.py | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 app/models/Planet.py diff --git a/app/__init__.py b/app/__init__.py index 3944d50c3..e6cf1f137 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -19,6 +19,8 @@ def create_app(test_config=None): from.routes import planets app.register_blueprint(planets) + from app.models.Planet import Planet + return app diff --git a/app/models/Planet.py b/app/models/Planet.py new file mode 100644 index 000000000..ab51f7f10 --- /dev/null +++ b/app/models/Planet.py @@ -0,0 +1,9 @@ +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) + place = db.Column(db.Integer) + + \ No newline at end of file From 9c736e0eaba3e87f1a55d72881333d8df1454e20 Mon Sep 17 00:00:00 2001 From: Charlie Date: Thu, 27 Apr 2023 13:50:38 -0700 Subject: [PATCH 11/26] creatd route to post new planet in db --- app/routes.py | 90 +++++++++-------- migrations/README | 1 + migrations/alembic.ini | 45 +++++++++ migrations/env.py | 96 +++++++++++++++++++ migrations/script.py.mako | 24 +++++ .../869f3c3ab97b_adds_planet_model.py | 34 +++++++ 6 files changed, 250 insertions(+), 40 deletions(-) create mode 100644 migrations/README create mode 100644 migrations/alembic.ini create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako create mode 100644 migrations/versions/869f3c3ab97b_adds_planet_model.py diff --git a/app/routes.py b/app/routes.py index d689cc247..50bf2e1c9 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,29 +1,39 @@ -from flask import Blueprint, jsonify, abort, make_response -import json - -class Planet(): - - def __init__(self, id, name, description, place): - self.id = id - self.name = name - self.description = description - self.place = place - -Planets = [ - Planet(1, "Mercury", "filler", "First"), - Planet(2, "Venus", "Filler", "Second"), - Planet(3, "Earth", "Filler", "Third"), - Planet(4, "Mars", "Filler", "Forth"), - Planet(5, "Jupiter", "Filler", "Fifth"), - Planet(6, "Saturn", "Filler", "Sixth"), - Planet(7, "Uranus", "Filler", "Seventh"), - Planet(8, "Neptune", "Filler", "Eighth") -] - - -planets = Blueprint ("planets", __name__, url_prefix="/planets") - -@planets.route("", methods=["GET"]) +from app import db +from app.models.Planet import Planet +from flask import Blueprint, jsonify, make_response, request + +# class Planet(): + +# def __init__(self, id, name, description, place): +# self.id = id +# self.name = name +# self.description = description +# self.place = place + +# Planets = [ +# Planet(1, "Mercury", "filler", "First"), +# Planet(2, "Venus", "Filler", "Second"), +# Planet(3, "Earth", "Filler", "Third"), +# Planet(4, "Mars", "Filler", "Forth"), +# Planet(5, "Jupiter", "Filler", "Fifth"), +# Planet(6, "Saturn", "Filler", "Sixth"), +# Planet(7, "Uranus", "Filler", "Seventh"), +# Planet(8, "Neptune", "Filler", "Eighth") +# ] + + +planets = Blueprint("planets", __name__, url_prefix="/planets") +@planets.route("", methods=["POST"]) + +def handle_planet(): + request_body = request.get_json() + new_planet = Planet(name=request_body["name"], + description=request_body["description"], + place=request_body["place"]) + db.session.add(new_planet) + db.session.commit() + + return make_response(f"Planet {new_planet.name} succesfully created",201) #to get all existing planets #see a list of planets and their attributes @@ -32,21 +42,21 @@ def __init__(self, id, name, description, place): # return jsonify(all_planets) # returns a dictionary -def return_planets(): - planet_list = [] +# def return_planets(): +# planet_list = [] - for planet in Planets: - planet_dict = { - "id": planet.id, - "name": planet.name, - "description": planet.description, - "place": planet.place - } - planet_list.append(planet_dict) - - - planets_json = json.dumps(planet_list) - return planets_json +# for planet in Planets: +# planet_dict = { +# "id": planet.id, +# "name": planet.name, +# "description": planet.description, +# "place": planet.place +# } +# planet_list.append(planet_dict) + + +# planets_json = json.dumps(planet_list) +# return planets_json diff --git a/migrations/README b/migrations/README new file mode 100644 index 000000000..98e4f9c44 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 000000000..f8ed4801f --- /dev/null +++ b/migrations/alembic.ini @@ -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 diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 000000000..8b3fb3353 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,96 @@ +from __future__ import with_statement + +import logging +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool +from flask import current_app + +from alembic import context + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) +logger = logging.getLogger('alembic.env') + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +config.set_main_option( + 'sqlalchemy.url', + str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%')) +target_metadata = current_app.extensions['migrate'].db.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, target_metadata=target_metadata, literal_binds=True + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, 'autogenerate', False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info('No changes in schema detected.') + + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + process_revision_directives=process_revision_directives, + **current_app.extensions['migrate'].configure_args + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 000000000..2c0156303 --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/869f3c3ab97b_adds_planet_model.py b/migrations/versions/869f3c3ab97b_adds_planet_model.py new file mode 100644 index 000000000..7272f0f7b --- /dev/null +++ b/migrations/versions/869f3c3ab97b_adds_planet_model.py @@ -0,0 +1,34 @@ +"""adds planet model + +Revision ID: 869f3c3ab97b +Revises: +Create Date: 2023-04-27 13:29:14.798363 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '869f3c3ab97b' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('planet', + sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.Column('description', sa.String(), nullable=True), + sa.Column('place', sa.Integer(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('planet') + # ### end Alembic commands ### From 1940e93cee99a89706f6edd464cf99da38ab22d8 Mon Sep 17 00:00:00 2001 From: louloera Date: Thu, 27 Apr 2023 14:09:06 -0700 Subject: [PATCH 12/26] Created route to return all of the planets --- app/routes.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/routes.py b/app/routes.py index 50bf2e1c9..a5f751cd7 100644 --- a/app/routes.py +++ b/app/routes.py @@ -35,6 +35,25 @@ def handle_planet(): return make_response(f"Planet {new_planet.name} succesfully created",201) +@planets.route("", methods=["GET"]) + +def return_all_planets(): + + planets= Planet.query.all() + planet_response = [] + + for planet in planets: + planet_response.append({ + "id": planet.id, + "name": planet.name, + "description":planet.description, + 'place':planet.place + }) + return jsonify(planet_response) + + + + #to get all existing planets #see a list of planets and their attributes # def request_planets(): @@ -71,6 +90,9 @@ def returns_one_planet_info(planet_id): if int(planet_id) == planet.id: return vars(planet), 200 return {"message":f"planet {planet_id} not found"}, 404 + + + # #return vars(planet), 200 # # return jsonify({ From 89ee99e63326bb321213bc7852787a6709f6b5c6 Mon Sep 17 00:00:00 2001 From: Charlie Date: Mon, 1 May 2023 13:08:22 -0700 Subject: [PATCH 13/26] removed comments --- app/routes.py | 62 ++------------------------------------------------- 1 file changed, 2 insertions(+), 60 deletions(-) diff --git a/app/routes.py b/app/routes.py index a5f751cd7..e9aeeea25 100644 --- a/app/routes.py +++ b/app/routes.py @@ -2,29 +2,10 @@ from app.models.Planet import Planet from flask import Blueprint, jsonify, make_response, request -# class Planet(): - -# def __init__(self, id, name, description, place): -# self.id = id -# self.name = name -# self.description = description -# self.place = place - -# Planets = [ -# Planet(1, "Mercury", "filler", "First"), -# Planet(2, "Venus", "Filler", "Second"), -# Planet(3, "Earth", "Filler", "Third"), -# Planet(4, "Mars", "Filler", "Forth"), -# Planet(5, "Jupiter", "Filler", "Fifth"), -# Planet(6, "Saturn", "Filler", "Sixth"), -# Planet(7, "Uranus", "Filler", "Seventh"), -# Planet(8, "Neptune", "Filler", "Eighth") -# ] - planets = Blueprint("planets", __name__, url_prefix="/planets") -@planets.route("", methods=["POST"]) +@planets.route("", methods=["POST"]) def handle_planet(): request_body = request.get_json() new_planet = Planet(name=request_body["name"], @@ -35,8 +16,8 @@ def handle_planet(): return make_response(f"Planet {new_planet.name} succesfully created",201) -@planets.route("", methods=["GET"]) +@planets.route("", methods=["GET"]) def return_all_planets(): planets= Planet.query.all() @@ -52,35 +33,7 @@ def return_all_planets(): return jsonify(planet_response) - - -#to get all existing planets -#see a list of planets and their attributes -# def request_planets(): -# all_planets = Planets[id(name,description,place)] -# return jsonify(all_planets) - -# returns a dictionary -# def return_planets(): -# planet_list = [] - -# for planet in Planets: -# planet_dict = { -# "id": planet.id, -# "name": planet.name, -# "description": planet.description, -# "place": planet.place -# } -# planet_list.append(planet_dict) - - -# planets_json = json.dumps(planet_list) -# return planets_json - - - @planets.route("/", methods=["GET"]) - def returns_one_planet_info(planet_id): try: planet_id = int(planet_id) @@ -92,14 +45,3 @@ def returns_one_planet_info(planet_id): return {"message":f"planet {planet_id} not found"}, 404 - - -# #return vars(planet), 200 -# # return jsonify({ -# # "name" : planet.name -# # "id" : planet.id -# # "description" : planet.description -# # "place" : planet.place -# # }) - - From 2783e27c186d8728a4ab11c98cd30557cd99f0ea Mon Sep 17 00:00:00 2001 From: louloera Date: Mon, 1 May 2023 13:26:01 -0700 Subject: [PATCH 14/26] refactored get one planet route and created a function to validate id --- app/routes.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/app/routes.py b/app/routes.py index e9aeeea25..037b618ce 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,6 @@ from app import db from app.models.Planet import Planet -from flask import Blueprint, jsonify, make_response, request +from flask import Blueprint, jsonify, make_response, request, abort planets = Blueprint("planets", __name__, url_prefix="/planets") @@ -32,16 +32,30 @@ def return_all_planets(): }) return jsonify(planet_response) - -@planets.route("/", methods=["GET"]) -def returns_one_planet_info(planet_id): +def handle_planet_id(planet_id): try: planet_id = int(planet_id) except: - return {"message":f"planet {planet_id} invalid"}, 400 - for planet in Planets: - if int(planet_id) == planet.id: - return vars(planet), 200 - return {"message":f"planet {planet_id} not found"}, 404 + 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 + + +@planets.route("/", methods=["GET"]) + +def returns_one_planet_info(planet_id): + planet= handle_planet_id(planet_id) + planet_response= ({ + "id": planet.id, + "name": planet.name, + "description":planet.description, + 'place':planet.place + }) + return (planet_response) + + From a9a999f1751a6289b006a53c50e913cf91755c31 Mon Sep 17 00:00:00 2001 From: Charlie Date: Mon, 1 May 2023 13:34:24 -0700 Subject: [PATCH 15/26] created update planet endpoint --- app/routes.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 037b618ce..f008d10ea 100644 --- a/app/routes.py +++ b/app/routes.py @@ -45,7 +45,6 @@ def handle_planet_id(planet_id): @planets.route("/", methods=["GET"]) - def returns_one_planet_info(planet_id): planet= handle_planet_id(planet_id) planet_response= ({ @@ -57,5 +56,15 @@ def returns_one_planet_info(planet_id): return (planet_response) +@planets.route("/", methods=["PUT"]) +def update_planet(planet_id): + planet = handle_planet_id(planet_id) + request_body = request.get_json() + + planet.name = request_body["name"] + planet.description = request_body["description"] + planet.place = request_body["place"] + db.session.commit() + return make_response(f"Planet {planet.id} successfully updated") \ No newline at end of file From 6d467319a1550d5a7c3b69ebb278d1fe094e407a Mon Sep 17 00:00:00 2001 From: louloera Date: Mon, 1 May 2023 13:41:18 -0700 Subject: [PATCH 16/26] Created route to delete a planet --- app/routes.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index f008d10ea..592fe49df 100644 --- a/app/routes.py +++ b/app/routes.py @@ -67,4 +67,14 @@ def update_planet(planet_id): db.session.commit() - return make_response(f"Planet {planet.id} successfully updated") \ No newline at end of file + return make_response(f"Planet {planet.id} successfully updated") + +@planets.route("/", methods=["DELETE"]) + +def delete_planet(planet_id): + planet = handle_planet_id(planet_id) + + db.session.delete(planet) + db.session.commit() + + return make_response(f"Planet #{planet.id} succesfully deleted") \ No newline at end of file From d77592a09861e2269859f627250f06fcca5b25ed Mon Sep 17 00:00:00 2001 From: Charlie Date: Tue, 2 May 2023 11:27:08 -0700 Subject: [PATCH 17/26] added query param --- app/routes.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 592fe49df..acddf2bc3 100644 --- a/app/routes.py +++ b/app/routes.py @@ -20,9 +20,13 @@ def handle_planet(): @planets.route("", methods=["GET"]) def return_all_planets(): - planets= Planet.query.all() - planet_response = [] + name_query = request.args.get("name") + if name_query: + planets = Planet.query.filter_by(name=name_query) + else: + planets = Planet.query.all() + planet_response = [] for planet in planets: planet_response.append({ "id": planet.id, From d72945d408c09f1eb11f731ed3f308c2e9712038 Mon Sep 17 00:00:00 2001 From: Charlie Date: Wed, 3 May 2023 13:23:31 -0700 Subject: [PATCH 18/26] created env file and reconfiged createapp --- app/__init__.py | 13 +++++++++++-- requirements.txt | 6 ++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index e6cf1f137..48d962194 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,16 +1,25 @@ 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__) - app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' + 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) diff --git a/requirements.txt b/requirements.txt index ae59e7b55..966d770b4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,14 +4,19 @@ blinker==1.4 certifi==2020.12.5 chardet==4.0.0 click==7.1.2 +coverage==7.2.3 +exceptiongroup==1.1.1 Flask==1.1.2 Flask-Migrate==2.6.0 Flask-SQLAlchemy==2.4.4 idna==2.10 +iniconfig==2.0.0 itsdangerous==1.1.0 Jinja2==2.11.3 Mako==1.1.4 MarkupSafe==1.1.1 +packaging==23.1 +pluggy==1.0.0 psycopg2-binary==2.9.5 pycodestyle==2.6.0 pytest==7.3.1 @@ -23,5 +28,6 @@ requests==2.25.1 six==1.15.0 SQLAlchemy==1.3.23 toml==0.10.2 +tomli==2.0.1 urllib3==1.26.4 Werkzeug==1.0.1 From b42869e242e9b4a75c3050d2c95ab7213b28fdd5 Mon Sep 17 00:00:00 2001 From: louloera Date: Wed, 3 May 2023 13:34:29 -0700 Subject: [PATCH 19/26] Created tests folder and the test_fixture file --- tests/__init__.py | 0 tests/conftest.py | 25 +++++++++++++++++++++++++ tests/test_fixture.py | 10 ++++++++++ tests/test_routes.py | 0 4 files changed, 35 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_fixture.py create mode 100644 tests/test_routes.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..96d35f4c1 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,25 @@ +import pytest +from app import create_app +from app import db +from flask.signals import request_finished + + +@pytest.fixture +def app(): + app = create_app({"TESTING": True}) + + @request_finished.connect_via(app) + def expire_session(sender, response, **extra): + db.session.remove() + + with app.app_context(): + db.create_all() + yield app + + with app.app_context(): + db.drop_all() + + +@pytest.fixture +def client(app): + return app.test_client() diff --git a/tests/test_fixture.py b/tests/test_fixture.py new file mode 100644 index 000000000..7e8d03486 --- /dev/null +++ b/tests/test_fixture.py @@ -0,0 +1,10 @@ +import pytest + +@pytest.fixture + +def empty_list(): + return [] + +def test_len_of_empty_list(empty_list): + assert isinstance(empty_list, list) + assert len(empty_list) == 0 \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 000000000..e69de29bb From 76e6e5e8579e8e36719211adb5c5e2654a941dfd Mon Sep 17 00:00:00 2001 From: Charlie Date: Wed, 3 May 2023 13:38:56 -0700 Subject: [PATCH 20/26] added test for GET with 200 response --- tests/test_fixture.py | 10 ---------- tests/test_routes.py | 8 ++++++++ 2 files changed, 8 insertions(+), 10 deletions(-) delete mode 100644 tests/test_fixture.py diff --git a/tests/test_fixture.py b/tests/test_fixture.py deleted file mode 100644 index 7e8d03486..000000000 --- a/tests/test_fixture.py +++ /dev/null @@ -1,10 +0,0 @@ -import pytest - -@pytest.fixture - -def empty_list(): - return [] - -def test_len_of_empty_list(empty_list): - assert isinstance(empty_list, list) - assert len(empty_list) == 0 \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py index e69de29bb..1124a9765 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -0,0 +1,8 @@ +def test_get_all_planets_with_no_records(client): + # Act + response = client.get("/planets") + response_body = response.get_json() + + # Assert + assert response.status_code == 200 + assert response_body == [] \ No newline at end of file From 0f72de70d4eadb050c1f5f90ea3f4e599271d246 Mon Sep 17 00:00:00 2001 From: louloera Date: Wed, 3 May 2023 14:01:13 -0700 Subject: [PATCH 21/26] Created test case to return one planet and populated test database with two planets --- tests/conftest.py | 10 ++++++++++ tests/test_routes.py | 12 +++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 96d35f4c1..a7bab9588 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,7 @@ from app import create_app from app import db from flask.signals import request_finished +from app.models.Planet import Planet @pytest.fixture @@ -23,3 +24,12 @@ def expire_session(sender, response, **extra): @pytest.fixture def client(app): return app.test_client() + +@pytest.fixture +def two_planets(app): + #Arrange + venus= Planet (name="Venus",description ="Gassy love goddess", place= 2) + earth = Planet(name="Earth",description ="le home", place= 3) + + db.session.add_all([venus,earth]) + db.session.commit() \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py index 1124a9765..89d89168f 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -5,4 +5,14 @@ def test_get_all_planets_with_no_records(client): # Assert assert response.status_code == 200 - assert response_body == [] \ No newline at end of file + assert response_body == [] + +def test_get_all_planets_with_two_records(client, two_planets): + # Act + response = client.get("/planets/1") + response_body = response.get_json() + + # Assert + assert response.status_code == 200 + assert response_body == {"id": 1,"name":"Venus","description":"Gassy love goddess","place":2} +#{"id" : 2, "name":"Earth","description" : "le home", "place": 3}] \ No newline at end of file From 8e072572ca044e333a42d60f2a4c0d3320c3d173 Mon Sep 17 00:00:00 2001 From: Charlie Date: Fri, 5 May 2023 14:26:52 -0700 Subject: [PATCH 22/26] added test for empty db proper status return --- tests/conftest.py | 4 +++- tests/test_routes.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a7bab9588..96dda0997 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -32,4 +32,6 @@ def two_planets(app): earth = Planet(name="Earth",description ="le home", place= 3) db.session.add_all([venus,earth]) - db.session.commit() \ No newline at end of file + db.session.commit() + + diff --git a/tests/test_routes.py b/tests/test_routes.py index 89d89168f..1b7c8fdb7 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -15,4 +15,14 @@ def test_get_all_planets_with_two_records(client, two_planets): # Assert assert response.status_code == 200 assert response_body == {"id": 1,"name":"Venus","description":"Gassy love goddess","place":2} -#{"id" : 2, "name":"Earth","description" : "le home", "place": 3}] \ No newline at end of file +#{"id" : 2, "name":"Earth","description" : "le home", "place": 3}] + +# #GET /planets/1 with no data in test database (no fixture) returns a 404 +def test_empty_database_returns_404(client): + + #act + response = client.get("planets/1") + response_body = response.get_json() + #assert + assert response.status_code == 404 + assert response_body == {"message":"planet 1 not found"} \ No newline at end of file From 8ea92744fde90edffc29b46413a0fa20b1fe9297 Mon Sep 17 00:00:00 2001 From: louloera Date: Fri, 5 May 2023 14:30:41 -0700 Subject: [PATCH 23/26] Created test case to validate the get rout for all planets --- tests/test_routes.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index 1b7c8fdb7..fb0c2b829 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -7,7 +7,7 @@ def test_get_all_planets_with_no_records(client): assert response.status_code == 200 assert response_body == [] -def test_get_all_planets_with_two_records(client, two_planets): +def test_get_one_planet_with_two_records(client, two_planets): # Act response = client.get("/planets/1") response_body = response.get_json() @@ -25,4 +25,13 @@ def test_empty_database_returns_404(client): response_body = response.get_json() #assert assert response.status_code == 404 - assert response_body == {"message":"planet 1 not found"} \ No newline at end of file + assert response_body == {"message":"planet 1 not found"} + +def test_get_planets_with_two_records(client, two_planets): + # Act + response = client.get("/planets") + response_body = response.get_json() + + # Assert + assert response.status_code == 200 + assert response_body == [{"id": 1,"name":"Venus","description":"Gassy love goddess","place":2},{"id" : 2, "name":"Earth","description" : "le home", "place": 3}] From b49eaf04733ab475dec41f2b521fbd21c5058658 Mon Sep 17 00:00:00 2001 From: Charlie Date: Fri, 5 May 2023 14:46:40 -0700 Subject: [PATCH 24/26] added test for post to return 201 --- app/routes.py | 2 +- tests/test_routes.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index acddf2bc3..d4884ef5c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -14,7 +14,7 @@ def handle_planet(): db.session.add(new_planet) db.session.commit() - return make_response(f"Planet {new_planet.name} succesfully created",201) + return make_response(jsonify(f"Planet {new_planet.name} succesfully created"),201) @planets.route("", methods=["GET"]) diff --git a/tests/test_routes.py b/tests/test_routes.py index fb0c2b829..c5ca74d58 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -35,3 +35,18 @@ def test_get_planets_with_two_records(client, two_planets): # Assert assert response.status_code == 200 assert response_body == [{"id": 1,"name":"Venus","description":"Gassy love goddess","place":2},{"id" : 2, "name":"Earth","description" : "le home", "place": 3}] + +#POST /planets with a JSON request body returns a 201 + +def test_JSON_request_returns_201(client, two_planets): + + response = client.post("/planets", json={ + "name": "Venus", + "description":"Gassy love goddess", + "place":2}) + + response_body = response.get_json() + + assert response.status_code == 201 + assert response_body == "Planet Venus succesfully created" + From cab067124d5c97a2156f7830732f94b8bd2f46d0 Mon Sep 17 00:00:00 2001 From: louloera Date: Tue, 9 May 2023 11:12:43 -0700 Subject: [PATCH 25/26] Changed database connector string to deployment env --- app/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 48d962194..39d52d5a3 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -14,7 +14,9 @@ def create_app(test_config=None): if not test_config: app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("SQLALCHEMY_DATABASE_URI") + #app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("SQLALCHEMY_DATABASE_URI") + app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("RENDER_DATABASE_URI") + else: app.config["TESTING"] = True app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False From 6e27096ae56cceebc0af7c3316319325408b99fb Mon Sep 17 00:00:00 2001 From: louloera Date: Tue, 9 May 2023 11:29:11 -0700 Subject: [PATCH 26/26] updated requirements.txt to contain gunicorn --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 966d770b4..f4ff77124 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,6 +9,7 @@ exceptiongroup==1.1.1 Flask==1.1.2 Flask-Migrate==2.6.0 Flask-SQLAlchemy==2.4.4 +gunicorn==20.1.0 idna==2.10 iniconfig==2.0.0 itsdangerous==1.1.0