From bdc7465d68cfcf9bd05deb9f25e035097704ddb1 Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Fri, 7 May 2021 09:50:17 -0700 Subject: [PATCH 01/16] added no and one saved task. Get task and task not found --- app/__init__.py | 5 ++ app/models/task.py | 8 ++- app/routes.py | 34 ++++++++++ app/task-list-api.code-workspace | 8 +++ migrations/README | 1 + migrations/alembic.ini | 45 +++++++++++++ migrations/env.py | 96 ++++++++++++++++++++++++++++ migrations/script.py.mako | 24 +++++++ migrations/versions/7f45ce564a8c_.py | 39 +++++++++++ migrations/versions/84ac43910dbf_.py | 30 +++++++++ migrations/versions/af1f5482504b_.py | 28 ++++++++ 11 files changed, 317 insertions(+), 1 deletion(-) create mode 100644 app/task-list-api.code-workspace 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/7f45ce564a8c_.py create mode 100644 migrations/versions/84ac43910dbf_.py create mode 100644 migrations/versions/af1f5482504b_.py diff --git a/app/__init__.py b/app/__init__.py index 2764c4cc8..cb5d1ac8e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -29,6 +29,11 @@ def create_app(test_config=None): db.init_app(app) migrate.init_app(app, db) + + # Register Blueprints here + from .routes import task_list_bp + app.register_blueprint(task_list_bp) + return app diff --git a/app/models/task.py b/app/models/task.py index 39c89cd16..da5827dac 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -3,4 +3,10 @@ class Task(db.Model): - task_id = db.Column(db.Integer, primary_key=True) + id = db.Column(db.Integer, primary_key=True) + title = db.Column(db.String) + description = db.Column(db.String) + completed_at = db.Column(db.DateTime, nullable = True) + is_complete = db.Column(db.Boolean, default = False) + + diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..5f6298426 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,36 @@ from flask import Blueprint +from app import db +from app.models.task import Task +from flask import request, Blueprint, make_response, jsonify + +task_list_bp = Blueprint("task_list", __name__, url_prefix='/tasks') +@task_list_bp.route('', methods=['GET', 'POST']) +def handle_tasks(): + if request.method == 'GET': + #return full list of tasks + tasks = Task.query.all() + task_response = [] + for task in tasks: + task_response.append({ + 'id': task.id, + 'title': task.title, + 'description': task.description, + 'is_complete': task.is_complete + }) + return jsonify(task_response) + +@task_list_bp.route('/', methods=['GET']) +def handle_task(task_id): # same name as parameter route + task = Task.query.get(task_id) + if not task: + return "", 404 + return({ + 'task':{ + 'id': task.id, + 'title': task.title, + 'description': task.description, + 'is_complete': task.is_complete + } + }) + diff --git a/app/task-list-api.code-workspace b/app/task-list-api.code-workspace new file mode 100644 index 000000000..bab1b7f61 --- /dev/null +++ b/app/task-list-api.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": ".." + } + ], + "settings": {} +} \ 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/7f45ce564a8c_.py b/migrations/versions/7f45ce564a8c_.py new file mode 100644 index 000000000..fb8b04f36 --- /dev/null +++ b/migrations/versions/7f45ce564a8c_.py @@ -0,0 +1,39 @@ +"""empty message + +Revision ID: 7f45ce564a8c +Revises: +Create Date: 2021-05-05 19:55:02.318438 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '7f45ce564a8c' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('goal', + sa.Column('goal_id', sa.Integer(), nullable=False), + sa.PrimaryKeyConstraint('goal_id') + ) + op.create_table('task', + sa.Column('task_id', sa.Integer(), nullable=False), + sa.Column('title', sa.String(), nullable=True), + sa.Column('description', sa.String(), nullable=True), + sa.Column('completed_at', sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint('task_id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('task') + op.drop_table('goal') + # ### end Alembic commands ### diff --git a/migrations/versions/84ac43910dbf_.py b/migrations/versions/84ac43910dbf_.py new file mode 100644 index 000000000..3934666ee --- /dev/null +++ b/migrations/versions/84ac43910dbf_.py @@ -0,0 +1,30 @@ +"""empty message + +Revision ID: 84ac43910dbf +Revises: af1f5482504b +Create Date: 2021-05-06 18:26:16.610407 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '84ac43910dbf' +down_revision = 'af1f5482504b' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('task', sa.Column('id', sa.Integer(), nullable=False)) + op.drop_column('task', 'task_id') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('task', sa.Column('task_id', sa.INTEGER(), autoincrement=True, nullable=False)) + op.drop_column('task', 'id') + # ### end Alembic commands ### diff --git a/migrations/versions/af1f5482504b_.py b/migrations/versions/af1f5482504b_.py new file mode 100644 index 000000000..76cd28715 --- /dev/null +++ b/migrations/versions/af1f5482504b_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: af1f5482504b +Revises: 7f45ce564a8c +Create Date: 2021-05-06 18:18:16.649575 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'af1f5482504b' +down_revision = '7f45ce564a8c' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('task', sa.Column('is_complete', sa.Boolean(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('task', 'is_complete') + # ### end Alembic commands ### From 79280af39a571b81fbc2c14769b1a22730d87faf Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Mon, 10 May 2021 14:53:10 -0700 Subject: [PATCH 02/16] updated id to task_id --- app/models/task.py | 4 ++-- app/routes.py | 23 ++++++++++++++++++++--- app/task-list-api.code-workspace | 3 +++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/models/task.py b/app/models/task.py index da5827dac..400a6a653 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -3,10 +3,10 @@ class Task(db.Model): - id = db.Column(db.Integer, primary_key=True) + task_id = db.Column(db.Integer, autoincrement=True, primary_key=True) title = db.Column(db.String) description = db.Column(db.String) completed_at = db.Column(db.DateTime, nullable = True) - is_complete = db.Column(db.Boolean, default = False) + # is_complete = db.Column(db.Boolean, default = False) diff --git a/app/routes.py b/app/routes.py index 5f6298426..653af102a 100644 --- a/app/routes.py +++ b/app/routes.py @@ -12,13 +12,30 @@ def handle_tasks(): task_response = [] for task in tasks: task_response.append({ - 'id': task.id, + 'task_id': task.task_id, 'title': task.title, 'description': task.description, - 'is_complete': task.is_complete + #'is_complete': task.is_complete }) return jsonify(task_response) - + elif request.method == 'POST': + request_body = request.get_json() + new_task = Task(title = request_body['title'], + description = request_body['description'], + + ) + db.session.add(new_task) + db.session.commit() + return{ + 'task':{ + 'task_id': new_task.task_id, + 'title': new_task.title, + 'description': new_task.description, + # 'is_complete': new_task.is_complete + } + },201 + + @task_list_bp.route('/', methods=['GET']) def handle_task(task_id): # same name as parameter route task = Task.query.get(task_id) diff --git a/app/task-list-api.code-workspace b/app/task-list-api.code-workspace index bab1b7f61..c7436621b 100644 --- a/app/task-list-api.code-workspace +++ b/app/task-list-api.code-workspace @@ -2,6 +2,9 @@ "folders": [ { "path": ".." + }, + { + "path": "../../api/solar-system-api" } ], "settings": {} From 1f2f443fb10eb025deee5983d84aa7e18e80c634 Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Tue, 11 May 2021 09:24:56 -0700 Subject: [PATCH 03/16] changed get complete logic in routes and id to task id in task.py --- app/models/task.py | 2 +- app/routes.py | 13 ++++++----- migrations/versions/a50e57a3eff0_.py | 28 +++++++++++++++++++++++ migrations/versions/f3d84e20bc58_.py | 34 ++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 migrations/versions/a50e57a3eff0_.py create mode 100644 migrations/versions/f3d84e20bc58_.py diff --git a/app/models/task.py b/app/models/task.py index 400a6a653..f5f464a95 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -7,6 +7,6 @@ class Task(db.Model): title = db.Column(db.String) description = db.Column(db.String) completed_at = db.Column(db.DateTime, nullable = True) - # is_complete = db.Column(db.Boolean, default = False) + diff --git a/app/routes.py b/app/routes.py index 653af102a..6219b8184 100644 --- a/app/routes.py +++ b/app/routes.py @@ -12,15 +12,16 @@ def handle_tasks(): task_response = [] for task in tasks: task_response.append({ - 'task_id': task.task_id, + 'id': task.task_id, 'title': task.title, 'description': task.description, - #'is_complete': task.is_complete + 'is_complete': task.completed_at != None }) return jsonify(task_response) elif request.method == 'POST': request_body = request.get_json() - new_task = Task(title = request_body['title'], + new_task = Task( + title = request_body['title'], description = request_body['description'], ) @@ -28,7 +29,7 @@ def handle_tasks(): db.session.commit() return{ 'task':{ - 'task_id': new_task.task_id, + 'id': new_task.task_id, 'title': new_task.title, 'description': new_task.description, # 'is_complete': new_task.is_complete @@ -43,10 +44,10 @@ def handle_task(task_id): # same name as parameter route return "", 404 return({ 'task':{ - 'id': task.id, + 'id': task.task_id, 'title': task.title, 'description': task.description, - 'is_complete': task.is_complete + 'is_complete': task.completed_at != None } }) diff --git a/migrations/versions/a50e57a3eff0_.py b/migrations/versions/a50e57a3eff0_.py new file mode 100644 index 000000000..2e4eabbf3 --- /dev/null +++ b/migrations/versions/a50e57a3eff0_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: a50e57a3eff0 +Revises: f3d84e20bc58 +Create Date: 2021-05-10 15:49:17.182962 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'a50e57a3eff0' +down_revision = 'f3d84e20bc58' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('task', sa.Column('is_complete', sa.Boolean(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('task', 'is_complete') + # ### end Alembic commands ### diff --git a/migrations/versions/f3d84e20bc58_.py b/migrations/versions/f3d84e20bc58_.py new file mode 100644 index 000000000..5f50f2482 --- /dev/null +++ b/migrations/versions/f3d84e20bc58_.py @@ -0,0 +1,34 @@ +"""empty message + +Revision ID: f3d84e20bc58 +Revises: 84ac43910dbf +Create Date: 2021-05-09 14:38:07.209685 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f3d84e20bc58' +down_revision = '84ac43910dbf' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('task', + sa.Column('task_id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('title', sa.String(), nullable=True), + sa.Column('description', sa.String(), nullable=True), + sa.Column('completed_at', sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint('task_id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('task') + # ### end Alembic commands ### From 4166b812f002971ae0aaa2c23062f3a51979f6e4 Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Wed, 12 May 2021 12:50:58 -0700 Subject: [PATCH 04/16] added sorted by asc and desc title funtionality --- app/models/task.py | 10 ++++++++ app/routes.py | 58 ++++++++++++++++++++++++++++++++++--------- tests/test_wave_01.py | 6 +++-- 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/app/models/task.py b/app/models/task.py index f5f464a95..d7a3f0fe1 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -7,6 +7,16 @@ class Task(db.Model): title = db.Column(db.String) description = db.Column(db.String) completed_at = db.Column(db.DateTime, nullable = True) + + def serialize(self): + result = { + 'id': self.task_id, + 'title': self.title, + 'description': self.description, + 'is_complete': self.completed_at != None + } + return result + diff --git a/app/routes.py b/app/routes.py index 6219b8184..9e6c2a044 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,3 +1,4 @@ +from tests.test_wave_01 import test_create_task_must_contain_description from flask import Blueprint from app import db from app.models.task import Task @@ -20,10 +21,22 @@ def handle_tasks(): return jsonify(task_response) elif request.method == 'POST': request_body = request.get_json() + if "title" not in request_body: + return { + "details": "Invalid data" + }, 400 + elif "description" not in request_body: + return { + "details": "Invalid data" + }, 400 + elif "is complete" not in request_body: + return { + "details": "Invalid data" + }, 400 + new_task = Task( title = request_body['title'], - description = request_body['description'], - + description = request_body['description'], ) db.session.add(new_task) db.session.commit() @@ -32,23 +45,44 @@ def handle_tasks(): 'id': new_task.task_id, 'title': new_task.title, 'description': new_task.description, - # 'is_complete': new_task.is_complete + 'is_complete': new_task.completed_at != None } },201 -@task_list_bp.route('/', methods=['GET']) +@task_list_bp.route('/', methods=['GET','PUT', 'DELETE']) def handle_task(task_id): # same name as parameter route task = Task.query.get(task_id) if not task: return "", 404 - return({ - 'task':{ - 'id': task.task_id, - 'title': task.title, - 'description': task.description, - 'is_complete': task.completed_at != None - } - }) + + if request.method == 'GET': + return({ + 'task': task.serialize() + }) + elif request.method == 'DELETE': + db.session.delete(task) + db.session.commit() + return({ + "details": f'Task {task_id} "{task.title}" successfully deleted' + },200) + + + elif request.method =='PUT': + request_body = request.get_json() + if 'title' in request_body: + task.title = request_body['title'] + if 'description' in request_body: + task.description = request_body['description'] + if 'completed_at' in request_body: + task.completed = request_body['completed_at'] + db.session.commit() + return({ + 'task': task.serialize() + },200) + + + + diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index fb943d9b3..55e43b366 100644 --- a/tests/test_wave_01.py +++ b/tests/test_wave_01.py @@ -59,11 +59,13 @@ def test_get_task_not_found(client): def test_create_task_with_none_completed_at(client): # Act - response = client.post("/tasks", json={ + + request_body = { "title": "A Brand New Task", "description": "Test Description", "completed_at": None - }) + } + response = client.post("/tasks", json = request_body) response_body = response.get_json() # Assert From 98c752461a7ea2c7983b7d94ba8628ab47c65fdd Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Fri, 14 May 2021 16:00:40 -0700 Subject: [PATCH 05/16] added string value for date time create task with valid completed at --- app/routes.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 9e6c2a044..085d7d579 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,8 +1,14 @@ +from flask_sqlalchemy.model import camel_to_snake_case from tests.test_wave_01 import test_create_task_must_contain_description from flask import Blueprint from app import db from app.models.task import Task from flask import request, Blueprint, make_response, jsonify +from datetime import datetime + + +def order_by_title(task_response): + return task_response["title"] task_list_bp = Blueprint("task_list", __name__, url_prefix='/tasks') @task_list_bp.route('', methods=['GET', 'POST']) @@ -18,6 +24,14 @@ def handle_tasks(): 'description': task.description, 'is_complete': task.completed_at != None }) + # allows access to keys - sort + arg = request.args + if "sort" in request.args: + if arg['sort'] == "asc": + task_response = sorted(task_response, key = order_by_title) + elif arg['sort'] == "desc": + task_response = sorted(task_response, key = order_by_title, reverse = True) + return jsonify(task_response) elif request.method == 'POST': request_body = request.get_json() @@ -29,14 +43,15 @@ def handle_tasks(): return { "details": "Invalid data" }, 400 - elif "is complete" not in request_body: + elif "completed_at" not in request_body: return { "details": "Invalid data" }, 400 - + stringify_format = "%a, %d %b %Y %H:%M:%S %Z" new_task = Task( title = request_body['title'], description = request_body['description'], + completed_at = datetime.strptime(request_body["completed_at"], stringify_format) ) db.session.add(new_task) db.session.commit() @@ -80,6 +95,41 @@ def handle_task(task_id): # same name as parameter route return({ 'task': task.serialize() },200) + +@task_list_bp.route('//mark_complete',methods = ['PATCH']) +def mark_complete_task(task_id): + task = Task.query.get(task_id) + if not task: + return "", 404 + task.completed_at = datetime.utcnow() + return{ + 'task':{ + 'id': task.task_id, + 'title': task.title, + 'description': task.description, + 'is_complete': task.completed_at != None + } + },200 +@task_list_bp.route('//mark_incomplete',methods = ['PATCH']) +def mark_incomplete_task(task_id): + task = Task.query.get(task_id) + if not task: + return "", 404 + task.completed_at = None + return{ + 'task':{ + 'id': task.task_id, + 'title': task.title, + 'description': task.description, + 'is_complete': task.completed_at != None + } + },200 + + + + + + From fd6f35a73fa1a57b69c0968178b5b184e1b8ba51 Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Tue, 18 May 2021 21:26:10 -0700 Subject: [PATCH 06/16] added GET and POST routes for goal --- app/__init__.py | 5 ++- app/models/goal.py | 14 +++++- app/models/task.py | 18 ++++++++ app/routes.py | 64 +++++++++++++++++++++++++--- migrations/versions/4b0b452824e1_.py | 30 +++++++++++++ migrations/versions/b05ae8147b20_.py | 30 +++++++++++++ 6 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 migrations/versions/4b0b452824e1_.py create mode 100644 migrations/versions/b05ae8147b20_.py diff --git a/app/__init__.py b/app/__init__.py index cb5d1ac8e..d5935c4a6 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -32,8 +32,11 @@ def create_app(test_config=None): # Register Blueprints here - from .routes import task_list_bp + from .routes import task_list_bp, goal_list_bp app.register_blueprint(task_list_bp) + app.register_blueprint(goal_list_bp) + + return app diff --git a/app/models/goal.py b/app/models/goal.py index 8cad278f8..619623c4b 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -3,4 +3,16 @@ class Goal(db.Model): - goal_id = db.Column(db.Integer, primary_key=True) + goal_id = db.Column(db.Integer, autoincrement=True, primary_key=True) + title = db.Column(db.String, nullable = False) + tasks = db.relationship( + 'Task', backref='goal', lazy=True + ) + # task_id = db.Column(db.Integer, autoincrement=True, primary_key=True) + + def serialize(self): + result = { + 'id': self.goal_id, + 'title': self.title + } + return result diff --git a/app/models/task.py b/app/models/task.py index d7a3f0fe1..a7bc7fe19 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -1,5 +1,7 @@ from flask import current_app +from requests.models import parse_header_links from app import db +import requests class Task(db.Model): @@ -7,6 +9,7 @@ class Task(db.Model): title = db.Column(db.String) description = db.Column(db.String) completed_at = db.Column(db.DateTime, nullable = True) + goal_id = db.Column(db.Integer,db.ForeignKey('goal.goal_id'),nullable = True) def serialize(self): result = { @@ -17,6 +20,21 @@ def serialize(self): } return result + def notify_slack(self): + message = f"Someone just completed the task {self.title}" + url = "https://slack.com/api/chat.postMessage" + params = { + + "channel":"task-notifications", + "text": message + } + headers = { + "Authorization":"Bearer xoxb-2054232917126-2062969942578-52P3S23cj940QFt3KlQnQzOW", + } + r = requests.post(url, data = params, headers = headers) + r.status_code + + diff --git a/app/routes.py b/app/routes.py index 085d7d579..e9dc2863d 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,8 +3,54 @@ from flask import Blueprint from app import db from app.models.task import Task +from app.models.goal import Goal from flask import request, Blueprint, make_response, jsonify from datetime import datetime +import requests + + + +goal_list_bp = Blueprint("goal_list", __name__, url_prefix = '/goals') + +@goal_list_bp.route('', methods = ['GET','POST']) +def handle_goals(): + if request.method == 'GET': + goals = Goal.query.all() + goal_response = [] + for goal in goals: + goal_response.append({ + 'id': goal.goal_id, + 'title': goal.title + }) + return jsonify(goal_response) + + elif request.method == 'POST': + request_body = request.get_json() + if "title" in request_body: + new_goal = Goal( + title = request_body['title'] + ) + db.session.add(new_goal) + db.session.commit() + return{ + 'goal': + new_goal.serialize() + },201 + +@goal_list_bp.route('/', methods = ['GET']) +def handle_goal(goal_id): + goal = Goal.query.get(goal_id) + if not goal: + return "", 404 + + if request.method == 'GET': + #check expected output in test + return({ + 'goal': + goal.serialize() + + }) + def order_by_title(task_response): @@ -48,11 +94,18 @@ def handle_tasks(): "details": "Invalid data" }, 400 stringify_format = "%a, %d %b %Y %H:%M:%S %Z" - new_task = Task( - title = request_body['title'], - description = request_body['description'], - completed_at = datetime.strptime(request_body["completed_at"], stringify_format) - ) + if request_body["completed_at"]: + + new_task = Task( + title = request_body['title'], + description = request_body['description'], + completed_at = datetime.strptime(request_body["completed_at"], stringify_format) + ) + else: + new_task = Task( + title = request_body['title'], + description = request_body['description'], + ) db.session.add(new_task) db.session.commit() return{ @@ -102,6 +155,7 @@ def mark_complete_task(task_id): if not task: return "", 404 task.completed_at = datetime.utcnow() + task.notify_slack() return{ 'task':{ 'id': task.task_id, diff --git a/migrations/versions/4b0b452824e1_.py b/migrations/versions/4b0b452824e1_.py new file mode 100644 index 000000000..0f8c03076 --- /dev/null +++ b/migrations/versions/4b0b452824e1_.py @@ -0,0 +1,30 @@ +"""empty message + +Revision ID: 4b0b452824e1 +Revises: b05ae8147b20 +Create Date: 2021-05-18 17:11:57.232014 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '4b0b452824e1' +down_revision = 'b05ae8147b20' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('task', sa.Column('goal_id', sa.Integer(), nullable=True)) + op.create_foreign_key(None, 'task', 'goal', ['goal_id'], ['goal_id']) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'task', type_='foreignkey') + op.drop_column('task', 'goal_id') + # ### end Alembic commands ### diff --git a/migrations/versions/b05ae8147b20_.py b/migrations/versions/b05ae8147b20_.py new file mode 100644 index 000000000..6cf13f13c --- /dev/null +++ b/migrations/versions/b05ae8147b20_.py @@ -0,0 +1,30 @@ +"""empty message + +Revision ID: b05ae8147b20 +Revises: a50e57a3eff0 +Create Date: 2021-05-17 18:04:32.681801 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'b05ae8147b20' +down_revision = 'a50e57a3eff0' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('goal', sa.Column('title', sa.String(), nullable=False)) + op.drop_column('task', 'is_complete') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('task', sa.Column('is_complete', sa.BOOLEAN(), autoincrement=False, nullable=True)) + op.drop_column('goal', 'title') + # ### end Alembic commands ### From 62ecac489b666a3ddd845d8aa22fb8f9c45b72ae Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Tue, 18 May 2021 21:51:58 -0700 Subject: [PATCH 07/16] added PUT method for goals --- app/routes.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index e9dc2863d..1d2857bd1 100644 --- a/app/routes.py +++ b/app/routes.py @@ -37,7 +37,7 @@ def handle_goals(): new_goal.serialize() },201 -@goal_list_bp.route('/', methods = ['GET']) +@goal_list_bp.route('/', methods = ['GET','PUT']) def handle_goal(goal_id): goal = Goal.query.get(goal_id) if not goal: @@ -50,7 +50,14 @@ def handle_goal(goal_id): goal.serialize() }) - + elif request.method == 'PUT': + request_body = request.get_json() + if 'title' in request_body: + goal.title = request_body['title'] + db.session.commit() + return({ + 'goal': goal.serialize() + },200) def order_by_title(task_response): From 827ade9e66ebe1bc1e9d9227eae591724b55a793 Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Tue, 18 May 2021 22:34:38 -0700 Subject: [PATCH 08/16] added update and delete methods --- app/routes.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 1d2857bd1..ae4a56b4f 100644 --- a/app/routes.py +++ b/app/routes.py @@ -37,7 +37,7 @@ def handle_goals(): new_goal.serialize() },201 -@goal_list_bp.route('/', methods = ['GET','PUT']) +@goal_list_bp.route('/', methods = ['GET','PUT', 'DELETE']) def handle_goal(goal_id): goal = Goal.query.get(goal_id) if not goal: @@ -58,6 +58,13 @@ def handle_goal(goal_id): return({ 'goal': goal.serialize() },200) + + elif request.method =='DELETE': + db.session.delete(goal) + db.session.commit() + return({ + "details": f'Goal {goal_id} "{goal.title}" successfully deleted' + },200) def order_by_title(task_response): From efabc3ea7be6bcb083f66a39b8c215e89ad9b13f Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Wed, 19 May 2021 00:27:59 -0700 Subject: [PATCH 09/16] added create goal missing title completed wave 5 --- app/routes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/routes.py b/app/routes.py index ae4a56b4f..c9b52a02e 100644 --- a/app/routes.py +++ b/app/routes.py @@ -36,6 +36,10 @@ def handle_goals(): 'goal': new_goal.serialize() },201 + else: + return({ + "details": f'Invalid data' + },400) @goal_list_bp.route('/', methods = ['GET','PUT', 'DELETE']) def handle_goal(goal_id): From 3630670989973a84f87a2307f4eb80aa4dcab98c Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Mon, 24 May 2021 11:42:16 -0700 Subject: [PATCH 10/16] added route for seeing task(s) associated with goal id --- app/models/task.py | 10 +++++++ app/routes.py | 69 ++++++++++++++++++++++++++++++++++++++++++- tests/test_wave_06.py | 3 ++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/app/models/task.py b/app/models/task.py index a7bc7fe19..cabe36ed9 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -20,6 +20,16 @@ def serialize(self): } return result + # def serialize_two(self): + # result = { + # 'id': self.task_id, + # 'goal_id': self.goal_id, + # 'title': self.title, + # 'description': self.description, + # 'is_complete': self.completed_at != None + # } + # return result + def notify_slack(self): message = f"Someone just completed the task {self.title}" url = "https://slack.com/api/chat.postMessage" diff --git a/app/routes.py b/app/routes.py index c9b52a02e..f5c77bf71 100644 --- a/app/routes.py +++ b/app/routes.py @@ -70,6 +70,31 @@ def handle_goal(goal_id): "details": f'Goal {goal_id} "{goal.title}" successfully deleted' },200) +@goal_list_bp.route('//tasks', methods = ['GET']) +def get_tasks_for_goal(goal_id): + goal = Goal.query.get(goal_id) + if not goal: + return "", 404 + + tasks = goal.tasks + json_tasks = [] + for task in tasks: + serializeds_task = task.serialize() # dict + serializeds_task['goal_id'] = int(goal_id) + json_tasks.append(serializeds_task) + # result = { + # 'id': self.task_id, + # 'title': self.title, + # 'description': self.description, + # 'is_complete': self.completed_at != None + # } + + return { + "id": goal.goal_id, + "title": goal.title, + "tasks": json_tasks + }, 200 + def order_by_title(task_response): return task_response["title"] @@ -86,7 +111,8 @@ def handle_tasks(): 'id': task.task_id, 'title': task.title, 'description': task.description, - 'is_complete': task.completed_at != None + 'is_complete': task.completed_at != None, + 'goal_id': task.goal_id }) # allows access to keys - sort arg = request.args @@ -162,6 +188,8 @@ def handle_task(task_id): # same name as parameter route task.description = request_body['description'] if 'completed_at' in request_body: task.completed = request_body['completed_at'] + if 'goal_id' in request_body: + task.goal_id= request_body['goal_id'] db.session.commit() return({ 'task': task.serialize() @@ -197,6 +225,45 @@ def mark_incomplete_task(task_id): } },200 +@goal_list_bp.route('//tasks', methods = ['POST']) +def add_task_to_goal(goal_id): + goal = Goal.query.get(goal_id) + tasks = request.json + tasks = tasks["task_ids"] + if not goal: + return "", 404 + # task = db.session.query(Task.id) + query = db.session.query(Task).filter(Task.task_id.in_(tasks)) + + results = query.all() + for task in results: + task.goal_id = goal.goal_id + return{ + 'id': goal.goal_id, + 'task_ids': tasks + },200 + + + + + + + + + + +#filter + #get task id from request body + + + #Use task id to query test database to get those task sql alchemy join filter all + #loop through the list + #Set each tasks.goal id to the goal id of goal task.goal_id = goal.id + #create response body from test + + + + diff --git a/tests/test_wave_06.py b/tests/test_wave_06.py index 48ecd0523..e8aa19417 100644 --- a/tests/test_wave_06.py +++ b/tests/test_wave_06.py @@ -54,6 +54,9 @@ def test_get_tasks_for_specific_goal_no_tasks(client, one_goal): response = client.get("/goals/1/tasks") response_body = response.get_json() + # package = amazon_website.buy("socks") + # package_contents = package.get_json() + # Assert assert response.status_code == 200 assert "tasks" in response_body From 7ab413e937dac5f3c257be6f80a8b38a2764600a Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Tue, 25 May 2021 14:55:42 -0700 Subject: [PATCH 11/16] used helper method serialize in a few more places --- app/models/task.py | 2 ++ app/routes.py | 89 +++++++++++++++++++--------------------------- 2 files changed, 39 insertions(+), 52 deletions(-) diff --git a/app/models/task.py b/app/models/task.py index cabe36ed9..8645c409d 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -18,6 +18,8 @@ def serialize(self): 'description': self.description, 'is_complete': self.completed_at != None } + if self.goal_id: + result['goal_id'] = self.goal_id return result # def serialize_two(self): diff --git a/app/routes.py b/app/routes.py index f5c77bf71..e4e41df0d 100644 --- a/app/routes.py +++ b/app/routes.py @@ -9,7 +9,9 @@ import requests - +def order_by_title(task_response): + return task_response["title"] + goal_list_bp = Blueprint("goal_list", __name__, url_prefix = '/goals') @goal_list_bp.route('', methods = ['GET','POST']) @@ -18,10 +20,7 @@ def handle_goals(): goals = Goal.query.all() goal_response = [] for goal in goals: - goal_response.append({ - 'id': goal.goal_id, - 'title': goal.title - }) + goal_response.append(goal.serialize()) return jsonify(goal_response) elif request.method == 'POST': @@ -82,12 +81,7 @@ def get_tasks_for_goal(goal_id): serializeds_task = task.serialize() # dict serializeds_task['goal_id'] = int(goal_id) json_tasks.append(serializeds_task) - # result = { - # 'id': self.task_id, - # 'title': self.title, - # 'description': self.description, - # 'is_complete': self.completed_at != None - # } + return { "id": goal.goal_id, @@ -95,34 +89,49 @@ def get_tasks_for_goal(goal_id): "tasks": json_tasks }, 200 +@goal_list_bp.route('//tasks', methods = ['POST']) +def add_task_to_goal(goal_id): + goal = Goal.query.get(goal_id) + tasks = request.json + tasks = tasks["task_ids"] + if not goal: + return "", 404 + # task = db.session.query(Task.id) + query = db.session.query(Task).filter(Task.task_id.in_(tasks)) + + results = query.all() + for task in results: + task.goal_id = goal.goal_id + return{ + 'id': goal.goal_id, + 'task_ids': tasks + },200 + + + -def order_by_title(task_response): - return task_response["title"] task_list_bp = Blueprint("task_list", __name__, url_prefix='/tasks') @task_list_bp.route('', methods=['GET', 'POST']) def handle_tasks(): if request.method == 'GET': #return full list of tasks - tasks = Task.query.all() - task_response = [] - for task in tasks: - task_response.append({ - 'id': task.task_id, - 'title': task.title, - 'description': task.description, - 'is_complete': task.completed_at != None, - 'goal_id': task.goal_id - }) + # allows access to keys - sort - arg = request.args - if "sort" in request.args: + arg = request.args # better name + if "sort" in arg: if arg['sort'] == "asc": - task_response = sorted(task_response, key = order_by_title) + tasks = Task.query.order_by(Task.title.asc()) elif arg['sort'] == "desc": - task_response = sorted(task_response, key = order_by_title, reverse = True) - + tasks = Task.query.order_by(Task.title.desc()) + else: + tasks = Task.query.all() + task_response = [] + for task in tasks: + task_response.append(task.serialize()) + return jsonify(task_response) + elif request.method == 'POST': request_body = request.get_json() if "title" not in request_body: @@ -225,24 +234,7 @@ def mark_incomplete_task(task_id): } },200 -@goal_list_bp.route('//tasks', methods = ['POST']) -def add_task_to_goal(goal_id): - goal = Goal.query.get(goal_id) - tasks = request.json - tasks = tasks["task_ids"] - if not goal: - return "", 404 - # task = db.session.query(Task.id) - query = db.session.query(Task).filter(Task.task_id.in_(tasks)) - results = query.all() - for task in results: - task.goal_id = goal.goal_id - return{ - 'id': goal.goal_id, - 'task_ids': tasks - },200 - @@ -252,14 +244,7 @@ def add_task_to_goal(goal_id): -#filter - #get task id from request body - - #Use task id to query test database to get those task sql alchemy join filter all - #loop through the list - #Set each tasks.goal id to the goal id of goal task.goal_id = goal.id - #create response body from test From 65fe2d364bb2b34a129a8a1ee552eb94b48580eb Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Tue, 25 May 2021 16:52:14 -0700 Subject: [PATCH 12/16] add Procfile and finished wave 6 --- Procfile | 1 + app/models/task.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 Procfile diff --git a/Procfile b/Procfile new file mode 100644 index 000000000..62e430aca --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn 'app:create_app()' \ No newline at end of file diff --git a/app/models/task.py b/app/models/task.py index 8645c409d..a52c84818 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -2,6 +2,7 @@ from requests.models import parse_header_links from app import db import requests +import os class Task(db.Model): @@ -41,7 +42,7 @@ def notify_slack(self): "text": message } headers = { - "Authorization":"Bearer xoxb-2054232917126-2062969942578-52P3S23cj940QFt3KlQnQzOW", + "Authorization": os.environ.get('SLACK_API') } r = requests.post(url, data = params, headers = headers) r.status_code From 661a0f987a114f73bae87c05b8f8dc0eb634f170 Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Tue, 25 May 2021 17:49:22 -0700 Subject: [PATCH 13/16] re adding migrations folder after heroku error msg --- migrations/versions/4b0b452824e1_.py | 30 ---------------- .../{7f45ce564a8c_.py => 51918445a19e_.py} | 13 ++++--- migrations/versions/84ac43910dbf_.py | 30 ---------------- migrations/versions/a50e57a3eff0_.py | 28 --------------- migrations/versions/af1f5482504b_.py | 28 --------------- migrations/versions/b05ae8147b20_.py | 30 ---------------- migrations/versions/f3d84e20bc58_.py | 34 ------------------- 7 files changed, 8 insertions(+), 185 deletions(-) delete mode 100644 migrations/versions/4b0b452824e1_.py rename migrations/versions/{7f45ce564a8c_.py => 51918445a19e_.py} (65%) delete mode 100644 migrations/versions/84ac43910dbf_.py delete mode 100644 migrations/versions/a50e57a3eff0_.py delete mode 100644 migrations/versions/af1f5482504b_.py delete mode 100644 migrations/versions/b05ae8147b20_.py delete mode 100644 migrations/versions/f3d84e20bc58_.py diff --git a/migrations/versions/4b0b452824e1_.py b/migrations/versions/4b0b452824e1_.py deleted file mode 100644 index 0f8c03076..000000000 --- a/migrations/versions/4b0b452824e1_.py +++ /dev/null @@ -1,30 +0,0 @@ -"""empty message - -Revision ID: 4b0b452824e1 -Revises: b05ae8147b20 -Create Date: 2021-05-18 17:11:57.232014 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '4b0b452824e1' -down_revision = 'b05ae8147b20' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('task', sa.Column('goal_id', sa.Integer(), nullable=True)) - op.create_foreign_key(None, 'task', 'goal', ['goal_id'], ['goal_id']) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_constraint(None, 'task', type_='foreignkey') - op.drop_column('task', 'goal_id') - # ### end Alembic commands ### diff --git a/migrations/versions/7f45ce564a8c_.py b/migrations/versions/51918445a19e_.py similarity index 65% rename from migrations/versions/7f45ce564a8c_.py rename to migrations/versions/51918445a19e_.py index fb8b04f36..8966ccc9f 100644 --- a/migrations/versions/7f45ce564a8c_.py +++ b/migrations/versions/51918445a19e_.py @@ -1,8 +1,8 @@ """empty message -Revision ID: 7f45ce564a8c +Revision ID: 51918445a19e Revises: -Create Date: 2021-05-05 19:55:02.318438 +Create Date: 2021-05-25 17:45:04.997629 """ from alembic import op @@ -10,7 +10,7 @@ # revision identifiers, used by Alembic. -revision = '7f45ce564a8c' +revision = '51918445a19e' down_revision = None branch_labels = None depends_on = None @@ -19,14 +19,17 @@ def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table('goal', - sa.Column('goal_id', sa.Integer(), nullable=False), + sa.Column('goal_id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('title', sa.String(), nullable=False), sa.PrimaryKeyConstraint('goal_id') ) op.create_table('task', - sa.Column('task_id', sa.Integer(), nullable=False), + sa.Column('task_id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('title', sa.String(), nullable=True), sa.Column('description', sa.String(), nullable=True), sa.Column('completed_at', sa.DateTime(), nullable=True), + sa.Column('goal_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['goal_id'], ['goal.goal_id'], ), sa.PrimaryKeyConstraint('task_id') ) # ### end Alembic commands ### diff --git a/migrations/versions/84ac43910dbf_.py b/migrations/versions/84ac43910dbf_.py deleted file mode 100644 index 3934666ee..000000000 --- a/migrations/versions/84ac43910dbf_.py +++ /dev/null @@ -1,30 +0,0 @@ -"""empty message - -Revision ID: 84ac43910dbf -Revises: af1f5482504b -Create Date: 2021-05-06 18:26:16.610407 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '84ac43910dbf' -down_revision = 'af1f5482504b' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('task', sa.Column('id', sa.Integer(), nullable=False)) - op.drop_column('task', 'task_id') - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('task', sa.Column('task_id', sa.INTEGER(), autoincrement=True, nullable=False)) - op.drop_column('task', 'id') - # ### end Alembic commands ### diff --git a/migrations/versions/a50e57a3eff0_.py b/migrations/versions/a50e57a3eff0_.py deleted file mode 100644 index 2e4eabbf3..000000000 --- a/migrations/versions/a50e57a3eff0_.py +++ /dev/null @@ -1,28 +0,0 @@ -"""empty message - -Revision ID: a50e57a3eff0 -Revises: f3d84e20bc58 -Create Date: 2021-05-10 15:49:17.182962 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = 'a50e57a3eff0' -down_revision = 'f3d84e20bc58' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('task', sa.Column('is_complete', sa.Boolean(), nullable=True)) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_column('task', 'is_complete') - # ### end Alembic commands ### diff --git a/migrations/versions/af1f5482504b_.py b/migrations/versions/af1f5482504b_.py deleted file mode 100644 index 76cd28715..000000000 --- a/migrations/versions/af1f5482504b_.py +++ /dev/null @@ -1,28 +0,0 @@ -"""empty message - -Revision ID: af1f5482504b -Revises: 7f45ce564a8c -Create Date: 2021-05-06 18:18:16.649575 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = 'af1f5482504b' -down_revision = '7f45ce564a8c' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('task', sa.Column('is_complete', sa.Boolean(), nullable=True)) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_column('task', 'is_complete') - # ### end Alembic commands ### diff --git a/migrations/versions/b05ae8147b20_.py b/migrations/versions/b05ae8147b20_.py deleted file mode 100644 index 6cf13f13c..000000000 --- a/migrations/versions/b05ae8147b20_.py +++ /dev/null @@ -1,30 +0,0 @@ -"""empty message - -Revision ID: b05ae8147b20 -Revises: a50e57a3eff0 -Create Date: 2021-05-17 18:04:32.681801 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = 'b05ae8147b20' -down_revision = 'a50e57a3eff0' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('goal', sa.Column('title', sa.String(), nullable=False)) - op.drop_column('task', 'is_complete') - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('task', sa.Column('is_complete', sa.BOOLEAN(), autoincrement=False, nullable=True)) - op.drop_column('goal', 'title') - # ### end Alembic commands ### diff --git a/migrations/versions/f3d84e20bc58_.py b/migrations/versions/f3d84e20bc58_.py deleted file mode 100644 index 5f50f2482..000000000 --- a/migrations/versions/f3d84e20bc58_.py +++ /dev/null @@ -1,34 +0,0 @@ -"""empty message - -Revision ID: f3d84e20bc58 -Revises: 84ac43910dbf -Create Date: 2021-05-09 14:38:07.209685 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = 'f3d84e20bc58' -down_revision = '84ac43910dbf' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('task', - sa.Column('task_id', sa.Integer(), autoincrement=True, nullable=False), - sa.Column('title', sa.String(), nullable=True), - sa.Column('description', sa.String(), nullable=True), - sa.Column('completed_at', sa.DateTime(), nullable=True), - sa.PrimaryKeyConstraint('task_id') - ) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('task') - # ### end Alembic commands ### From 8e41386ed6d563f2ebfd2ec1efd7843e6d7c32f0 Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Tue, 6 Jul 2021 12:34:09 -0700 Subject: [PATCH 14/16] added db commit to task posts --- app/routes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/routes.py b/app/routes.py index e4e41df0d..f1926e004 100644 --- a/app/routes.py +++ b/app/routes.py @@ -102,6 +102,8 @@ def add_task_to_goal(goal_id): results = query.all() for task in results: task.goal_id = goal.goal_id + db.session.commit() + return{ 'id': goal.goal_id, 'task_ids': tasks From 4eb44d482da1f8320963304780e6c8c37b01ef9a Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Tue, 6 Jul 2021 13:16:14 -0700 Subject: [PATCH 15/16] added db commit to mark complete and incomplete --- app/routes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index f1926e004..43e93b9ad 100644 --- a/app/routes.py +++ b/app/routes.py @@ -210,9 +210,10 @@ def handle_task(task_id): # same name as parameter route def mark_complete_task(task_id): task = Task.query.get(task_id) if not task: - return "", 404 + return "Task not found", 404 task.completed_at = datetime.utcnow() task.notify_slack() + db.session.commit() return{ 'task':{ 'id': task.task_id, @@ -225,8 +226,9 @@ def mark_complete_task(task_id): def mark_incomplete_task(task_id): task = Task.query.get(task_id) if not task: - return "", 404 + return "Task Not Found", 404 task.completed_at = None + db.session.commit() return{ 'task':{ 'id': task.task_id, From 7ea7661c3d5d4d32c108ad09d52fb95632cdd2bf Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Wed, 7 Jul 2021 12:55:02 -0700 Subject: [PATCH 16/16] added corsto files --- app/__init__.py | 2 ++ requirements.txt | 1 + 2 files changed, 3 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index d5935c4a6..a50705cc1 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,6 +1,7 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate +from flask_cors import CORS import os from dotenv import load_dotenv @@ -27,6 +28,7 @@ def create_app(test_config=None): from app.models.goal import Goal db.init_app(app) + CORS(app) migrate.init_app(app, db) diff --git a/requirements.txt b/requirements.txt index cfdf74050..eac2ec627 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ certifi==2020.12.5 chardet==4.0.0 click==7.1.2 Flask==1.1.2 +flask-cors Flask-Migrate==2.6.0 Flask-SQLAlchemy==2.4.4 gunicorn==20.1.0