From c5a9bfc777fb8f3b9b50c3f98ced24f5db83daf6 Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Mon, 18 Oct 2021 13:42:48 -0700 Subject: [PATCH 01/37] Adds class Planet to routes.py --- app/routes.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..ac63ca7a2 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,14 @@ from flask import Blueprint + +class Planet(): + def __init__(self, id, name, description, xenomorphs=None): + self.id = id + self.name = name + self.description = description + self.xenomorphs = xenomorphs + + +PLANETS = [ + +] From eb71eb602ed8163d5bcb584bacda7514d2ab1f5d Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Mon, 18 Oct 2021 14:14:27 -0700 Subject: [PATCH 02/37] Add get all functions and registers blueprint class --- app/__init__.py | 3 +++ app/routes.py | 23 +++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..ab9eee40e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,4 +4,7 @@ def create_app(test_config=None): app = Flask(__name__) + from .routes import planets_bp + app.register_blueprint(planets_bp) + return app diff --git a/app/routes.py b/app/routes.py index ac63ca7a2..1420dd5cb 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,8 +1,8 @@ -from flask import Blueprint +from flask import Blueprint, jsonify class Planet(): - def __init__(self, id, name, description, xenomorphs=None): + def __init__(self, id, name, description, xenomorphs=False): self.id = id self.name = name self.description = description @@ -11,4 +11,23 @@ def __init__(self, id, name, description, xenomorphs=None): PLANETS = [ + Planet(426, "Nostromo's End", + "Hostile weather. Toxic atmosphere. Evidence of civilization.", True), + Planet(224, "JollyPlanet", "Okay. Decent. Will live for long time.", True) + ] + +planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") + +# @blueprint_name.route("", methods=["GET"]) + + +@planets_bp.route("", methods=["GET"]) +def get_all_planets(): + planets_response = [] + + for planet in PLANETS: + planets_response.append(vars(planet)) + + return jsonify(planets_response) + From 3d7bffd2dff311eb797760b52b2a61f81a76d0e2 Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Mon, 18 Oct 2021 14:27:07 -0700 Subject: [PATCH 03/37] Adds get_planet function to routes.py --- app/routes.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/routes.py b/app/routes.py index 1420dd5cb..9d3a53b0e 100644 --- a/app/routes.py +++ b/app/routes.py @@ -11,17 +11,14 @@ def __init__(self, id, name, description, xenomorphs=False): PLANETS = [ - Planet(426, "Nostromo's End", - "Hostile weather. Toxic atmosphere. Evidence of civilization.", True), + Planet(426, "Nostromo's End", "Hostile weather. Toxic atmosphere. Evidence of civilization.", True), Planet(224, "JollyPlanet", "Okay. Decent. Will live for long time.", True) ] planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") -# @blueprint_name.route("", methods=["GET"]) - - +# @blueprint_name.route("/endpoint/path/here", methods=["GET"]) @planets_bp.route("", methods=["GET"]) def get_all_planets(): planets_response = [] @@ -31,3 +28,12 @@ def get_all_planets(): return jsonify(planets_response) +@planets_bp.route("/planet_id", methods=["GET"]) +def get_planet(planet_id): + planet_id = int(planet_id) + + for planet in PLANETS: + if planet.id == planet_id: + return vars(planet_id) + + From 9cf827ea34cec7f8ff4013b04dcba8fc22f9efa7 Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Mon, 18 Oct 2021 14:37:50 -0700 Subject: [PATCH 04/37] debug adds missing carrots to routes.py --- app/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 9d3a53b0e..9a8a79990 100644 --- a/app/routes.py +++ b/app/routes.py @@ -28,7 +28,7 @@ def get_all_planets(): return jsonify(planets_response) -@planets_bp.route("/planet_id", methods=["GET"]) +@planets_bp.route("/", methods=["GET"]) def get_planet(planet_id): planet_id = int(planet_id) From 39c4dd7c5b1beb952d7ceca71db88addd55f8494 Mon Sep 17 00:00:00 2001 From: jacy Date: Mon, 18 Oct 2021 14:55:24 -0700 Subject: [PATCH 05/37] fixed bug in routes.py; changed planet_id to planet in get_planet function --- app/routes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/routes.py b/app/routes.py index 9a8a79990..cf23eb446 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,3 +1,5 @@ +# localhost:5000/ + from flask import Blueprint, jsonify @@ -34,6 +36,4 @@ def get_planet(planet_id): for planet in PLANETS: if planet.id == planet_id: - return vars(planet_id) - - + return vars(planet) \ No newline at end of file From 6d98074400b8f6258ce9433dda6505d0ad413671 Mon Sep 17 00:00:00 2001 From: jacy Date: Mon, 18 Oct 2021 14:59:10 -0700 Subject: [PATCH 06/37] add comment about local host url --- app/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index cf23eb446..9095d798e 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -# localhost:5000/ +# localhost:5000/ <-- add url endpoint/parameters here from flask import Blueprint, jsonify From b42cc8f9f1a402651d623c9e7cd90aa63d695ce3 Mon Sep 17 00:00:00 2001 From: jacy Date: Fri, 22 Oct 2021 09:29:31 -0700 Subject: [PATCH 07/37] add to_json instance method and removed var(planet) --- app/routes.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 9095d798e..33923f3bb 100644 --- a/app/routes.py +++ b/app/routes.py @@ -10,6 +10,15 @@ def __init__(self, id, name, description, xenomorphs=False): self.description = description self.xenomorphs = xenomorphs + def to_json(self): + json_dict = { + "id": self.id, + "name": self.name, + "description": self.description, + "xenomorphs": self.xenomorphs + } + return json_dict + PLANETS = [ @@ -26,7 +35,7 @@ def get_all_planets(): planets_response = [] for planet in PLANETS: - planets_response.append(vars(planet)) + planets_response.append(planet.to_json()) return jsonify(planets_response) @@ -36,4 +45,4 @@ def get_planet(planet_id): for planet in PLANETS: if planet.id == planet_id: - return vars(planet) \ No newline at end of file + return planet.to_json() \ No newline at end of file From 0b775b2e92997f8b837f2430dd005b200c485af9 Mon Sep 17 00:00:00 2001 From: jacy Date: Fri, 22 Oct 2021 09:31:35 -0700 Subject: [PATCH 08/37] delete spacing --- app/routes.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index 33923f3bb..b3dfbc5ee 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,6 @@ # localhost:5000/ <-- add url endpoint/parameters here -from flask import Blueprint, jsonify +from flask import Blueprint,jsonify class Planet(): @@ -21,11 +21,9 @@ def to_json(self): PLANETS = [ - Planet(426, "Nostromo's End", "Hostile weather. Toxic atmosphere. Evidence of civilization.", True), Planet(224, "JollyPlanet", "Okay. Decent. Will live for long time.", True) - -] + ] planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") From 45c78865ebe4beebae67d1d3cad936d8f379ee2d Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Mon, 25 Oct 2021 13:55:16 -0700 Subject: [PATCH 09/37] replaces planet class in app with planet model in app.models. --- app/__init__.py | 11 +++++++++++ app/routes.py | 23 ++--------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index ab9eee40e..0371e44cf 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,9 +1,20 @@ from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate +db = SQLAlchemy() +migrate = Migrate() +database = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' def create_app(test_config=None): app = Flask(__name__) + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + app.config['SQLALCHEMY_DATABASE_URI'] = database + + db.init_app(app) + migrate.init_app(app, db) + from .routes import planets_bp app.register_blueprint(planets_bp) diff --git a/app/routes.py b/app/routes.py index b3dfbc5ee..ef34daf7c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,27 +3,8 @@ from flask import Blueprint,jsonify -class Planet(): - def __init__(self, id, name, description, xenomorphs=False): - self.id = id - self.name = name - self.description = description - self.xenomorphs = xenomorphs - - def to_json(self): - json_dict = { - "id": self.id, - "name": self.name, - "description": self.description, - "xenomorphs": self.xenomorphs - } - return json_dict - - -PLANETS = [ - Planet(426, "Nostromo's End", "Hostile weather. Toxic atmosphere. Evidence of civilization.", True), - Planet(224, "JollyPlanet", "Okay. Decent. Will live for long time.", True) - ] + + planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") From a29a3afa3545d41a357a7857b6c56b3c6a39ab38 Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Mon, 25 Oct 2021 14:06:14 -0700 Subject: [PATCH 10/37] moves models folder from pycache to app under SOLAR-SYSTEM-API. --- app/models/__init__.py | 1 + app/models/planet.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 app/models/__init__.py create mode 100644 app/models/planet.py diff --git a/app/models/__init__.py b/app/models/__init__.py new file mode 100644 index 000000000..c4dc5a1f3 --- /dev/null +++ b/app/models/__init__.py @@ -0,0 +1 @@ +#"we don't need to do anything" - jacy diff --git a/app/models/planet.py b/app/models/planet.py new file mode 100644 index 000000000..bdc44c6d1 --- /dev/null +++ b/app/models/planet.py @@ -0,0 +1,18 @@ +from app import db +#"This is where we CREATE OUR PLANET! (class)" + +class Planet(): + def __init__(self, id, name, description, xenomorphs=False): + self.id = id + self.name = name + self.description = description + self.xenomorphs = xenomorphs + + def to_json(self): + json_dict = { + "id": self.id, + "name": self.name, + "description": self.description, + "xenomorphs": self.xenomorphs + } + return json_dict \ No newline at end of file From 9a0acd4477d4a32f90f33999c403846a3161af0d Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Mon, 25 Oct 2021 14:14:03 -0700 Subject: [PATCH 11/37] updates planet.py class in models to do stuff better... like.. with the server, or whatever. --- app/models/planet.py | 11 +++++------ app/routes.py | 6 ++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/app/models/planet.py b/app/models/planet.py index bdc44c6d1..be14d9667 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -1,12 +1,11 @@ from app import db #"This is where we CREATE OUR PLANET! (class)" -class Planet(): - def __init__(self, id, name, description, xenomorphs=False): - self.id = id - self.name = name - self.description = description - self.xenomorphs = xenomorphs +class Planet(db.Model): + id = db.Column(db.Integer, primary_key=True, autoincrement=True) + name = db.Column(db.String) + description = db.Column(db.String) + xenomorphs = db.Column(db.Boolean) def to_json(self): json_dict = { diff --git a/app/routes.py b/app/routes.py index ef34daf7c..40652e9b5 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,9 +1,7 @@ # localhost:5000/ <-- add url endpoint/parameters here -from flask import Blueprint,jsonify - - - +from flask import Blueprint,jsonify, make_response, request +from app.models.planet import Planet planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") From 3c2ff79ba21b4a0ab7822bd0d366c657cf8f525e Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Mon, 25 Oct 2021 14:48:37 -0700 Subject: [PATCH 12/37] we migrated models and updated routes. --- app/models/planet.py | 2 +- app/routes.py | 40 ++++++-- migrations/README | 1 + migrations/alembic.ini | 45 +++++++++ migrations/env.py | 96 +++++++++++++++++++ migrations/script.py.mako | 24 +++++ .../c888984465f3_adds_planet_model.py | 34 +++++++ 7 files changed, 232 insertions(+), 10 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/c888984465f3_adds_planet_model.py diff --git a/app/models/planet.py b/app/models/planet.py index be14d9667..b59297439 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -5,7 +5,7 @@ class Planet(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) description = db.Column(db.String) - xenomorphs = db.Column(db.Boolean) + xenomorphs = db.Column(db.Boolean, default=False) def to_json(self): json_dict = { diff --git a/app/routes.py b/app/routes.py index 40652e9b5..06e6ab2bf 100644 --- a/app/routes.py +++ b/app/routes.py @@ -2,24 +2,46 @@ from flask import Blueprint,jsonify, make_response, request from app.models.planet import Planet +from app import db planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") # @blueprint_name.route("/endpoint/path/here", methods=["GET"]) -@planets_bp.route("", methods=["GET"]) -def get_all_planets(): - planets_response = [] +@planets_bp.route("", methods=["GET", "POST"]) - for planet in PLANETS: - planets_response.append(planet.to_json()) +def handle_planets(): - return jsonify(planets_response) + if request.method == "POST": + request_body = request.get_json() + if "id" not in request_body or "name" not in request_body: + return make_response("Invalid Request", 400) + new_planet = Planet( + name=request_body['name'], + description=request_body['description'], + #xenomorphs=request_body[''] + ) + db.session.add(new_planet) #like git, stagging changes + db.session.commit() #committing to database + + return make_response(f"Your planet, {new_planet.name}, has been created.", 201) + + elif request.method == "GET": + + planets = Planet.query.all() + planets_response = [] + + for planet in planets: + planets_response.append(planet.to_json()) + + return jsonify(planets_response) @planets_bp.route("/", methods=["GET"]) def get_planet(planet_id): planet_id = int(planet_id) + planet = Planet.query.all(planet_id) - for planet in PLANETS: - if planet.id == planet_id: - return planet.to_json() \ No newline at end of file + if planet == None: + return make_response("your planet ain't real.", 404) + + return planet.to_json() \ No newline at end of file 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/c888984465f3_adds_planet_model.py b/migrations/versions/c888984465f3_adds_planet_model.py new file mode 100644 index 000000000..d3aeb88fe --- /dev/null +++ b/migrations/versions/c888984465f3_adds_planet_model.py @@ -0,0 +1,34 @@ +"""adds planet model + +Revision ID: c888984465f3 +Revises: +Create Date: 2021-10-25 14:35:56.579309 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'c888984465f3' +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('xenomorphs', sa.Boolean(), 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 54be1bb6382377ad7b6a38eb10af9899d56730ee Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Mon, 25 Oct 2021 14:55:57 -0700 Subject: [PATCH 13/37] updates routes to fix bug --- app/routes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 06e6ab2bf..5a9f70660 100644 --- a/app/routes.py +++ b/app/routes.py @@ -14,7 +14,7 @@ def handle_planets(): if request.method == "POST": request_body = request.get_json() - if "id" not in request_body or "name" not in request_body: + if "name" not in request_body: return make_response("Invalid Request", 400) new_planet = Planet( name=request_body['name'], @@ -43,5 +43,5 @@ def get_planet(planet_id): if planet == None: return make_response("your planet ain't real.", 404) - + return planet.to_json() \ No newline at end of file From 7884f0630811a36857ffdc9b114ed4496a067978 Mon Sep 17 00:00:00 2001 From: jacy Date: Mon, 25 Oct 2021 15:16:48 -0700 Subject: [PATCH 14/37] changed query.all to query.get for get_planet function --- app/routes.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 5a9f70660..fd6c08ade 100644 --- a/app/routes.py +++ b/app/routes.py @@ -16,11 +16,13 @@ def handle_planets(): request_body = request.get_json() if "name" not in request_body: return make_response("Invalid Request", 400) + new_planet = Planet( name=request_body['name'], description=request_body['description'], #xenomorphs=request_body[''] ) + db.session.add(new_planet) #like git, stagging changes db.session.commit() #committing to database @@ -38,8 +40,7 @@ def handle_planets(): @planets_bp.route("/", methods=["GET"]) def get_planet(planet_id): - planet_id = int(planet_id) - planet = Planet.query.all(planet_id) + planet = Planet.query.get(planet_id) if planet == None: return make_response("your planet ain't real.", 404) From 7d7a32f1d15c6ebadbec9900d53ce796b2515541 Mon Sep 17 00:00:00 2001 From: jacy Date: Tue, 26 Oct 2021 10:51:18 -0700 Subject: [PATCH 15/37] made request_body local var --- app/routes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index fd6c08ade..f65514a69 100644 --- a/app/routes.py +++ b/app/routes.py @@ -11,9 +11,8 @@ @planets_bp.route("", methods=["GET", "POST"]) def handle_planets(): - + request_body = request.get_json() if request.method == "POST": - request_body = request.get_json() if "name" not in request_body: return make_response("Invalid Request", 400) From 2628ed774122142682476a0c387d0e040867f4bf Mon Sep 17 00:00:00 2001 From: jacy Date: Tue, 26 Oct 2021 10:54:27 -0700 Subject: [PATCH 16/37] minor space changes --- app/routes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index f65514a69..140ea7bcc 100644 --- a/app/routes.py +++ b/app/routes.py @@ -12,13 +12,14 @@ def handle_planets(): request_body = request.get_json() + if request.method == "POST": if "name" not in request_body: return make_response("Invalid Request", 400) new_planet = Planet( name=request_body['name'], - description=request_body['description'], + #description=request_body['description'], #xenomorphs=request_body[''] ) @@ -28,7 +29,6 @@ def handle_planets(): return make_response(f"Your planet, {new_planet.name}, has been created.", 201) elif request.method == "GET": - planets = Planet.query.all() planets_response = [] From c737a22ffb2a8ba3b4f5154c61c9615aeb676bac Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Tue, 26 Oct 2021 11:27:26 -0700 Subject: [PATCH 17/37] Adds 'update' and 'delete' methods to routes.py --- app/routes.py | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/app/routes.py b/app/routes.py index 140ea7bcc..9b7108a3a 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,6 @@ # localhost:5000/ <-- add url endpoint/parameters here -from flask import Blueprint,jsonify, make_response, request +from flask import Blueprint, jsonify, make_response, request, abort from app.models.planet import Planet from app import db @@ -8,23 +8,24 @@ planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") # @blueprint_name.route("/endpoint/path/here", methods=["GET"]) -@planets_bp.route("", methods=["GET", "POST"]) + +@planets_bp.route("", methods=["GET", "POST"]) def handle_planets(): request_body = request.get_json() if request.method == "POST": if "name" not in request_body: return make_response("Invalid Request", 400) - + new_planet = Planet( name=request_body['name'], - #description=request_body['description'], - #xenomorphs=request_body[''] + # description=request_body['description'], + # xenomorphs=request_body[''] ) - db.session.add(new_planet) #like git, stagging changes - db.session.commit() #committing to database + db.session.add(new_planet) # like git, stagging changes + db.session.commit() # committing to database return make_response(f"Your planet, {new_planet.name}, has been created.", 201) @@ -37,11 +38,36 @@ def handle_planets(): return jsonify(planets_response) -@planets_bp.route("/", methods=["GET"]) + +@planets_bp.route("/", methods=["GET", "PATCH", "DELETE"]) def get_planet(planet_id): + request_body = request.get_json() planet = Planet.query.get(planet_id) - if planet == None: - return make_response("your planet ain't real.", 404) + try: + planet_id = int(planet_id) + except: + abort(make_response({"error": "planet_id must be an int"}, 400)) + + if request.method == 'GET': + if planet == None: + return make_response("your planet ain't real.", 404) + return planet.to_json() + + elif request.method == 'PATCH': + if "id" in request_body: + planet.id = request_body["id"] + if "name" in request_body: + planet.name = request_body["name"] + if "description" in request_body: + planet.description = request_body["description"] + if "xenomorphs" in request_body: + planet.xenomorphs = request_body["xenomorphs"] - return planet.to_json() \ No newline at end of file + db.session.commit() + return jsonify(planet.to_json(), 201) + + elif request.method == 'DELETE': + db.session.delete(planet) + db.session.commit() + return make_response(f"Planet #{planet_id} successfully destroyed.", 200) \ No newline at end of file From f1e3085f1a22287971bc7f54b775754eba001308 Mon Sep 17 00:00:00 2001 From: jacy Date: Wed, 27 Oct 2021 10:52:53 -0700 Subject: [PATCH 18/37] Started refactoring source code --- app/routes.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 9b7108a3a..a58c9b1b4 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,12 +4,18 @@ from app.models.planet import Planet from app import db - +# Global Vars planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") -# @blueprint_name.route("/endpoint/path/here", methods=["GET"]) +# Helper Functions +def validate_int(user_input, attribute_name): + try: + user_input = int(user_input) + except: + abort(make_response({"error": "{attribute_name} must be an int"}, 404)) +# Routes @planets_bp.route("", methods=["GET", "POST"]) def handle_planets(): request_body = request.get_json() From c673c70a582cb3ff7ebdb49159d0fe8c191c2242 Mon Sep 17 00:00:00 2001 From: jacy Date: Wed, 27 Oct 2021 10:54:41 -0700 Subject: [PATCH 19/37] add 404 error code --- app/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index a58c9b1b4..d3f616c7f 100644 --- a/app/routes.py +++ b/app/routes.py @@ -22,7 +22,7 @@ def handle_planets(): if request.method == "POST": if "name" not in request_body: - return make_response("Invalid Request", 400) + return make_response("Invalid Request", 404) new_planet = Planet( name=request_body['name'], From 778f1eff9247bff181b3244d73e2a2eb868f404a Mon Sep 17 00:00:00 2001 From: jacy Date: Wed, 27 Oct 2021 11:29:41 -0700 Subject: [PATCH 20/37] refactor all functions --- app/routes.py | 99 ++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 48 deletions(-) diff --git a/app/routes.py b/app/routes.py index d3f616c7f..c0467a804 100644 --- a/app/routes.py +++ b/app/routes.py @@ -15,65 +15,68 @@ def validate_int(user_input, attribute_name): except: abort(make_response({"error": "{attribute_name} must be an int"}, 404)) +def get_planet(planet_id): + validate_int(planet_id, "id") + + return Planet.query.get_or_404(planet_id, description="Planet does not exist.") + # Routes -@planets_bp.route("", methods=["GET", "POST"]) -def handle_planets(): +@planets_bp.route("", methods=["POST"]) +def create_planets(): request_body = request.get_json() - if request.method == "POST": - if "name" not in request_body: - return make_response("Invalid Request", 404) + if request_body is None: + return make_response("Invalid Request", 404) - new_planet = Planet( - name=request_body['name'], - # description=request_body['description'], - # xenomorphs=request_body[''] - ) + new_planet = Planet( + name=request_body['name'], + description=request_body['description'], + xenomorphs=request_body['xenomorphs'] + ) - db.session.add(new_planet) # like git, stagging changes - db.session.commit() # committing to database + db.session.add(new_planet) # like git, stagging changes + db.session.commit() # committing to database - return make_response(f"Your planet, {new_planet.name}, has been created.", 201) + return make_response(f"Your planet, {new_planet.name}, has been created.", 201) - elif request.method == "GET": - planets = Planet.query.all() - planets_response = [] +@planets_bp.route("", methods=["GET"]) +def handle_planets(): + planets = Planet.query.all() + planets_response = [] + + for planet in planets: + planets_response.append(planet.to_json()) - for planet in planets: - planets_response.append(planet.to_json()) + return jsonify(planets_response) - return jsonify(planets_response) +@planets_bp.route("/", methods=["GET"]) +def get_planet(planet_id): + planet = get_planet(planet_id) + return jsonify(planet.to_json(), 200) -@planets_bp.route("/", methods=["GET", "PATCH", "DELETE"]) +@planets_bp.route("/", methods=["PATCH"]) def get_planet(planet_id): request_body = request.get_json() - planet = Planet.query.get(planet_id) + planet = get_planet(planet_id) - try: - planet_id = int(planet_id) - except: - abort(make_response({"error": "planet_id must be an int"}, 400)) - - if request.method == 'GET': - if planet == None: - return make_response("your planet ain't real.", 404) - return planet.to_json() - - elif request.method == 'PATCH': - if "id" in request_body: - planet.id = request_body["id"] - if "name" in request_body: - planet.name = request_body["name"] - if "description" in request_body: - planet.description = request_body["description"] - if "xenomorphs" in request_body: - planet.xenomorphs = request_body["xenomorphs"] - - db.session.commit() - return jsonify(planet.to_json(), 201) - - elif request.method == 'DELETE': - db.session.delete(planet) - db.session.commit() - return make_response(f"Planet #{planet_id} successfully destroyed.", 200) \ No newline at end of file + if "id" in request_body: + planet.id = request_body["id"] + if "name" in request_body: + planet.name = request_body["name"] + if "description" in request_body: + planet.description = request_body["description"] + if "xenomorphs" in request_body: + planet.xenomorphs = request_body["xenomorphs"] + + db.session.commit() + return jsonify(planet.to_json(), 201) + + +@planets_bp.route("/", methods=["DELETE"]) +def get_planet(planet_id): + planet = get_planet(planet_id) + + db.session.delete(planet) + db.session.commit() + return make_response(f"Planet #{planet_id} successfully destroyed.", 200) \ No newline at end of file From 7caf8d2307339841f3b5aaa576d86c7b84b1bfb0 Mon Sep 17 00:00:00 2001 From: jacy Date: Wed, 27 Oct 2021 11:33:47 -0700 Subject: [PATCH 21/37] update function names --- app/routes.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index c0467a804..f7519c748 100644 --- a/app/routes.py +++ b/app/routes.py @@ -40,7 +40,7 @@ def create_planets(): return make_response(f"Your planet, {new_planet.name}, has been created.", 201) @planets_bp.route("", methods=["GET"]) -def handle_planets(): +def read_all_planets(): planets = Planet.query.all() planets_response = [] @@ -51,12 +51,12 @@ def handle_planets(): @planets_bp.route("/", methods=["GET"]) -def get_planet(planet_id): +def read_a_planet(planet_id): planet = get_planet(planet_id) return jsonify(planet.to_json(), 200) @planets_bp.route("/", methods=["PATCH"]) -def get_planet(planet_id): +def update_a_planet(planet_id): request_body = request.get_json() planet = get_planet(planet_id) @@ -74,7 +74,7 @@ def get_planet(planet_id): @planets_bp.route("/", methods=["DELETE"]) -def get_planet(planet_id): +def delete_a_planet(planet_id): planet = get_planet(planet_id) db.session.delete(planet) From 08d0db397554b65cd269ddbdd15eb37bcd64fe3c Mon Sep 17 00:00:00 2001 From: jacy Date: Wed, 27 Oct 2021 11:42:33 -0700 Subject: [PATCH 22/37] add error codes for return statements --- app/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index f7519c748..54c340c71 100644 --- a/app/routes.py +++ b/app/routes.py @@ -47,7 +47,7 @@ def read_all_planets(): for planet in planets: planets_response.append(planet.to_json()) - return jsonify(planets_response) + return jsonify(planets_response, 200) @planets_bp.route("/", methods=["GET"]) From 9bd9398bc320045124bb89e10b2a03136a276567 Mon Sep 17 00:00:00 2001 From: jacy Date: Wed, 27 Oct 2021 11:51:41 -0700 Subject: [PATCH 23/37] add query search routes --- app/routes.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 54c340c71..5129a5239 100644 --- a/app/routes.py +++ b/app/routes.py @@ -41,9 +41,19 @@ def create_planets(): @planets_bp.route("", methods=["GET"]) def read_all_planets(): - planets = Planet.query.all() - planets_response = [] + + name_query = request.args.get("name") + xenomorphs_query = request.args.get("xenomorphs") + + if name_query: + planets = Planet.query.filter_by(name=name_query) + elif xenomorphs_query: + planets = Planet.query.filter_by(xenomorphs=xenomorphs_query) + else: + planets = Planet.query.all() + + planets_response = [] for planet in planets: planets_response.append(planet.to_json()) From 45f4ace35aa930069f3aea9307829a2e3f78a1d8 Mon Sep 17 00:00:00 2001 From: jacy Date: Wed, 27 Oct 2021 12:00:14 -0700 Subject: [PATCH 24/37] add purge --- app/routes.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 5129a5239..d75c1495e 100644 --- a/app/routes.py +++ b/app/routes.py @@ -52,8 +52,11 @@ def read_all_planets(): else: planets = Planet.query.all() - planets_response = [] + + # if planets is None: + # return make_response("ENVIRON CTR PURGE", 404) + for planet in planets: planets_response.append(planet.to_json()) From edf2bfcbec5e5a906815184d4a1531b2d5c69a27 Mon Sep 17 00:00:00 2001 From: jacy Date: Thu, 28 Oct 2021 10:42:40 -0700 Subject: [PATCH 25/37] fix bugs in create_planets function --- app/routes.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index d75c1495e..819ed4daf 100644 --- a/app/routes.py +++ b/app/routes.py @@ -28,6 +28,9 @@ def create_planets(): if request_body is None: return make_response("Invalid Request", 404) + if "name" not in request_body or "description" not in request_body or "xenomorphs" not in request_body: + return make_response("Invalid Request", 404) + new_planet = Planet( name=request_body['name'], description=request_body['description'], @@ -54,8 +57,8 @@ def read_all_planets(): planets_response = [] - # if planets is None: - # return make_response("ENVIRON CTR PURGE", 404) + if planets is None: + return make_response("ENVIRON CTR PURGE", 404) for planet in planets: planets_response.append(planet.to_json()) From 351d71d4367d4dba5a4b0aec519ece5980e09d44 Mon Sep 17 00:00:00 2001 From: jacy Date: Thu, 28 Oct 2021 11:06:59 -0700 Subject: [PATCH 26/37] update create_planets function --- app/routes.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/app/routes.py b/app/routes.py index 819ed4daf..b4d56c7fa 100644 --- a/app/routes.py +++ b/app/routes.py @@ -9,15 +9,14 @@ # Helper Functions -def validate_int(user_input, attribute_name): - try: - user_input = int(user_input) - except: - abort(make_response({"error": "{attribute_name} must be an int"}, 404)) +# def validate_int(user_input, attribute_name): +# try: +# user_input = int(user_input) +# except: +# abort(make_response({"error": "{attribute_name} must be an int"}, 404)) def get_planet(planet_id): - validate_int(planet_id, "id") - + # validate_int(planet_id, "id") return Planet.query.get_or_404(planet_id, description="Planet does not exist.") # Routes @@ -57,8 +56,8 @@ def read_all_planets(): planets_response = [] - if planets is None: - return make_response("ENVIRON CTR PURGE", 404) + # if planets is None: + # return make_response("ENVIRON CTR PURGE", 404) for planet in planets: planets_response.append(planet.to_json()) @@ -86,7 +85,7 @@ def update_a_planet(planet_id): planet.xenomorphs = request_body["xenomorphs"] db.session.commit() - return jsonify(planet.to_json(), 201) + return make_response(f"Planet {planet.id} has been updated.", 201) @planets_bp.route("/", methods=["DELETE"]) From 8aafbfbfb7b49055fbc1c1c20f551ca85d95ccc6 Mon Sep 17 00:00:00 2001 From: jacy Date: Thu, 28 Oct 2021 11:39:46 -0700 Subject: [PATCH 27/37] set up environmental vars --- app/__init__.py | 9 ++++++++- app/routes.py | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 0371e44cf..42d10c698 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,6 +1,8 @@ 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() @@ -10,7 +12,12 @@ def create_app(test_config=None): app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - app.config['SQLALCHEMY_DATABASE_URI'] = database + + if not test_config: + app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI') + else: + app.config["TESTING"] = True + app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_TEST_DATABASE_URI') db.init_app(app) migrate.init_app(app, db) diff --git a/app/routes.py b/app/routes.py index b4d56c7fa..2419b0bd1 100644 --- a/app/routes.py +++ b/app/routes.py @@ -56,12 +56,12 @@ def read_all_planets(): planets_response = [] - # if planets is None: - # return make_response("ENVIRON CTR PURGE", 404) - for planet in planets: planets_response.append(planet.to_json()) + # if planets is None: + # return make_response("ENVIRON CTR PURGE", 404) + return jsonify(planets_response, 200) From 42e0d2cc4c44708a6be9ef6e9beb353d89e92250 Mon Sep 17 00:00:00 2001 From: jacy Date: Thu, 28 Oct 2021 11:43:55 -0700 Subject: [PATCH 28/37] set up test files --- app/__init__.py | 2 +- tests/__init__.py | 0 tests/conftest.py | 0 tests/test_routes.py | 0 4 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_routes.py diff --git a/app/__init__.py b/app/__init__.py index 42d10c698..1e2920bfc 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -6,7 +6,7 @@ db = SQLAlchemy() migrate = Migrate() -database = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' +load_dotenv() def create_app(test_config=None): app = Flask(__name__) 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..e69de29bb diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 000000000..e69de29bb From 19ece102308afe029d4119e179db018f62c38e1f Mon Sep 17 00:00:00 2001 From: jacy Date: Thu, 28 Oct 2021 11:57:39 -0700 Subject: [PATCH 29/37] add test routes --- tests/conftest.py | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_routes.py | 24 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index e69de29bb..3e37a2393 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -0,0 +1,38 @@ +# file to store all my fixtures; confiure test: conftest + +import pytest +from app import create_app +from app import db +from app.models.planet import Planet + +# app +@pytest +def app(): + app = create_app({"TESTING": True}) + + with app.app_context(): + db.create_all() # creates empty database + yield app + + with app.app_context(): + db.drop_all() # deletes data + +# client +@pytest.fixture +def client(app): + return app.test_client() + +# data +@pytest.fixture +def save_two_planets(app): + # Arrange + dune_planet = Planet(name="Arrakis", + description="sand", + xenomorhps=False) + + water_planet = Planet(title="Water", + description="waterisLife", + xenomorphs=True) + + db.session.add_all([dune_planet, water_planet]) + db.session.commit() diff --git a/tests/test_routes.py b/tests/test_routes.py index e69de29bb..b9b464623 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -0,0 +1,24 @@ +# get all books and return no records +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 == [] + +# get one book by id +def test_get_one_planet(client, two_saved_planets): + # Act + response = client.get("/planets/1") + response_body = response.get_json() + + #Assert + assert response.status_code == 200 + assert response_body == { + 'id' : 1, + 'name' : 'Arrakis', + 'description' : 'sand', + 'xenomorphs' : False + } From 54abab44a2c67bee256c9f80e8af7f9be63dfa6a Mon Sep 17 00:00:00 2001 From: jacy Date: Thu, 28 Oct 2021 12:06:27 -0700 Subject: [PATCH 30/37] update --- tests/conftest.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 3e37a2393..ea5692a11 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,7 @@ # file to store all my fixtures; confiure test: conftest - +# from flask import Flask import pytest -from app import create_app -from app import db +from app import create_app, db from app.models.planet import Planet # app From 49302c35e1b9b9e5e1647729430996b72ff9e9e6 Mon Sep 17 00:00:00 2001 From: jacy Date: Thu, 28 Oct 2021 13:07:24 -0700 Subject: [PATCH 31/37] fix test bugs --- app/__init__.py | 3 +++ app/routes.py | 3 ++- tests/conftest.py | 12 ++++++------ tests/test_routes.py | 14 +++++++------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 1e2920bfc..176d72d95 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -22,6 +22,9 @@ def create_app(test_config=None): db.init_app(app) migrate.init_app(app, db) + #import models + from app.models.planet import Planet + from .routes import planets_bp app.register_blueprint(planets_bp) diff --git a/app/routes.py b/app/routes.py index 2419b0bd1..68b673b7c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -41,6 +41,7 @@ def create_planets(): return make_response(f"Your planet, {new_planet.name}, has been created.", 201) + @planets_bp.route("", methods=["GET"]) def read_all_planets(): @@ -62,7 +63,7 @@ def read_all_planets(): # if planets is None: # return make_response("ENVIRON CTR PURGE", 404) - return jsonify(planets_response, 200) + return jsonify(planets_response) @planets_bp.route("/", methods=["GET"]) diff --git a/tests/conftest.py b/tests/conftest.py index ea5692a11..b7175f784 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,20 +1,20 @@ # file to store all my fixtures; confiure test: conftest -# from flask import Flask + import pytest from app import create_app, db from app.models.planet import Planet # app -@pytest +@pytest.fixture def app(): app = create_app({"TESTING": True}) with app.app_context(): - db.create_all() # creates empty database + db.create_all() yield app with app.app_context(): - db.drop_all() # deletes data + db.drop_all() # client @pytest.fixture @@ -27,9 +27,9 @@ def save_two_planets(app): # Arrange dune_planet = Planet(name="Arrakis", description="sand", - xenomorhps=False) + xenomorphs=False) - water_planet = Planet(title="Water", + water_planet = Planet(name="Water", description="waterisLife", xenomorphs=True) diff --git a/tests/test_routes.py b/tests/test_routes.py index b9b464623..b0f93dbf1 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -9,16 +9,16 @@ def test_get_all_planets_with_no_records(client): assert response_body == [] # get one book by id -def test_get_one_planet(client, two_saved_planets): +def test_get_one_planet(client, save_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' : 'Arrakis', - 'description' : 'sand', - 'xenomorphs' : False - } + # assert response_body == { + # 'id' : 1, + # 'name' : 'Arrakis', + # 'description' : 'sand', + # 'xenomorphs' : False + # } From 67b3bf406744f973d3195c8263d5299195203a35 Mon Sep 17 00:00:00 2001 From: jacy Date: Thu, 28 Oct 2021 13:15:06 -0700 Subject: [PATCH 32/37] fix test bugs, update return statement --- app/routes.py | 2 +- tests/test_routes.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/routes.py b/app/routes.py index 68b673b7c..aece3f238 100644 --- a/app/routes.py +++ b/app/routes.py @@ -69,7 +69,7 @@ def read_all_planets(): @planets_bp.route("/", methods=["GET"]) def read_a_planet(planet_id): planet = get_planet(planet_id) - return jsonify(planet.to_json(), 200) + return planet.to_json() @planets_bp.route("/", methods=["PATCH"]) def update_a_planet(planet_id): diff --git a/tests/test_routes.py b/tests/test_routes.py index b0f93dbf1..8303a74aa 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -16,9 +16,9 @@ def test_get_one_planet(client, save_two_planets): #Assert assert response.status_code == 200 - # assert response_body == { - # 'id' : 1, - # 'name' : 'Arrakis', - # 'description' : 'sand', - # 'xenomorphs' : False - # } + assert response_body == { + 'id' : 1, + 'name' : 'Arrakis', + 'description' : 'sand', + 'xenomorphs' : False + } From 9af523ea51c2fccb3dad916d93cee594cf6021be Mon Sep 17 00:00:00 2001 From: jacy Date: Thu, 28 Oct 2021 14:34:32 -0700 Subject: [PATCH 33/37] change error code --- app/routes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index aece3f238..344b6686c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -25,10 +25,10 @@ def create_planets(): request_body = request.get_json() if request_body is None: - return make_response("Invalid Request", 404) + return make_response("Invalid Request", 400) if "name" not in request_body or "description" not in request_body or "xenomorphs" not in request_body: - return make_response("Invalid Request", 404) + return make_response("Invalid Request", 400) new_planet = Planet( name=request_body['name'], From 663d89a619c4df6b4ed8619737642fb762d09770 Mon Sep 17 00:00:00 2001 From: jacy Date: Thu, 28 Oct 2021 15:12:14 -0700 Subject: [PATCH 34/37] finalized comments --- app/routes.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/app/routes.py b/app/routes.py index 344b6686c..1e70f2480 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,27 +1,23 @@ -# localhost:5000/ <-- add url endpoint/parameters here from flask import Blueprint, jsonify, make_response, request, abort from app.models.planet import Planet from app import db + # Global Vars planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") # Helper Functions -# def validate_int(user_input, attribute_name): -# try: -# user_input = int(user_input) -# except: -# abort(make_response({"error": "{attribute_name} must be an int"}, 404)) - def get_planet(planet_id): - # validate_int(planet_id, "id") + """Get planet by planet_id or return 404""" return Planet.query.get_or_404(planet_id, description="Planet does not exist.") + # Routes @planets_bp.route("", methods=["POST"]) def create_planets(): + """Create new planet in database.""" request_body = request.get_json() if request_body is None: @@ -36,22 +32,25 @@ def create_planets(): xenomorphs=request_body['xenomorphs'] ) - db.session.add(new_planet) # like git, stagging changes - db.session.commit() # committing to database + # add and commit new_planet to database + db.session.add(new_planet) + db.session.commit() return make_response(f"Your planet, {new_planet.name}, has been created.", 201) @planets_bp.route("", methods=["GET"]) def read_all_planets(): - + """Get all planets or get planets with query params""" name_query = request.args.get("name") xenomorphs_query = request.args.get("xenomorphs") + ##---partial functionality; would like to discuss---## if name_query: planets = Planet.query.filter_by(name=name_query) elif xenomorphs_query: planets = Planet.query.filter_by(xenomorphs=xenomorphs_query) + ##---END---## else: planets = Planet.query.all() @@ -60,19 +59,18 @@ def read_all_planets(): for planet in planets: planets_response.append(planet.to_json()) - # if planets is None: - # return make_response("ENVIRON CTR PURGE", 404) - return jsonify(planets_response) @planets_bp.route("/", methods=["GET"]) def read_a_planet(planet_id): + """Get planet with planet_id""" planet = get_planet(planet_id) return planet.to_json() @planets_bp.route("/", methods=["PATCH"]) def update_a_planet(planet_id): + """Update data for planet with planet_id in database""" request_body = request.get_json() planet = get_planet(planet_id) @@ -91,6 +89,7 @@ def update_a_planet(planet_id): @planets_bp.route("/", methods=["DELETE"]) def delete_a_planet(planet_id): + """Delete planet with planet_id in database""" planet = get_planet(planet_id) db.session.delete(planet) From 8b5fa845bc4e14a3ef0401d7519ddb0862bb0eda Mon Sep 17 00:00:00 2001 From: jacy Date: Wed, 3 Nov 2021 10:30:43 -0700 Subject: [PATCH 35/37] add procfile --- Procfile | 1 + 1 file changed, 1 insertion(+) create mode 100644 Procfile diff --git a/Procfile b/Procfile new file mode 100644 index 000000000..62e430aca --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn 'app:create_app()' \ No newline at end of file From b54163c69206d0b0ced49b5c0ad7a166367a5c83 Mon Sep 17 00:00:00 2001 From: jacy Date: Wed, 3 Nov 2021 10:32:09 -0700 Subject: [PATCH 36/37] add gunicorn to requirements.txt --- requirements.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/requirements.txt b/requirements.txt index fd90fffa8..e3a0c228c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ alembic==1.5.4 +attrs==21.2.0 autopep8==1.5.5 certifi==2020.12.5 chardet==4.0.0 @@ -6,13 +7,20 @@ click==7.1.2 Flask==1.1.2 Flask-Migrate==2.6.0 Flask-SQLAlchemy==2.4.4 +gunicorn==20.1.0 idna==2.10 +iniconfig==1.1.1 itsdangerous==1.1.0 Jinja2==2.11.3 Mako==1.1.4 MarkupSafe==1.1.1 +packaging==21.0 +pluggy==1.0.0 psycopg2-binary==2.8.6 +py==1.10.0 pycodestyle==2.6.0 +pyparsing==3.0.3 +pytest==6.2.5 python-dateutil==2.8.1 python-dotenv==0.15.0 python-editor==1.0.4 From ca7eb005e91a1663ba9ecb4da593e4bd2a2b76a8 Mon Sep 17 00:00:00 2001 From: KitSutliff <38538497+KitSutliff@users.noreply.github.com> Date: Wed, 3 Nov 2021 11:05:15 -0700 Subject: [PATCH 37/37] adds Procfile --- Procfile | 1 + requirements.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 Procfile diff --git a/Procfile b/Procfile new file mode 100644 index 000000000..066ed31d9 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn 'app:create_app()' diff --git a/requirements.txt b/requirements.txt index fd90fffa8..d2f045e58 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ click==7.1.2 Flask==1.1.2 Flask-Migrate==2.6.0 Flask-SQLAlchemy==2.4.4 +gunicorn==20.1.0 idna==2.10 itsdangerous==1.1.0 Jinja2==2.11.3