From d13796cefd967949a896a820ddfe8bbb5e811fe9 Mon Sep 17 00:00:00 2001 From: Tia Willis Date: Tue, 25 Oct 2022 14:11:31 -0600 Subject: [PATCH 01/11] Wave 1 and 2 completion. --- app/__init__.py | 7 +++++++ app/routes.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..d2ff7d1da 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,4 +4,11 @@ def create_app(test_config=None): app = Flask(__name__) + from .routes import planets_bp + app.register_blueprint(planets_bp) + return app + +# commands to remember: +# flask run -h localhost -p 3000 +# FLASK_ENV=development flask run \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..6cf0af76c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,52 @@ -from flask import Blueprint +from unicodedata import name +from flask import Blueprint, jsonify, abort, make_response +class Planet: + def __init__(self, id, name, description, size): + self.id = id + self.name = name + self.description = description + self.size = size + +planets = [ + Planet(1, "Earth", "3rd planet from sun, inhabits life", "7,917.5 mi"), + Planet(2, "Mars", "4th planet from the sun, cold dusty desert", "4,212.3 mi"), + Planet(3, "Saturn", "2nd largest planet, Adorned with thousands of beautiful ringlets", "72,367 mi") +] + +planets_bp = Blueprint("planets", __name__, url_prefix="/planets") + +@planets_bp.route("",methods=["GET"]) +def handle_planets_data(): + planets_response = [] + for planet in planets: + planets_response.append({ + "id": planet.id, + "name": planet.name, + "description": planet.description, + "size": planet.size + }), 200 + return jsonify(planets_response) + +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)) + +@planets_bp.route("/", methods=["GET"]) +def handle_planet(planet_id): + planet = validate_planet(planet_id) + + return jsonify({ + "id": planet.id, + "name": planet.name, + "description": planet.description, + "size": planet.size + }), 200 \ No newline at end of file From 84980017c5ca026fda78cf71d1e16e1442c5d7e8 Mon Sep 17 00:00:00 2001 From: Tia Willis Date: Tue, 1 Nov 2022 00:33:05 -0600 Subject: [PATCH 02/11] wave 3 complete --- app/__init__.py | 14 +- app/models/__init__.py | 0 app/models/planet.py | 9 ++ app/routes.py | 125 +++++++++++------- migrations/README | 1 + migrations/alembic.ini | 45 +++++++ migrations/env.py | 96 ++++++++++++++ migrations/script.py.mako | 24 ++++ .../32a2816ba4ad_adds_planet_model.py | 34 +++++ project-directions/database.sql | 8 ++ 10 files changed, 307 insertions(+), 49 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/32a2816ba4ad_adds_planet_model.py create mode 100644 project-directions/database.sql diff --git a/app/__init__.py b/app/__init__.py index d2ff7d1da..7e9181a55 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,14 +1,26 @@ from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate +db = SQLAlchemy() +migrate = Migrate() def create_app(test_config=None): app = Flask(__name__) + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' + + db.init_app(app) + migrate.init_app(app, db) + from .routes import planets_bp app.register_blueprint(planets_bp) + from app.models.planet import Planet + return app - + # commands to remember: # flask run -h localhost -p 3000 # FLASK_ENV=development flask run \ 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..6b2ce2f44 --- /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) + title = db.Column(db.String) + description = db.Column(db.String) + size = db.Column(db.String) \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 6cf0af76c..9cdec5815 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,52 +1,81 @@ -from unicodedata import name -from flask import Blueprint, jsonify, abort, make_response - -class Planet: - def __init__(self, id, name, description, size): - self.id = id - self.name = name - self.description = description - self.size = size - -planets = [ - Planet(1, "Earth", "3rd planet from sun, inhabits life", "7,917.5 mi"), - Planet(2, "Mars", "4th planet from the sun, cold dusty desert", "4,212.3 mi"), - Planet(3, "Saturn", "2nd largest planet, Adorned with thousands of beautiful ringlets", "72,367 mi") -] +from app import db +from app.models.planet import Planet +# from unicodedata import name +from flask import Blueprint, jsonify, abort, make_response, request planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -@planets_bp.route("",methods=["GET"]) -def handle_planets_data(): - planets_response = [] - for planet in planets: - planets_response.append({ - "id": planet.id, - "name": planet.name, - "description": planet.description, - "size": planet.size - }), 200 - return jsonify(planets_response) - -def validate_planet(planet_id): - try: - planet_id = int(planet_id) - except: - abort(make_response({"message": f"Planet {planet_id} invalid"}, 400)) +@planets_bp.route("", methods=["GET", "POST"]) +def handle_planets(): + if request.method == "GET": + planets = Planet.query.all() + planets_response = [] + for planet in planets: + planets_response.append({ + "id": planet.id, + "title": planet.title, + "description": planet.description, + "size": planet.description + }) + return jsonify(planets_response) + elif request.method == "POST": + request_body = request.get_json() + print (request_body) + new_planet = Planet(title=request_body["title"], + description=request_body["description"], + size=request_body["size"]) + + db.session.add(new_planet) + db.session.commit() + + return make_response(f"Planet {new_planet.title} successfully created", 201) + +# class Planet: +# def __init__(self, id, name, description, size): +# self.id = id +# self.name = name +# self.description = description +# self.size = size + +# planets = [ +# Planet(1, "Earth", "inhabits life", "7,917.5 mi"), +# Planet(2, "Mars", "cold dusty desert", "4,212.3 mi"), +# Planet(3, "Saturn", "Adorned with ringlets", "72,367 mi") +# ] + +# planets_bp = Blueprint("planets", __name__, url_prefix="/planets") + +# @planets_bp.route("",methods=["GET"]) +# def handle_planets_data(): +# planets_response = [] +# for planet in planets: +# planets_response.append({ +# "id": planet.id, +# "name": planet.name, +# "description": planet.description, +# "size": planet.size +# }), 200 +# return jsonify(planets_response) + +# 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)) - -@planets_bp.route("/", methods=["GET"]) -def handle_planet(planet_id): - planet = validate_planet(planet_id) - - return jsonify({ - "id": planet.id, - "name": planet.name, - "description": planet.description, - "size": planet.size - }), 200 \ No newline at end of file +# for planet in planets: +# if planet.id == planet_id: +# return planet + +# abort(make_response({"message":f"planet {planet_id} not found"}, 404)) + +# @planets_bp.route("/", methods=["GET"]) +# def handle_planet(planet_id): +# planet = validate_planet(planet_id) + +# return jsonify({ +# "id": planet.id, +# "name": planet.name, +# "description": planet.description, +# "size": planet.size +# }), 200 \ 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/32a2816ba4ad_adds_planet_model.py b/migrations/versions/32a2816ba4ad_adds_planet_model.py new file mode 100644 index 000000000..a250d3c97 --- /dev/null +++ b/migrations/versions/32a2816ba4ad_adds_planet_model.py @@ -0,0 +1,34 @@ +"""adds Planet model + +Revision ID: 32a2816ba4ad +Revises: +Create Date: 2022-10-29 19:35:26.964099 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '32a2816ba4ad' +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('title', sa.String(), nullable=True), + sa.Column('description', sa.String(), nullable=True), + sa.Column('size', sa.String(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('planet') + # ### end Alembic commands ### diff --git a/project-directions/database.sql b/project-directions/database.sql new file mode 100644 index 000000000..898408a1b --- /dev/null +++ b/project-directions/database.sql @@ -0,0 +1,8 @@ +CREATE DATABASE solar_system_development; +CREATE TABLE Planet ( + id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + name TEXT, + description TEXT, + size VARCHAR(20) + +); From eae7b496c14479089b36edf61d79f570f123886c Mon Sep 17 00:00:00 2001 From: Tia Willis Date: Tue, 1 Nov 2022 20:48:31 -0600 Subject: [PATCH 03/11] wave 4 complete, "title" error fixed throughout code. --- app/models/planet.py | 2 +- app/routes.py | 112 ++++++++++++++++++++++++++----------------- 2 files changed, 70 insertions(+), 44 deletions(-) diff --git a/app/models/planet.py b/app/models/planet.py index 6b2ce2f44..c5410d2e9 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -4,6 +4,6 @@ class Planet(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) - title = db.Column(db.String) + name = db.Column(db.String) description = db.Column(db.String) size = db.Column(db.String) \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 9cdec5815..e933241b6 100644 --- a/app/routes.py +++ b/app/routes.py @@ -5,30 +5,46 @@ planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -@planets_bp.route("", methods=["GET", "POST"]) -def handle_planets(): - if request.method == "GET": - planets = Planet.query.all() - planets_response = [] - for planet in planets: - planets_response.append({ - "id": planet.id, - "title": planet.title, - "description": planet.description, - "size": planet.description - }) - return jsonify(planets_response) - elif request.method == "POST": - request_body = request.get_json() - print (request_body) - new_planet = Planet(title=request_body["title"], - description=request_body["description"], - size=request_body["size"]) - - db.session.add(new_planet) - db.session.commit() - - return make_response(f"Planet {new_planet.title} successfully created", 201) +#helper function +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 + +#route functions +@planets_bp.route("", methods=["GET"]) +def read_all_planets(): + planets = Planet.query.all() + planets_response = [] + for planet in planets: + planets_response.append({ + "id": planet.id, + "name": planet.name, + "description": planet.description, + "size": planet.description + }), 200 + return jsonify(planets_response) + +@planets_bp.route("", methods=["POST"]) +def create_planet(): + request_body = request.get_json() + print (request_body) + new_planet = Planet(name=request_body["name"], + description=request_body["description"], + size=request_body["size"]) + + db.session.add(new_planet) + db.session.commit() + + return make_response(f"Planet {new_planet.name} successfully created", 201) # class Planet: # def __init__(self, id, name, description, size): @@ -57,25 +73,35 @@ def handle_planets(): # }), 200 # return jsonify(planets_response) -# 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 +@planets_bp.route("/", methods=["GET"]) +def read_one_planet(planet_id): + planet = validate_planet(planet_id) + return jsonify({ + "id": planet.id, + "name": planet.name, + "description": planet.description, + "size": planet.size + }), 200 + +@planets_bp.route("/", methods=["PUT"]) +def update_planet(planet_id): + planet = validate_planet(planet_id) + + request_body = request.get_json() + + planet.name = request_body["name"] + planet.description = request_body["description"] + planet.size = request_body["size"] + + db.session.commit() + + return make_response(f"Planet #{planet.id} successfully updated", 200) -# abort(make_response({"message":f"planet {planet_id} not found"}, 404)) +@planets_bp.route("/", methods=["DELETE"]) +def delete_planet(planet_id): + planet = validate_planet(planet_id) -# @planets_bp.route("/", methods=["GET"]) -# def handle_planet(planet_id): -# planet = validate_planet(planet_id) + db.session.delete(planet) + db.session.commit() -# return jsonify({ -# "id": planet.id, -# "name": planet.name, -# "description": planet.description, -# "size": planet.size -# }), 200 \ No newline at end of file + return make_response(f"Planet #{planet.id} successfully deleted", 200) \ No newline at end of file From 69ad8873cdf52d69bfcec4b9029751cf57845c3b Mon Sep 17 00:00:00 2001 From: samiya Date: Wed, 2 Nov 2022 10:54:09 -0400 Subject: [PATCH 04/11] I've commited my files. --- app/__init__.py | 14 +++- app/models/__init__.py | 0 app/models/planet.py | 8 +++ app/routes.py | 95 +++++++++++++++++---------- migrations/README | 1 + migrations/alembic.ini | 45 +++++++++++++ migrations/env.py | 96 ++++++++++++++++++++++++++++ migrations/script.py.mako | 24 +++++++ migrations/versions/88f852796d30_.py | 34 ++++++++++ 9 files changed, 280 insertions(+), 37 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/88f852796d30_.py diff --git a/app/__init__.py b/app/__init__.py index d2ff7d1da..31023e9ee 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,12 +1,22 @@ 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__) - - from .routes import planets_bp + app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False + app.config['SQLALCHEMY_DATABASE_URI']='postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' + db.init_app(app) + migrate.init_app(app,db) + from .models.planet import Planet + + from.routes import planets_bp app.register_blueprint(planets_bp) + return app # commands to remember: 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..953e74b7e --- /dev/null +++ b/app/models/planet.py @@ -0,0 +1,8 @@ +from app import db +class Planet(db.Model): + id=db.Column(db.Integer,primary_key=True, autoincrement=True) + name=db.Column(db.String) + description=db.Column(db.String) + size=db.Column(db.Integer) + + diff --git a/app/routes.py b/app/routes.py index 6cf0af76c..eaea2b738 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,34 +1,59 @@ +from hashlib import new from unicodedata import name -from flask import Blueprint, jsonify, abort, make_response +from flask import Blueprint, jsonify, abort, make_response, request +from app import db +from app.models.planet import Planet -class Planet: - def __init__(self, id, name, description, size): - self.id = id - self.name = name - self.description = description - self.size = size +planets_bp = Blueprint("planets", __name__, url_prefix="/planets") +#id=request_body["id"], +@planets_bp.route("",methods=["GET","POST"]) +def handle_planets(): + if request.method=="GET": + planets=Planet.query.all() + planets_response=[] + for planet in planets: + planets_response.append({"id":planet.id, + "name":planet.name, + "description":planet.description, + "size":planet.size}) + return jsonify(planets_response) + elif request.method=="POST": + request_body=request.get_json() + print(request_body) + new_planet=Planet(name= request_body["name"], + description=request_body ["description"], + size=request_body["size"]) + db.session.add(new_planet) + db.session.commit() + return make_response( + f"Planet{new_planet} created",201) -planets = [ - Planet(1, "Earth", "3rd planet from sun, inhabits life", "7,917.5 mi"), - Planet(2, "Mars", "4th planet from the sun, cold dusty desert", "4,212.3 mi"), - Planet(3, "Saturn", "2nd largest planet, Adorned with thousands of beautiful ringlets", "72,367 mi") -] +#class Planet: + #def __init__(self, id, name, description, size): + #self.id = id + #self.name = name + #self.description = description + #self.size = size -planets_bp = Blueprint("planets", __name__, url_prefix="/planets") +#planets = [ + # Planet(1, "Earth", "3rd planet from sun, inhabits life", "7,917.5 mi"), + # Planet(2, "Mars", "4th planet from the sun, cold dusty desert", "4,212.3 mi"), + #Planet(3, "Saturn", "2nd largest planet, Adorned with thousands of beautiful ringlets", "72,367 mi") +#] -@planets_bp.route("",methods=["GET"]) -def handle_planets_data(): - planets_response = [] - for planet in planets: - planets_response.append({ - "id": planet.id, - "name": planet.name, - "description": planet.description, - "size": planet.size - }), 200 - return jsonify(planets_response) - -def validate_planet(planet_id): +#@planets_bp.route("",methods=["GET"]) +#def handle_planets_data(): + #planets_response = [] + # for planet in planets: + # planets_response.append({ + # "id": planet.id, + # "name": planet.name, + # "description": planet.description, + # "size": planet.size + # }), 200 + #return jsonify(planets_response) + +#def validate_planet(planet_id): try: planet_id = int(planet_id) except: @@ -40,13 +65,13 @@ def validate_planet(planet_id): abort(make_response({"message":f"planet {planet_id} not found"}, 404)) -@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 jsonify({ - "id": planet.id, - "name": planet.name, - "description": planet.description, - "size": planet.size - }), 200 \ No newline at end of file + #return jsonify({ + # "id": planet.id, + # "name": planet.name, + #"description": planet.description, + # "size": planet.size + # }), 200 \ 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/88f852796d30_.py b/migrations/versions/88f852796d30_.py new file mode 100644 index 000000000..ffdb9a04b --- /dev/null +++ b/migrations/versions/88f852796d30_.py @@ -0,0 +1,34 @@ +"""empty message + +Revision ID: 88f852796d30 +Revises: +Create Date: 2022-11-01 14:22:08.041140 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '88f852796d30' +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('size', sa.Integer(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('planet') + # ### end Alembic commands ### From 8a6c815846f821df830ee4feae55ba850f7b5c05 Mon Sep 17 00:00:00 2001 From: Tia Willis Date: Wed, 2 Nov 2022 12:29:43 -0600 Subject: [PATCH 05/11] nothing major --- app/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index e933241b6..cd8430f95 100644 --- a/app/routes.py +++ b/app/routes.py @@ -29,7 +29,7 @@ def read_all_planets(): "id": planet.id, "name": planet.name, "description": planet.description, - "size": planet.description + "size": planet.size }), 200 return jsonify(planets_response) From c88a4b5d239870a75e8e0d01cfcbfdd0b0cdaf94 Mon Sep 17 00:00:00 2001 From: samiya Date: Wed, 2 Nov 2022 14:31:43 -0400 Subject: [PATCH 06/11] 'we've completed query params." --- app/models/planet.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/planet.py b/app/models/planet.py index c5410d2e9..41b29ffef 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -6,4 +6,12 @@ class Planet(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) description = db.Column(db.String) - size = db.Column(db.String) \ No newline at end of file + size = db.Column(db.String) + + #def dict_planet(self): + #return{ + #"id": self.id, + #"name": self.name, + #"description": self.description, + #"size": self.size + #} \ No newline at end of file From 603d17552f808f0b6e7040a777c925b2f8e9e096 Mon Sep 17 00:00:00 2001 From: samiya Date: Wed, 2 Nov 2022 14:32:43 -0400 Subject: [PATCH 07/11] we've completed the query params --- app/routes.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/app/routes.py b/app/routes.py index e933241b6..14fa7e276 100644 --- a/app/routes.py +++ b/app/routes.py @@ -22,16 +22,34 @@ def validate_planet(planet_id): #route functions @planets_bp.route("", methods=["GET"]) def read_all_planets(): - planets = Planet.query.all() + #planets = Planet.query.all() planets_response = [] + + name_param=request.args.get("name") + + description_param=request.args.get("description") + + size_param=request.args.get("size") + + if name_param: + planets=Planet.query.filter_by(name=name_param) + elif description_param: + planets=Planet.query.filter_by(description=description_param) + elif size_param: + planets=Planet.query.filter_by(size=size_param) + else: + planets=Planet.query.all() + + for planet in planets: planets_response.append({ "id": planet.id, "name": planet.name, "description": planet.description, - "size": planet.description + "size": planet.size }), 200 - return jsonify(planets_response) + #planets_response.append(planet.dict_planet()) + return jsonify(planets_response),200 @planets_bp.route("", methods=["POST"]) def create_planet(): From e90256e251c13b6a3cb50a2de14b3c1740fcf2bc Mon Sep 17 00:00:00 2001 From: samiya Date: Wed, 2 Nov 2022 14:34:48 -0400 Subject: [PATCH 08/11] did it --- app/routes.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 14fa7e276..ebfd73ac6 100644 --- a/app/routes.py +++ b/app/routes.py @@ -26,9 +26,7 @@ def read_all_planets(): planets_response = [] name_param=request.args.get("name") - description_param=request.args.get("description") - size_param=request.args.get("size") if name_param: From 54a2a3177b55659c5e41543b7bbdeabf7343eab7 Mon Sep 17 00:00:00 2001 From: Tia Willis Date: Thu, 3 Nov 2022 10:36:03 -0600 Subject: [PATCH 09/11] push and pull --- app/routes.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/routes.py b/app/routes.py index 14fa7e276..cd71cf1b8 100644 --- a/app/routes.py +++ b/app/routes.py @@ -26,9 +26,7 @@ def read_all_planets(): planets_response = [] name_param=request.args.get("name") - description_param=request.args.get("description") - size_param=request.args.get("size") if name_param: @@ -49,7 +47,7 @@ def read_all_planets(): "size": planet.size }), 200 #planets_response.append(planet.dict_planet()) - return jsonify(planets_response),200 + return jsonify(planets_response),200 @planets_bp.route("", methods=["POST"]) def create_planet(): From 22f547f294151dd1be385a73103147a2bc333dd2 Mon Sep 17 00:00:00 2001 From: Tia Willis Date: Thu, 3 Nov 2022 19:08:09 -0600 Subject: [PATCH 10/11] wave 6 pt1 and partial pt2 completed --- app/__init__.py | 29 +++++++++++++++++++++-------- tests/__init__.py | 0 tests/conftest.py | 41 +++++++++++++++++++++++++++++++++++++++++ tests/test_routes.py | 22 ++++++++++++++++++++++ 4 files changed, 84 insertions(+), 8 deletions(-) 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 eea26bbea..b34b512a1 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,24 +1,37 @@ 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() - -db = SQLAlchemy() -migrate = Migrate() +load_dotenv() def create_app(test_config=None): app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False - app.config['SQLALCHEMY_DATABASE_URI']='postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' + + if not test_config: + app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get( + "SQLALCHEMY_DATABASE_URI") + #app.config['SQLALCHEMY_DATABASE_URI']='postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' + + else: + app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( + "SQLALCHEMY_TEST_DATABASE_URI") + + + + db.init_app(app) migrate.init_app(app,db) - from .models.planet import Planet - + + ###QUESTION: Do we need this line? its in learn but im unsure why we need it? + #from app.models.planet import Planet + from.routes import planets_bp app.register_blueprint(planets_bp) - from app.models.planet import Planet - return app 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..b325557fd --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,41 @@ +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() + +from app.models.planet import Planet +# ... + +@pytest.fixture +def two_saved_planets(app): + # Arrange + earth_planet = Planet(name="Earth", + description="Inhabits life", + size= "7,917.5 mi" ) + saturn_planet = Planet(title="Saturn", + description="Adorned with ringlets 0o0o0o", + size="72,367 mi") + + db.session.add_all([earth_planet, saturn_planet]) + db.session.commit() \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 000000000..11ca2df5b --- /dev/null +++ b/tests/test_routes.py @@ -0,0 +1,22 @@ +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 == [] + +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": "", + "name": "", + "description": "", + "size": "" + } \ No newline at end of file From 3267f271add650766c0715e71031a5c00008ec15 Mon Sep 17 00:00:00 2001 From: Tia Willis Date: Thu, 3 Nov 2022 20:31:20 -0600 Subject: [PATCH 11/11] complete project --- app/routes.py | 6 +++--- tests/conftest.py | 2 +- tests/test_routes.py | 23 ++++++++++++++++++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/routes.py b/app/routes.py index cd71cf1b8..e18ac891c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -60,7 +60,7 @@ def create_planet(): db.session.add(new_planet) db.session.commit() - return make_response(f"Planet {new_planet.name} successfully created", 201) + return make_response(jsonify(f"Planet {new_planet.name} successfully created"), 201) # class Planet: # def __init__(self, id, name, description, size): @@ -111,7 +111,7 @@ def update_planet(planet_id): db.session.commit() - return make_response(f"Planet #{planet.id} successfully updated", 200) + return make_response(jsonify(f"Planet #{planet.id} successfully updated"), 200) @planets_bp.route("/", methods=["DELETE"]) def delete_planet(planet_id): @@ -120,4 +120,4 @@ def delete_planet(planet_id): db.session.delete(planet) db.session.commit() - return make_response(f"Planet #{planet.id} successfully deleted", 200) \ No newline at end of file + return make_response(jsonify(f"Planet #{planet.id} successfully deleted"), 200) \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index b325557fd..2f0848f18 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -33,7 +33,7 @@ def two_saved_planets(app): earth_planet = Planet(name="Earth", description="Inhabits life", size= "7,917.5 mi" ) - saturn_planet = Planet(title="Saturn", + saturn_planet = Planet(name="Saturn", description="Adorned with ringlets 0o0o0o", size="72,367 mi") diff --git a/tests/test_routes.py b/tests/test_routes.py index 11ca2df5b..dc0dc35fe 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -15,8 +15,21 @@ def test_get_one_planet(client, two_saved_planets): # Assert assert response.status_code == 200 assert response_body == { - "id": "", - "name": "", - "description": "", - "size": "" - } \ No newline at end of file + "id": 1, + "name": "Earth", + "description": "Inhabits life", + "size": "7,917.5 mi" + } + +def test_create_one_planet(client): + # Act + response = client.post("/planets", json={ + "name": "New Planet", + "description": "The Best!", + "size": "10000 happy mi" + }) + response_body = response.get_json() + + # Assert + assert response.status_code == 201 + assert response_body == "Planet New Planet successfully created" \ No newline at end of file