From f445de9fac89fc26c113453899f984e23f6b9aec Mon Sep 17 00:00:00 2001 From: nadia Date: Fri, 21 Apr 2023 11:30:14 -0700 Subject: [PATCH 01/28] defines planet class --- app/planets.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 app/planets.py diff --git a/app/planets.py b/app/planets.py new file mode 100644 index 000000000..c66ec38ab --- /dev/null +++ b/app/planets.py @@ -0,0 +1,9 @@ +# Define a Planet class with the attributes id, name, +# and description, and one additional attribute + +class Planet: + def __init__(self, id, name, description, mass): + self.id = id + self.name = name + self.description = description + self.mass = mass From e3c87f13abfe598814219427cc4c7e284ba10947 Mon Sep 17 00:00:00 2001 From: nadia Date: Fri, 21 Apr 2023 11:42:32 -0700 Subject: [PATCH 02/28] adds list of planets --- app/planets.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/planets.py b/app/planets.py index c66ec38ab..e3de6077e 100644 --- a/app/planets.py +++ b/app/planets.py @@ -2,8 +2,21 @@ # and description, and one additional attribute class Planet: - def __init__(self, id, name, description, mass): + def __init__(self, id, name, description, solar_day): self.id = id self.name = name self.description = description - self.mass = mass + self.solar_day = solar_day + + +planets = [ + Planet(1, "Mercury", "smallest planet", 176.0) + Planet(2, "Venus", "planet of love", 243.0) + Planet(3, "Earth", "home planet", 1.0) + Planet(4, "Mars", "red planet", 1.25) + Planet(5, "Jupiter", "largest planet", 0.42) + Planet(6, "Saturn", "ring planet", .45) + Planet(7, "Uranus", "coldest planet", .71) + Planet(8, "Neptune", "not visible to the naked eye", .67) + Planet(9, "Pluto", "unqualified planet", 6.375) + ] \ No newline at end of file From 73993bb2e9616bd47e21a03dd869008f702b653a Mon Sep 17 00:00:00 2001 From: nadia Date: Fri, 21 Apr 2023 11:44:11 -0700 Subject: [PATCH 03/28] adds missing commas to list of planets --- app/planets.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/planets.py b/app/planets.py index e3de6077e..47123f553 100644 --- a/app/planets.py +++ b/app/planets.py @@ -10,13 +10,13 @@ def __init__(self, id, name, description, solar_day): planets = [ - Planet(1, "Mercury", "smallest planet", 176.0) - Planet(2, "Venus", "planet of love", 243.0) - Planet(3, "Earth", "home planet", 1.0) - Planet(4, "Mars", "red planet", 1.25) - Planet(5, "Jupiter", "largest planet", 0.42) - Planet(6, "Saturn", "ring planet", .45) - Planet(7, "Uranus", "coldest planet", .71) - Planet(8, "Neptune", "not visible to the naked eye", .67) + Planet(1, "Mercury", "smallest planet", 176.0), + Planet(2, "Venus", "planet of love", 243.0), + Planet(3, "Earth", "home planet", 1.0), + Planet(4, "Mars", "red planet", 1.25), + Planet(5, "Jupiter", "largest planet", 0.42), + Planet(6, "Saturn", "ring planet", .45), + Planet(7, "Uranus", "coldest planet", .71), + Planet(8, "Neptune", "not visible to the naked eye", .67), Planet(9, "Pluto", "unqualified planet", 6.375) ] \ No newline at end of file From 1d266a277c8a5b128c940b3572ffba2c52585752 Mon Sep 17 00:00:00 2001 From: nadia Date: Fri, 21 Apr 2023 11:57:26 -0700 Subject: [PATCH 04/28] adds blueprint --- app/planets.py | 22 ---------------------- app/routes.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 23 deletions(-) delete mode 100644 app/planets.py diff --git a/app/planets.py b/app/planets.py deleted file mode 100644 index 47123f553..000000000 --- a/app/planets.py +++ /dev/null @@ -1,22 +0,0 @@ -# Define a Planet class with the attributes id, name, -# and description, and one additional attribute - -class Planet: - def __init__(self, id, name, description, solar_day): - self.id = id - self.name = name - self.description = description - self.solar_day = solar_day - - -planets = [ - Planet(1, "Mercury", "smallest planet", 176.0), - Planet(2, "Venus", "planet of love", 243.0), - Planet(3, "Earth", "home planet", 1.0), - Planet(4, "Mars", "red planet", 1.25), - Planet(5, "Jupiter", "largest planet", 0.42), - Planet(6, "Saturn", "ring planet", .45), - Planet(7, "Uranus", "coldest planet", .71), - Planet(8, "Neptune", "not visible to the naked eye", .67), - Planet(9, "Pluto", "unqualified planet", 6.375) - ] \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..f8b316728 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,36 @@ -from flask import Blueprint +from flask import Blueprint, jsonify +class Planet: + def __init__(self, id, name, description, solar_day): + self.id = id + self.name = name + self.description = description + self.solar_day = solar_day + + +planets = [ + Planet(1, "Mercury", "smallest planet", 176.0), + Planet(2, "Venus", "planet of love", 243.0), + Planet(3, "Earth", "home planet", 1.0), + Planet(4, "Mars", "red planet", 1.25), + Planet(5, "Jupiter", "largest planet", 0.42), + Planet(6, "Saturn", "ring planet", .45), + Planet(7, "Uranus", "coldest planet", .71), + Planet(8, "Neptune", "not visible to the naked eye", .67), + Planet(9, "Pluto", "unqualified planet", 6.375) + ] + +bp = Blueprint("planets", __name__, url_prefix="/planets") + +@bp.route("", methods=["GET"]) +def handle_planets(): + results = [] + for planet in planets: + results.append(dict( + id=planet.id, + name=planet.name, + description=planet.description, + solar_day=planet.solar_day + )) + + return jsonify(results) \ No newline at end of file From 469adc32929c3ed9bb0cabbcefed7586d29853ad Mon Sep 17 00:00:00 2001 From: nadia Date: Mon, 24 Apr 2023 16:18:51 -0700 Subject: [PATCH 05/28] minor changes --- app/__init__.py | 3 +++ app/routes.py | 6 +++--- 2 files changed, 6 insertions(+), 3 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 f8b316728..96cd29c37 100644 --- a/app/routes.py +++ b/app/routes.py @@ -20,11 +20,11 @@ def __init__(self, id, name, description, solar_day): Planet(9, "Pluto", "unqualified planet", 6.375) ] -bp = Blueprint("planets", __name__, url_prefix="/planets") +planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -@bp.route("", methods=["GET"]) +@planets_bp.route("", methods=["GET"]) def handle_planets(): - results = [] + results = [] for planet in planets: results.append(dict( id=planet.id, From fb3c2bf4d2afd6ed8c047be2f78bea6991ddba6d Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Mon, 24 Apr 2023 18:21:48 -0500 Subject: [PATCH 06/28] reads one planet and handles invalid and non existent id --- app/planets.py | 16 ++++++------ app/routes.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/app/planets.py b/app/planets.py index e3de6077e..47123f553 100644 --- a/app/planets.py +++ b/app/planets.py @@ -10,13 +10,13 @@ def __init__(self, id, name, description, solar_day): planets = [ - Planet(1, "Mercury", "smallest planet", 176.0) - Planet(2, "Venus", "planet of love", 243.0) - Planet(3, "Earth", "home planet", 1.0) - Planet(4, "Mars", "red planet", 1.25) - Planet(5, "Jupiter", "largest planet", 0.42) - Planet(6, "Saturn", "ring planet", .45) - Planet(7, "Uranus", "coldest planet", .71) - Planet(8, "Neptune", "not visible to the naked eye", .67) + Planet(1, "Mercury", "smallest planet", 176.0), + Planet(2, "Venus", "planet of love", 243.0), + Planet(3, "Earth", "home planet", 1.0), + Planet(4, "Mars", "red planet", 1.25), + Planet(5, "Jupiter", "largest planet", 0.42), + Planet(6, "Saturn", "ring planet", .45), + Planet(7, "Uranus", "coldest planet", .71), + Planet(8, "Neptune", "not visible to the naked eye", .67), Planet(9, "Pluto", "unqualified planet", 6.375) ] \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..2930afb39 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,68 @@ -from flask import Blueprint +from flask import Blueprint, jsonify, abort, make_response +# Define a Planet class with the attributes id, name, +# and description, and one additional attribute + +class Planet: + def __init__(self, id, name, description, solar_day): + self.id = id + self.name = name + self.description = description + self.solar_day = solar_day + + +planets = [ + Planet(1, "Mercury", "smallest planet", 176.0), + Planet(2, "Venus", "planet of love", 243.0), + Planet(3, "Earth", "home planet", 1.0), + Planet(4, "Mars", "red planet", 1.25), + Planet(5, "Jupiter", "largest planet", 0.42), + Planet(6, "Saturn", "ring planet", .45), + Planet(7, "Uranus", "coldest planet", .71), + Planet(8, "Neptune", "not visible to the naked eye", .67), + Planet(9, "Pluto", "unqualified planet", 6.375) + ] + + +bp = Blueprint("planets". __name__, url_prefix="/planets") + +@bp.route("", methods=["GET"]) +def handle_planets(): + results = [] + for planet in planets: + results.append(dict( + id=planet.id, + name=planet.name, + description=planet.description, + solar_day=planet.solar_day + )) + return jsonify(results) + + # Wave 2 + # read one planet + # return 400 for invalid id + # return 404 for non existing planet + +def validate_planet(planet_id): + try: + planet_id = int(planet_id) + except: + abort(make_response({"message":f"book {planet_id} invalid"}, 400)) + + for planet in planets: + if planet.id == planet_id: + return planet + + abort(make_response({"message":f"book {planet_id} not found"}, 404)) + + +@bp.route("/", methods=["GET"]) +def handle_planet(planet_id): + planet = validate_planet(planet_id) + + return { + "id": planet.id, + "name": planet.name, + "description": planet.description, + "solar_day": planet.solar_day + } \ No newline at end of file From 8b27493310838f770d41ea1be6a95b3043bf72b0 Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Mon, 24 Apr 2023 18:29:05 -0500 Subject: [PATCH 07/28] adds decorator to line 85 --- app/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 1ba42dc1a..e2acf0826 100644 --- a/app/routes.py +++ b/app/routes.py @@ -82,7 +82,7 @@ def handle_planets(): )) return jsonify(results) -@bp.route("/", methods=["GET"]) +@planets_bp.route("/", methods=["GET"]) def handle_planet(planet_id): planet = validate_planet(planet_id) From 9edb99aa5f7334378ce0563a3f9b6dfba3bde111 Mon Sep 17 00:00:00 2001 From: nadia Date: Mon, 24 Apr 2023 16:43:04 -0700 Subject: [PATCH 08/28] adds to_dict function to planet class and deletes planets.py --- app/planets.py | 12 -------- app/routes.py | 78 ++++++++++++++++---------------------------------- 2 files changed, 24 insertions(+), 66 deletions(-) delete mode 100644 app/planets.py diff --git a/app/planets.py b/app/planets.py deleted file mode 100644 index 7ed295013..000000000 --- a/app/planets.py +++ /dev/null @@ -1,12 +0,0 @@ - -planets = [ - Planet(1, "Mercury", "smallest planet", 176.0), - Planet(2, "Venus", "planet of love", 243.0), - Planet(3, "Earth", "home planet", 1.0), - Planet(4, "Mars", "red planet", 1.25), - Planet(5, "Jupiter", "largest planet", 0.42), - Planet(6, "Saturn", "ring planet", .45), - Planet(7, "Uranus", "coldest planet", .71), - Planet(8, "Neptune", "not visible to the naked eye", .67), - Planet(9, "Pluto", "unqualified planet", 6.375) - ] \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index e2acf0826..1c7588f00 100644 --- a/app/routes.py +++ b/app/routes.py @@ -8,8 +8,15 @@ def __init__(self, id, name, description, solar_day): self.id = id self.name = name self.description = description - self.solar_day = solar_day + self.solar_day = solar_day + def to_dict(self): + return dict( + id=self.id, + name=self.name, + description=self.description, + solar_day=self.solar_day + ) planets = [ Planet(1, "Mercury", "smallest planet", 176.0), @@ -23,72 +30,35 @@ def __init__(self, id, name, description, solar_day): Planet(9, "Pluto", "unqualified planet", 6.375) ] - -bp = Blueprint("planets". __name__, url_prefix="/planets") - -@bp.route("", methods=["GET"]) -def handle_planets(): - results = [] - for planet in planets: - results.append(dict( - id=planet.id, - name=planet.name, - description=planet.description, - solar_day=planet.solar_day - )) - return jsonify(results) - # Wave 2 # read one planet # return 400 for invalid id # return 404 for non existing planet - -def validate_planet(planet_id): - try: - planet_id = int(planet_id) - except: - abort(make_response({"message":f"book {planet_id} invalid"}, 400)) - - for planet in planets: - if planet.id == planet_id: - return planet - - abort(make_response({"message":f"book {planet_id} not found"}, 404)) - - -planets = [ - Planet(1, "Mercury", "smallest planet", 176.0), - Planet(2, "Venus", "planet of love", 243.0), - Planet(3, "Earth", "home planet", 1.0), - Planet(4, "Mars", "red planet", 1.25), - Planet(5, "Jupiter", "largest planet", 0.42), - Planet(6, "Saturn", "ring planet", .45), - Planet(7, "Uranus", "coldest planet", .71), - Planet(8, "Neptune", "not visible to the naked eye", .67), - Planet(9, "Pluto", "unqualified planet", 6.375) - ] - planets_bp = Blueprint("planets", __name__, url_prefix="/planets") @planets_bp.route("", methods=["GET"]) def handle_planets(): results = [] for planet in planets: - results.append(dict( - id=planet.id, - name=planet.name, - description=planet.description, - solar_day=planet.solar_day - )) + results.append(planet.to_dict()) return jsonify(results) + @planets_bp.route("/", methods=["GET"]) def handle_planet(planet_id): planet = validate_planet(planet_id) - return { - "id": planet.id, - "name": planet.name, - "description": planet.description, - "solar_day": planet.solar_day - } \ No newline at end of file + return planet.to_dict() + + +def validate_planet(planet_id): + try: + planet_id = int(planet_id) + except: + abort(make_response({"message":f"planet {planet_id} invalid"}, 400)) + + for planet in planets: + if planet.id == planet_id: + return planet + + abort(make_response({"message":f"planet {planet_id} not found"}, 404)) \ No newline at end of file From 10309dcc5deba92119710b67b814bfa07d812510 Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Fri, 28 Apr 2023 13:17:22 -0500 Subject: [PATCH 09/28] add Planet model and imports migrations folder --- app/__init__.py | 17 +++- app/models/__init__.py | 0 app/models/planet.py | 9 ++ migrations/README | 1 + migrations/alembic.ini | 50 ++++++++++ migrations/env.py | 91 +++++++++++++++++++ migrations/script.py.mako | 24 +++++ .../595fe7f869fa_adds_planet_model.py | 34 +++++++ 8 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 app/models/__init__.py create mode 100644 app/models/planet.py 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/595fe7f869fa_adds_planet_model.py diff --git a/app/__init__.py b/app/__init__.py index ab9eee40e..4f0e11d77 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,10 +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' - from .routes import planets_bp - app.register_blueprint(planets_bp) + db.init_app(app) + migrate.init_app(app, db) + + from app.models.planet import Planet - return app + from .routes import planets_bp + app.register_blueprint(planets_bp) + + return app \ No newline at end of file diff --git a/app/models/__init__.py b/app/models/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/models/planet.py b/app/models/planet.py new file mode 100644 index 000000000..7fe54d866 --- /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, nullable=False) + description=db.Column(db.String, nullable=False) + solar_day=db.Column(db.Float, nullable=False) + + \ No newline at end of file diff --git a/migrations/README b/migrations/README new file mode 100644 index 000000000..0e0484415 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Single-database configuration for Flask. diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 000000000..ec9d45c26 --- /dev/null +++ b/migrations/alembic.ini @@ -0,0 +1,50 @@ +# 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,flask_migrate + +[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 + +[logger_flask_migrate] +level = INFO +handlers = +qualname = flask_migrate + +[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..68feded2a --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,91 @@ +from __future__ import with_statement + +import logging +from logging.config import fileConfig + +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.get_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 = current_app.extensions['migrate'].db.get_engine() + + 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/595fe7f869fa_adds_planet_model.py b/migrations/versions/595fe7f869fa_adds_planet_model.py new file mode 100644 index 000000000..ff034719c --- /dev/null +++ b/migrations/versions/595fe7f869fa_adds_planet_model.py @@ -0,0 +1,34 @@ +"""adds Planet model + +Revision ID: 595fe7f869fa +Revises: +Create Date: 2023-04-28 13:12:50.013892 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '595fe7f869fa' +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=False), + sa.Column('description', sa.String(), nullable=False), + sa.Column('solar_day', sa.Float(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('planet') + # ### end Alembic commands ### From 689428cdbd21e76d4b38385663bfc2863d977668 Mon Sep 17 00:00:00 2001 From: nadia Date: Sun, 30 Apr 2023 16:47:43 -0700 Subject: [PATCH 10/28] changes GET method to use list comprehension --- app/routes.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/routes.py b/app/routes.py index 1c7588f00..d717a8b56 100644 --- a/app/routes.py +++ b/app/routes.py @@ -38,9 +38,7 @@ def to_dict(self): @planets_bp.route("", methods=["GET"]) def handle_planets(): - results = [] - for planet in planets: - results.append(planet.to_dict()) + results = [planet.to_dict() for planet in planets] return jsonify(results) From 12d5e0284b089cbea762b621eaeacfa9ea1e7a76 Mon Sep 17 00:00:00 2001 From: nadia Date: Sun, 30 Apr 2023 16:51:34 -0700 Subject: [PATCH 11/28] removes old Planet class in favor of Model --- app/routes.py | 52 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/app/routes.py b/app/routes.py index d717a8b56..5661356a1 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,32 +3,32 @@ # Define a Planet class with the attributes id, name, # and description, and one additional attribute -class Planet: - def __init__(self, id, name, description, solar_day): - self.id = id - self.name = name - self.description = description - self.solar_day = solar_day - - def to_dict(self): - return dict( - id=self.id, - name=self.name, - description=self.description, - solar_day=self.solar_day - ) - -planets = [ - Planet(1, "Mercury", "smallest planet", 176.0), - Planet(2, "Venus", "planet of love", 243.0), - Planet(3, "Earth", "home planet", 1.0), - Planet(4, "Mars", "red planet", 1.25), - Planet(5, "Jupiter", "largest planet", 0.42), - Planet(6, "Saturn", "ring planet", .45), - Planet(7, "Uranus", "coldest planet", .71), - Planet(8, "Neptune", "not visible to the naked eye", .67), - Planet(9, "Pluto", "unqualified planet", 6.375) - ] +# class Planet: +# def __init__(self, id, name, description, solar_day): +# self.id = id +# self.name = name +# self.description = description +# self.solar_day = solar_day + +# def to_dict(self): +# return dict( +# id=self.id, +# name=self.name, +# description=self.description, +# solar_day=self.solar_day +# ) + +# planets = [ +# Planet(1, "Mercury", "smallest planet", 176.0), +# Planet(2, "Venus", "planet of love", 243.0), +# Planet(3, "Earth", "home planet", 1.0), +# Planet(4, "Mars", "red planet", 1.25), +# Planet(5, "Jupiter", "largest planet", 0.42), +# Planet(6, "Saturn", "ring planet", .45), +# Planet(7, "Uranus", "coldest planet", .71), +# Planet(8, "Neptune", "not visible to the naked eye", .67), +# Planet(9, "Pluto", "unqualified planet", 6.375) +# ] # Wave 2 # read one planet From 99fa769e9ac048e26e879143a4616ca989e6b7bb Mon Sep 17 00:00:00 2001 From: nadia Date: Sun, 30 Apr 2023 17:18:10 -0700 Subject: [PATCH 12/28] defines create method for Planet --- app/routes.py | 52 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/app/routes.py b/app/routes.py index 5661356a1..36fdc63e8 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,19 @@ -from flask import Blueprint, jsonify, abort, make_response +from app import db +from app.models.planet import Planet +from flask import Blueprint, jsonify, abort, make_response, request + +planets_bp = Blueprint("planets", __name__, url_prefix="/planets") + +@planets_bp.route("", methods=["POST"]) +def create_planet(): + request_body = request.get_json() + new_planet = Planet(name=request_body["name"], + description=request_body["description"], + solar_day=request_body["solar day"]) + db.session.add(new_planet) + db.session.commit() + + return make_response(f"Planet {new_planet.name} created successfully.", 201) # Define a Planet class with the attributes id, name, # and description, and one additional attribute @@ -34,29 +49,28 @@ # read one planet # return 400 for invalid id # return 404 for non existing planet -planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -@planets_bp.route("", methods=["GET"]) -def handle_planets(): - results = [planet.to_dict() for planet in planets] +# @planets_bp.route("", methods=["GET"]) +# def handle_planets(): +# results = [planet.to_dict() for planet in planets] - return jsonify(results) +# return jsonify(results) -@planets_bp.route("/", methods=["GET"]) -def handle_planet(planet_id): - planet = validate_planet(planet_id) +# @planets_bp.route("/", methods=["GET"]) +# def handle_planet(planet_id): +# planet = validate_planet(planet_id) - return planet.to_dict() +# return planet.to_dict() -def validate_planet(planet_id): - try: - planet_id = int(planet_id) - except: - abort(make_response({"message":f"planet {planet_id} invalid"}, 400)) +# def validate_planet(planet_id): +# try: +# planet_id = int(planet_id) +# except: +# abort(make_response({"message":f"planet {planet_id} invalid"}, 400)) - for planet in planets: - if planet.id == planet_id: - return planet +# for planet in planets: +# if planet.id == planet_id: +# return planet - abort(make_response({"message":f"planet {planet_id} not found"}, 404)) \ No newline at end of file +# abort(make_response({"message":f"planet {planet_id} not found"}, 404)) \ No newline at end of file From 7ea730a6f3470df6b8d3ca54d006733ca11738c9 Mon Sep 17 00:00:00 2001 From: nadia Date: Sun, 30 Apr 2023 17:34:47 -0700 Subject: [PATCH 13/28] creates READ ALL planets endpoint --- app/models/planet.py | 8 +++++++- app/routes.py | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/planet.py b/app/models/planet.py index 7fe54d866..6cdf8d3f2 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -6,4 +6,10 @@ class Planet(db.Model): description=db.Column(db.String, nullable=False) solar_day=db.Column(db.Float, nullable=False) - \ No newline at end of file + def to_dict(self): + return { + "id": self.id, + "name": self.name, + "description": self.description, + "solar day": self.solar_day + } \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 36fdc63e8..035799dde 100644 --- a/app/routes.py +++ b/app/routes.py @@ -15,6 +15,14 @@ def create_planet(): return make_response(f"Planet {new_planet.name} created successfully.", 201) +@planets_bp.route("", methods=["GET"]) +def get_all_planets(): + planets = Planet.query.all() + results = [planet.to_dict() for planet in planets] + + return jsonify(results) + + # Define a Planet class with the attributes id, name, # and description, and one additional attribute From 0582bb46f65857e0ef752ee7718d041a356b68ee Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Tue, 2 May 2023 13:20:28 -0500 Subject: [PATCH 14/28] adds route for get_one_planet --- app/routes.py | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/app/routes.py b/app/routes.py index 035799dde..4466b1ef6 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,6 +4,21 @@ planets_bp = Blueprint("planets", __name__, url_prefix="/planets") + +def validate_planet(planet_id): + try: + planet_id = int(planet_id) + except: + abort(make_response({"message":f"planet {planet_id} invalid"}, 400)) + + planet = Planet.query.get(planet_id) + if not planet: + abort(make_response({"message":f"planet {planet_id} not found"}, 404)) + + return planet + + + @planets_bp.route("", methods=["POST"]) def create_planet(): request_body = request.get_json() @@ -22,6 +37,21 @@ def get_all_planets(): return jsonify(results) +@planets_bp.route("/", methods=["GET"]) +def get_one_planet(planet_id): + planet = validate_planet(planet_id) + + return { + "id": planet.id, + "name": planet.name, + "description": planet.description, + "solar day": planet.solar_day + } + + + + + # Define a Planet class with the attributes id, name, # and description, and one additional attribute @@ -71,14 +101,6 @@ def get_all_planets(): # return planet.to_dict() -# def validate_planet(planet_id): -# try: -# planet_id = int(planet_id) -# except: -# abort(make_response({"message":f"planet {planet_id} invalid"}, 400)) - -# for planet in planets: -# if planet.id == planet_id: -# return planet + -# abort(make_response({"message":f"planet {planet_id} not found"}, 404)) \ No newline at end of file + \ No newline at end of file From 479455b6b634aa23ddfb92406541e34824007bba Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Tue, 2 May 2023 13:39:40 -0500 Subject: [PATCH 15/28] adds replace_planet route --- app/routes.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/routes.py b/app/routes.py index 4466b1ef6..29c1005f3 100644 --- a/app/routes.py +++ b/app/routes.py @@ -48,6 +48,19 @@ def get_one_planet(planet_id): "solar day": planet.solar_day } +@planets_bp.route("/", methods=["PUT"]) +def replace_planet(planet_id): + planet = validate_planet(planet_id) + planet_to_update = request.get_json() + planet.name = planet_to_update["name"] + planet.description = planet_to_update["description"] + planet.solar_day = planet_to_update["solar day"] + + db.session.commit() + + return make_response(f"Planet {planet.name} updated successfully.") + + From 73f5bc9dd94f3d4e5dbc383b914764ab4368023b Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Tue, 2 May 2023 13:44:44 -0500 Subject: [PATCH 16/28] adds delete_planet route --- app/routes.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/routes.py b/app/routes.py index 29c1005f3..ca3900e77 100644 --- a/app/routes.py +++ b/app/routes.py @@ -61,7 +61,13 @@ def replace_planet(planet_id): return make_response(f"Planet {planet.name} updated successfully.") +@planets_bp.route("/", methods=["DELETE"]) +def delete_planet(planet_id): + planet_to_delete = validate_planet(planet_id) + db.session.delete(planet_to_delete) + db.session.commit() + return make_response(f"Planet {planet_to_delete.name} was deleted.") From 8a0cae7794f3d0c6bb03c785882db284e16ff965 Mon Sep 17 00:00:00 2001 From: nadia Date: Wed, 3 May 2023 11:23:56 -0700 Subject: [PATCH 17/28] sets up dotenv --- app/__init__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 4f0e11d77..f29fb6122 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,14 +1,24 @@ 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' + app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False + + 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) From 770cfe62d4a199ae713efa94b589d2f46d3ae67e Mon Sep 17 00:00:00 2001 From: nadia Date: Wed, 3 May 2023 11:27:40 -0700 Subject: [PATCH 18/28] adds test folder --- tests/__init__.py | 0 tests/conftest.py | 25 +++++++++++++++++++++++++ tests/test_routes.py | 0 3 files changed, 25 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.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..ac8a4193b --- /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() \ 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 04af34d2e0bc67399c7cb274edf241bf7632daaf Mon Sep 17 00:00:00 2001 From: nadia Date: Wed, 3 May 2023 11:32:13 -0700 Subject: [PATCH 19/28] adds test get all with no records --- tests/test_routes.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_routes.py b/tests/test_routes.py index e69de29bb..db95c86aa 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -0,0 +1,7 @@ +def test_get_all_planets_with_no_records(client): + response = client.get("/planets") + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == [] + From 76af12eb19eed0112118cd14875f99948250cfe3 Mon Sep 17 00:00:00 2001 From: nadia Date: Wed, 3 May 2023 11:41:29 -0700 Subject: [PATCH 20/28] adds test for one planet --- tests/conftest.py | 22 +++++++++++++++++++++- tests/test_routes.py | 11 +++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index ac8a4193b..a03a195b7 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 @@ -22,4 +23,23 @@ def expire_session(sender, response, **extra): @pytest.fixture def client(app): - return app.test_client() \ No newline at end of file + return app.test_client() + +@pytest.fixture +def two_planets(app): + nebula = Planet( + name="Nebula", + description="Fake planet for testing purposes", + solar_day=420.0 + ) + + gamora = Planet( + name="Gamora", + description="Do not visit. Not a real place.", + solar_day=666.0 + ) + + db.session.add_all([nebula, gamora]) + db.session.commit() + + \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py index db95c86aa..c3d5ca4c2 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -5,3 +5,14 @@ def test_get_all_planets_with_no_records(client): assert response.status_code == 200 assert response_body == [] +def test_get_one_planet(client, two_planets): + response = client.get("/planets/1") + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == { + "id": 1, + "name": "Nebula", + "description": "Fake planet for testing purposes", + "solar day": 420.0 + } \ No newline at end of file From fbf13167dfcda7d9693456b14278c751a4522c5c Mon Sep 17 00:00:00 2001 From: nadia Date: Wed, 3 May 2023 12:03:42 -0700 Subject: [PATCH 21/28] finishes tests for all CRUD methods --- app/routes.py | 10 +++++----- tests/test_routes.py | 46 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/routes.py b/app/routes.py index ca3900e77..a0ce5d96d 100644 --- a/app/routes.py +++ b/app/routes.py @@ -9,11 +9,11 @@ def validate_planet(planet_id): try: planet_id = int(planet_id) except: - abort(make_response({"message":f"planet {planet_id} invalid"}, 400)) + abort(make_response(jsonify({"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)) + abort(make_response(jsonify({"message":f"planet {planet_id} not found"}), 404)) return planet @@ -28,7 +28,7 @@ def create_planet(): db.session.add(new_planet) db.session.commit() - return make_response(f"Planet {new_planet.name} created successfully.", 201) + return make_response(jsonify(f"Planet {new_planet.name} created successfully."), 201) @planets_bp.route("", methods=["GET"]) def get_all_planets(): @@ -58,7 +58,7 @@ def replace_planet(planet_id): db.session.commit() - return make_response(f"Planet {planet.name} updated successfully.") + return make_response(jsonify(f"Planet {planet.name} updated successfully.")) @planets_bp.route("/", methods=["DELETE"]) @@ -67,7 +67,7 @@ def delete_planet(planet_id): db.session.delete(planet_to_delete) db.session.commit() - return make_response(f"Planet {planet_to_delete.name} was deleted.") + return make_response(jsonify(f"Planet {planet_to_delete.name} was deleted.")) diff --git a/tests/test_routes.py b/tests/test_routes.py index c3d5ca4c2..23266ba54 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -15,4 +15,48 @@ def test_get_one_planet(client, two_planets): "name": "Nebula", "description": "Fake planet for testing purposes", "solar day": 420.0 - } \ No newline at end of file + } + +def test_get_one_planet_no_data_404(client): + response = client.get("/planets/1") + response_body = response.get_json() + + assert response.status_code == 404 + assert response_body["message"] == "planet 1 not found" + +def test_get_one_planet_invalid_id_400(client): + response = client.get("/planets/WALLA-WALLA") + response_body = response.get_json() + + assert response.status_code == 400 + assert response_body["message"] == "planet WALLA-WALLA invalid" + +def test_post_one_planet(client): + response = client.post("/planets", json={ + "name": "Walla-Walla", + "description": "All Black.", + "solar day": 0.5 + }) + + response_body = response.get_json() + + assert response.status_code == 201 + assert response_body == "Planet Walla-Walla created successfully." + +def test_delete_one_planet(client, two_planets): + response = client.delete("/planets/1") + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == "Planet Nebula was deleted." + +def test_update_one_planet(client, two_planets): + response = client.put("/planets/1", json={ + "name": "Walla-Walla", + "description": "All Black.", + "solar day": 0.5 + }) + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == "Planet Walla-Walla updated successfully." From ef7e85931600e25e0d33eaf38de851c98415282a Mon Sep 17 00:00:00 2001 From: nadia Date: Wed, 3 May 2023 12:06:21 -0700 Subject: [PATCH 22/28] updates walla-walla description --- tests/test_routes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index 23266ba54..b68adf6fb 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -34,7 +34,7 @@ def test_get_one_planet_invalid_id_400(client): def test_post_one_planet(client): response = client.post("/planets", json={ "name": "Walla-Walla", - "description": "All Black.", + "description": "A bad place.", "solar day": 0.5 }) @@ -53,7 +53,7 @@ def test_delete_one_planet(client, two_planets): def test_update_one_planet(client, two_planets): response = client.put("/planets/1", json={ "name": "Walla-Walla", - "description": "All Black.", + "description": "A bad place.", "solar day": 0.5 }) response_body = response.get_json() From 268cca6c6f58c43ea9a1ddce941f56f0480bb730 Mon Sep 17 00:00:00 2001 From: nadia Date: Wed, 3 May 2023 12:09:56 -0700 Subject: [PATCH 23/28] creates fixture for walla-walla planet --- tests/conftest.py | 8 +++++++- tests/test_routes.py | 16 ++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a03a195b7..d6be9b47d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -42,4 +42,10 @@ def two_planets(app): db.session.add_all([nebula, gamora]) db.session.commit() - \ No newline at end of file +@pytest.fixture +def walla_walla(): + return { + "name": "Walla-Walla", + "description": "A bad place.", + "solar day": 0.5 + } \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py index b68adf6fb..518f65278 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -31,12 +31,8 @@ def test_get_one_planet_invalid_id_400(client): assert response.status_code == 400 assert response_body["message"] == "planet WALLA-WALLA invalid" -def test_post_one_planet(client): - response = client.post("/planets", json={ - "name": "Walla-Walla", - "description": "A bad place.", - "solar day": 0.5 - }) +def test_post_one_planet(client, walla_walla): + response = client.post("/planets", json=walla_walla) response_body = response.get_json() @@ -50,12 +46,8 @@ def test_delete_one_planet(client, two_planets): assert response.status_code == 200 assert response_body == "Planet Nebula was deleted." -def test_update_one_planet(client, two_planets): - response = client.put("/planets/1", json={ - "name": "Walla-Walla", - "description": "A bad place.", - "solar day": 0.5 - }) +def test_update_one_planet(client, two_planets, walla_walla): + response = client.put("/planets/1", json=walla_walla) response_body = response.get_json() assert response.status_code == 200 From 50d95604423439efdfcae9608490640dae54a6b5 Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Thu, 4 May 2023 16:29:37 -0500 Subject: [PATCH 24/28] refactors with to_dict, updates solar_day --- app/models/planet.py | 2 +- app/routes.py | 11 +++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/app/models/planet.py b/app/models/planet.py index 6cdf8d3f2..a1af13760 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -11,5 +11,5 @@ def to_dict(self): "id": self.id, "name": self.name, "description": self.description, - "solar day": self.solar_day + "solar_day": self.solar_day } \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index a0ce5d96d..4d6abfba8 100644 --- a/app/routes.py +++ b/app/routes.py @@ -24,7 +24,7 @@ def create_planet(): request_body = request.get_json() new_planet = Planet(name=request_body["name"], description=request_body["description"], - solar_day=request_body["solar day"]) + solar_day=request_body["solar_day"]) db.session.add(new_planet) db.session.commit() @@ -41,12 +41,7 @@ def get_all_planets(): def get_one_planet(planet_id): planet = validate_planet(planet_id) - return { - "id": planet.id, - "name": planet.name, - "description": planet.description, - "solar day": planet.solar_day - } + return planet.to_dict() @planets_bp.route("/", methods=["PUT"]) def replace_planet(planet_id): @@ -54,7 +49,7 @@ def replace_planet(planet_id): planet_to_update = request.get_json() planet.name = planet_to_update["name"] planet.description = planet_to_update["description"] - planet.solar_day = planet_to_update["solar day"] + planet.solar_day = planet_to_update["solar_day"] db.session.commit() From ef7512609152fdb9538c50f4ab0df496525356ba Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Thu, 4 May 2023 16:41:05 -0500 Subject: [PATCH 25/28] creates test_to_dict_no_missing_info test passes --- tests/test_models.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/test_models.py diff --git a/tests/test_models.py b/tests/test_models.py new file mode 100644 index 000000000..fa01ccfdc --- /dev/null +++ b/tests/test_models.py @@ -0,0 +1,19 @@ +from app.models.planet import Planet + +def test_to_dict_no_missing_data(): + #arrange + test_data = Planet( + id=1, + name="testopolis", + description="a testing place", + solar_day=300 + ) + #act + result = test_data.to_dict() + + #assert + assert len(result) == 4 + assert result["id"] == 1 + assert result["name"] == "testopolis" + assert result["solar_day"] == 300 + assert result["description"] == "a testing place" From 414cfb4f7633f42a9d4d38a1eff0fca6b70810dd Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Thu, 4 May 2023 17:09:11 -0500 Subject: [PATCH 26/28] adds from_dict class method and updated post to include it --- app/models/planet.py | 10 +++++++++- app/routes.py | 5 ++--- tests/conftest.py | 2 +- tests/test_routes.py | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/models/planet.py b/app/models/planet.py index a1af13760..95b76ba93 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -12,4 +12,12 @@ def to_dict(self): "name": self.name, "description": self.description, "solar_day": self.solar_day - } \ No newline at end of file + } + + @classmethod + def from_dict(cls, planet_data): + return Planet( + name=planet_data["name"], + description=planet_data["description"], + solar_day=planet_data["solar_day"] + ) diff --git a/app/routes.py b/app/routes.py index 4d6abfba8..161f62161 100644 --- a/app/routes.py +++ b/app/routes.py @@ -22,9 +22,7 @@ def validate_planet(planet_id): @planets_bp.route("", methods=["POST"]) def create_planet(): request_body = request.get_json() - new_planet = Planet(name=request_body["name"], - description=request_body["description"], - solar_day=request_body["solar_day"]) + new_planet = Planet.from_dict(request_body) db.session.add(new_planet) db.session.commit() @@ -47,6 +45,7 @@ def get_one_planet(planet_id): def replace_planet(planet_id): planet = validate_planet(planet_id) planet_to_update = request.get_json() + planet.name = planet_to_update["name"] planet.description = planet_to_update["description"] planet.solar_day = planet_to_update["solar_day"] diff --git a/tests/conftest.py b/tests/conftest.py index d6be9b47d..6b1e2ef10 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -47,5 +47,5 @@ def walla_walla(): return { "name": "Walla-Walla", "description": "A bad place.", - "solar day": 0.5 + "solar_day": 0.5 } \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py index 518f65278..7af7477b2 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -14,7 +14,7 @@ def test_get_one_planet(client, two_planets): "id": 1, "name": "Nebula", "description": "Fake planet for testing purposes", - "solar day": 420.0 + "solar_day": 420.0 } def test_get_one_planet_no_data_404(client): From e357b164a6dadfcdc3c6c22644aabd7ec1b0e179 Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Thu, 4 May 2023 17:23:52 -0500 Subject: [PATCH 27/28] updates validate_planet to validate_models, updates deleteget put routes to include it --- app/routes.py | 20 ++++++++++---------- tests/test_routes.py | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/routes.py b/app/routes.py index 161f62161..16de52bf3 100644 --- a/app/routes.py +++ b/app/routes.py @@ -5,17 +5,17 @@ planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -def validate_planet(planet_id): +def validate_model(cls, model_id): try: - planet_id = int(planet_id) + model_id = int(model_id) except: - abort(make_response(jsonify({"message":f"planet {planet_id} invalid"}), 400)) + abort(make_response(jsonify({"message":f"{cls.__name__} {model_id} invalid"}), 400)) - planet = Planet.query.get(planet_id) - if not planet: - abort(make_response(jsonify({"message":f"planet {planet_id} not found"}), 404)) + model = cls.query.get(model_id) + if not model: + abort(make_response(jsonify({"message":f"{cls.__name__} {model_id} not found"}), 404)) - return planet + return model @@ -37,13 +37,13 @@ def get_all_planets(): @planets_bp.route("/", methods=["GET"]) def get_one_planet(planet_id): - planet = validate_planet(planet_id) + planet = validate_model(Planet, planet_id) return planet.to_dict() @planets_bp.route("/", methods=["PUT"]) def replace_planet(planet_id): - planet = validate_planet(planet_id) + planet = validate_model(Planet, planet_id) planet_to_update = request.get_json() planet.name = planet_to_update["name"] @@ -57,7 +57,7 @@ def replace_planet(planet_id): @planets_bp.route("/", methods=["DELETE"]) def delete_planet(planet_id): - planet_to_delete = validate_planet(planet_id) + planet_to_delete = validate_model(Planet, planet_id) db.session.delete(planet_to_delete) db.session.commit() diff --git a/tests/test_routes.py b/tests/test_routes.py index 7af7477b2..f9f190dd1 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -22,14 +22,14 @@ def test_get_one_planet_no_data_404(client): response_body = response.get_json() assert response.status_code == 404 - assert response_body["message"] == "planet 1 not found" + assert response_body["message"] == "Planet 1 not found" def test_get_one_planet_invalid_id_400(client): response = client.get("/planets/WALLA-WALLA") response_body = response.get_json() assert response.status_code == 400 - assert response_body["message"] == "planet WALLA-WALLA invalid" + assert response_body["message"] == "Planet WALLA-WALLA invalid" def test_post_one_planet(client, walla_walla): response = client.post("/planets", json=walla_walla) From d4d8805ce8ec1aa22e78b039949895443be1f5fd Mon Sep 17 00:00:00 2001 From: La Tasha Pollard Date: Thu, 4 May 2023 17:27:02 -0500 Subject: [PATCH 28/28] moves original planet list to seed py file --- app/routes.py | 55 --------------------------------------------------- app/seed.py | 11 +++++++++++ 2 files changed, 11 insertions(+), 55 deletions(-) create mode 100644 app/seed.py diff --git a/app/routes.py b/app/routes.py index 16de52bf3..5c30c4003 100644 --- a/app/routes.py +++ b/app/routes.py @@ -62,58 +62,3 @@ def delete_planet(planet_id): db.session.commit() return make_response(jsonify(f"Planet {planet_to_delete.name} was deleted.")) - - - - -# Define a Planet class with the attributes id, name, -# and description, and one additional attribute - -# class Planet: -# def __init__(self, id, name, description, solar_day): -# self.id = id -# self.name = name -# self.description = description -# self.solar_day = solar_day - -# def to_dict(self): -# return dict( -# id=self.id, -# name=self.name, -# description=self.description, -# solar_day=self.solar_day -# ) - -# planets = [ -# Planet(1, "Mercury", "smallest planet", 176.0), -# Planet(2, "Venus", "planet of love", 243.0), -# Planet(3, "Earth", "home planet", 1.0), -# Planet(4, "Mars", "red planet", 1.25), -# Planet(5, "Jupiter", "largest planet", 0.42), -# Planet(6, "Saturn", "ring planet", .45), -# Planet(7, "Uranus", "coldest planet", .71), -# Planet(8, "Neptune", "not visible to the naked eye", .67), -# Planet(9, "Pluto", "unqualified planet", 6.375) -# ] - - # Wave 2 - # read one planet - # return 400 for invalid id - # return 404 for non existing planet - -# @planets_bp.route("", methods=["GET"]) -# def handle_planets(): -# results = [planet.to_dict() for planet in planets] - -# return jsonify(results) - -# @planets_bp.route("/", methods=["GET"]) -# def handle_planet(planet_id): -# planet = validate_planet(planet_id) - -# return planet.to_dict() - - - - - \ No newline at end of file diff --git a/app/seed.py b/app/seed.py new file mode 100644 index 000000000..f81bb5940 --- /dev/null +++ b/app/seed.py @@ -0,0 +1,11 @@ +# planets = [ +# Planet(1, "Mercury", "smallest planet", 176.0), +# Planet(2, "Venus", "planet of love", 243.0), +# Planet(3, "Earth", "home planet", 1.0), +# Planet(4, "Mars", "red planet", 1.25), +# Planet(5, "Jupiter", "largest planet", 0.42), +# Planet(6, "Saturn", "ring planet", .45), +# Planet(7, "Uranus", "coldest planet", .71), +# Planet(8, "Neptune", "not visible to the naked eye", .67), +# Planet(9, "Pluto", "unqualified planet", 6.375) +# ] \ No newline at end of file