From 058b9b35a316d5e91215103ed430e0f291be19c4 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Tue, 9 May 2023 18:13:16 -0400 Subject: [PATCH 01/49] created Task model --- app/models/task.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/models/task.py b/app/models/task.py index c91ab281f..d098f7ee2 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -3,3 +3,6 @@ class Task(db.Model): task_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) From c14aa16baf2bf4535336ec506431aa55b92d213f Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Tue, 9 May 2023 18:18:43 -0400 Subject: [PATCH 02/49] adds autoincrement to task_id --- app/models/task.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/task.py b/app/models/task.py index d098f7ee2..569493509 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -2,7 +2,7 @@ class Task(db.Model): - task_id = db.Column(db.Integer, primary_key=True) + task_id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String) description = db.Column(db.String) completed_at = db.Column(db.DateTime, nullable=True) From 10c8536ecae21405359a93fdcccf6dd868989ec5 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Tue, 9 May 2023 18:24:38 -0400 Subject: [PATCH 03/49] task model migration --- migrations/README | 1 + migrations/alembic.ini | 45 +++++++++++++ migrations/env.py | 96 ++++++++++++++++++++++++++++ migrations/script.py.mako | 24 +++++++ migrations/versions/04b4481baf52_.py | 39 +++++++++++ 5 files changed, 205 insertions(+) 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/04b4481baf52_.py 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/04b4481baf52_.py b/migrations/versions/04b4481baf52_.py new file mode 100644 index 000000000..9408be4f2 --- /dev/null +++ b/migrations/versions/04b4481baf52_.py @@ -0,0 +1,39 @@ +"""empty message + +Revision ID: 04b4481baf52 +Revises: +Create Date: 2023-05-09 18:22:29.702391 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '04b4481baf52' +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(), 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') + op.drop_table('goal') + # ### end Alembic commands ### From 015f3f5046c8abdb60c78d535722c7dfff7546fa Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Tue, 9 May 2023 18:46:13 -0400 Subject: [PATCH 04/49] created and registered task blueprint --- app/__init__.py | 5 ++++- app/task_routes.py | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 app/task_routes.py diff --git a/app/__init__.py b/app/__init__.py index 2764c4cc8..59377428b 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,8 +1,9 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate -import os from dotenv import load_dotenv +import os + db = SQLAlchemy() @@ -30,5 +31,7 @@ def create_app(test_config=None): migrate.init_app(app, db) # Register Blueprints here + from .task_routes import tasks_bp + app.register_blueprint(tasks_bp) return app diff --git a/app/task_routes.py b/app/task_routes.py new file mode 100644 index 000000000..fd73eb38c --- /dev/null +++ b/app/task_routes.py @@ -0,0 +1,5 @@ +from app import db +from app.models.task import Task +from flask import Blueprint, jsonify, make_response, abort + +tasks_bp = Blueprint("tasks", __name__, url_prefix = "/tasks") \ No newline at end of file From 5c9216203e5d7bcfa521b626b61eca822f4d5e51 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Tue, 9 May 2023 20:46:21 -0400 Subject: [PATCH 05/49] adds read task route --- app/task_routes.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/app/task_routes.py b/app/task_routes.py index fd73eb38c..d22e479cf 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -1,5 +1,22 @@ from app import db from app.models.task import Task -from flask import Blueprint, jsonify, make_response, abort +from flask import Blueprint, jsonify, make_response, abort, request -tasks_bp = Blueprint("tasks", __name__, url_prefix = "/tasks") \ No newline at end of file +tasks_bp = Blueprint("tasks", __name__, url_prefix = "/tasks") + +@tasks_bp.route("", methods=["GET"]) +def read_task(): + tasks_response = [] + tasks = Task.query.all() + + for task in tasks: + tasks_response.append( + { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": False + } + ), 200 + + return jsonify(tasks_response) From 259ce1ee8eed1a87af45a660ef7b155850b049fd Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Tue, 9 May 2023 21:01:33 -0400 Subject: [PATCH 06/49] adds create task route --- app/task_routes.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/app/task_routes.py b/app/task_routes.py index d22e479cf..e908e2172 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -4,6 +4,33 @@ tasks_bp = Blueprint("tasks", __name__, url_prefix = "/tasks") +@tasks_bp.route("", methods=["POST"]) +def create_task(): + request_body = request.get_json() + + task = Task( + title = request_body["title"], + description = request_body["description"], + ) + + db.session.add(task) + db.session.commit() + + return make_response( + { + "task": { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": False + } + }, 201 + ) + + + + + @tasks_bp.route("", methods=["GET"]) def read_task(): tasks_response = [] From 8ece6ac67a63b1db8a747c42477984daf3211426 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Tue, 9 May 2023 21:13:27 -0400 Subject: [PATCH 07/49] adds read one saved task --- app/task_routes.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/task_routes.py b/app/task_routes.py index e908e2172..33440f28b 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -27,12 +27,8 @@ def create_task(): }, 201 ) - - - - @tasks_bp.route("", methods=["GET"]) -def read_task(): +def read_tasks(): tasks_response = [] tasks = Task.query.all() @@ -47,3 +43,16 @@ def read_task(): ), 200 return jsonify(tasks_response) + +@tasks_bp.route("/", methods=["GET"]) +def read_one_saved_task(task_id): + task = Task.query.get(task_id) + + return { + "task": { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": False + } + } \ No newline at end of file From 33c7dd48cf618e9a01469a99c0cce077cd9b52c8 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Wed, 10 May 2023 11:05:29 -0400 Subject: [PATCH 08/49] adds update task route --- app/task_routes.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/app/task_routes.py b/app/task_routes.py index 33440f28b..3b7675e01 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -55,4 +55,26 @@ def read_one_saved_task(task_id): "description": task.description, "is_complete": False } - } \ No newline at end of file + } + +@tasks_bp.route("/", methods=["PUT"]) +def update_task(task_id): + task = Task.query.get(task_id) + + request_body = request.get_json() + + task.title = request_body["title"] + task.description = request_body["description"] + + db.session.commit() + + return make_response( + { + "task": { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": False + } + }, 200 + ) \ No newline at end of file From 6e3acafc5f8c3e1381b5d4e52a9434ad4a8b77c0 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Wed, 10 May 2023 11:12:44 -0400 Subject: [PATCH 09/49] adds delete task route --- app/task_routes.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/task_routes.py b/app/task_routes.py index 3b7675e01..907426d0f 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -77,4 +77,17 @@ def update_task(task_id): "is_complete": False } }, 200 + ) + +@tasks_bp.route("/", methods=["DELETE"]) +def delete_task(task_id): + task = Task.query.get(task_id) + + db.session.delete(task) + db.session.commit() + + return make_response( + { + "details": f"Task {task.task_id} \"{task.title}\" successfully deleted" + }, 200 ) \ No newline at end of file From 7666b42696b9e381d5494d7bc3545d31f355f57a Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Wed, 10 May 2023 16:04:03 -0400 Subject: [PATCH 10/49] adds validate_tasks function to routes --- app/task_routes.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/task_routes.py b/app/task_routes.py index 907426d0f..5c9ed77bd 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -46,7 +46,7 @@ def read_tasks(): @tasks_bp.route("/", methods=["GET"]) def read_one_saved_task(task_id): - task = Task.query.get(task_id) + task = validate_task(task_id) return { "task": { @@ -59,7 +59,7 @@ def read_one_saved_task(task_id): @tasks_bp.route("/", methods=["PUT"]) def update_task(task_id): - task = Task.query.get(task_id) + task = validate_task(task_id) request_body = request.get_json() @@ -81,7 +81,7 @@ def update_task(task_id): @tasks_bp.route("/", methods=["DELETE"]) def delete_task(task_id): - task = Task.query.get(task_id) + task = validate_task(task_id) db.session.delete(task) db.session.commit() @@ -90,4 +90,16 @@ def delete_task(task_id): { "details": f"Task {task.task_id} \"{task.title}\" successfully deleted" }, 200 - ) \ No newline at end of file + ) + +def validate_task(task_id): + task = Task.query.get(task_id) + + if not task: + abort(make_response( + { + "details": "Invalid data" + }, 404 + )) + return task + \ No newline at end of file From 53e7f4fea0006d2627948c1c33864934b9e2812a Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Wed, 10 May 2023 16:14:36 -0400 Subject: [PATCH 11/49] updated validate_task_id function --- app/task_routes.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/task_routes.py b/app/task_routes.py index 5c9ed77bd..778b44f80 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -46,7 +46,7 @@ def read_tasks(): @tasks_bp.route("/", methods=["GET"]) def read_one_saved_task(task_id): - task = validate_task(task_id) + task = validate_task_id(task_id) return { "task": { @@ -59,7 +59,7 @@ def read_one_saved_task(task_id): @tasks_bp.route("/", methods=["PUT"]) def update_task(task_id): - task = validate_task(task_id) + task = validate_task_id(task_id) request_body = request.get_json() @@ -81,7 +81,7 @@ def update_task(task_id): @tasks_bp.route("/", methods=["DELETE"]) def delete_task(task_id): - task = validate_task(task_id) + task = validate_task_id(task_id) db.session.delete(task) db.session.commit() @@ -92,13 +92,13 @@ def delete_task(task_id): }, 200 ) -def validate_task(task_id): +def validate_task_id(task_id): task = Task.query.get(task_id) if not task: abort(make_response( { - "details": "Invalid data" + "details": "Task ID not found" }, 404 )) return task From 586c405c1d1b611e0a4aebbd2b9476c7dd7d2702 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Wed, 10 May 2023 18:21:06 -0400 Subject: [PATCH 12/49] renamed validate_task function and adds validation in POST route --- app/task_routes.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/app/task_routes.py b/app/task_routes.py index 778b44f80..aac6297c9 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -6,8 +6,17 @@ @tasks_bp.route("", methods=["POST"]) def create_task(): + request_body = request.get_json() + + if not request_body.get("title") or not request_body.get("description"): + abort(make_response( + { + "details": "Invalid data" + }, 400 + )) + task = Task( title = request_body["title"], description = request_body["description"], @@ -22,7 +31,7 @@ def create_task(): "id": task.task_id, "title": task.title, "description": task.description, - "is_complete": False + "is_complete": True if task.completed_at else False } }, 201 ) @@ -38,7 +47,7 @@ def read_tasks(): "id": task.task_id, "title": task.title, "description": task.description, - "is_complete": False + "is_complete": True if task.completed_at else False } ), 200 @@ -46,20 +55,20 @@ def read_tasks(): @tasks_bp.route("/", methods=["GET"]) def read_one_saved_task(task_id): - task = validate_task_id(task_id) + task = validate_task(task_id) return { "task": { "id": task.task_id, "title": task.title, "description": task.description, - "is_complete": False + "is_complete": True if task.completed_at else False } } @tasks_bp.route("/", methods=["PUT"]) def update_task(task_id): - task = validate_task_id(task_id) + task = validate_task(task_id) request_body = request.get_json() @@ -74,14 +83,14 @@ def update_task(task_id): "id": task.task_id, "title": task.title, "description": task.description, - "is_complete": False + "is_complete": True if task.completed_at else False } }, 200 ) @tasks_bp.route("/", methods=["DELETE"]) def delete_task(task_id): - task = validate_task_id(task_id) + task = validate_task(task_id) db.session.delete(task) db.session.commit() @@ -92,7 +101,7 @@ def delete_task(task_id): }, 200 ) -def validate_task_id(task_id): +def validate_task(task_id): task = Task.query.get(task_id) if not task: @@ -101,5 +110,4 @@ def validate_task_id(task_id): "details": "Task ID not found" }, 404 )) - return task - \ No newline at end of file + return task \ No newline at end of file From b762abee5d434107224a6ff755ea017ef7de8882 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Wed, 10 May 2023 18:22:48 -0400 Subject: [PATCH 13/49] adds assertions to tests --- tests/test_wave_01.py | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index dca626d78..f9ba041d8 100644 --- a/tests/test_wave_01.py +++ b/tests/test_wave_01.py @@ -2,7 +2,7 @@ import pytest -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_tasks_no_saved_tasks(client): # Act response = client.get("/tasks") @@ -13,7 +13,7 @@ def test_get_tasks_no_saved_tasks(client): assert response_body == [] -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_tasks_one_saved_tasks(client, one_task): # Act response = client.get("/tasks") @@ -32,7 +32,7 @@ def test_get_tasks_one_saved_tasks(client, one_task): ] -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_task(client, one_task): # Act response = client.get("/tasks/1") @@ -51,7 +51,7 @@ def test_get_task(client, one_task): } -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_task_not_found(client): # Act response = client.get("/tasks/1") @@ -59,14 +59,17 @@ def test_get_task_not_found(client): # Assert assert response.status_code == 404 + assert response_body == { + "details": "Task ID not found" + } - raise Exception("Complete test with assertion about response body") - # ***************************************************************** + # raise Exception("Complete test with assertion about response body") + # # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_create_task(client): # Act response = client.post("/tasks", json={ @@ -93,7 +96,7 @@ def test_create_task(client): assert new_task.completed_at == None -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_update_task(client, one_task): # Act response = client.put("/tasks/1", json={ @@ -119,7 +122,7 @@ def test_update_task(client, one_task): assert task.completed_at == None -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_update_task_not_found(client): # Act response = client.put("/tasks/1", json={ @@ -130,14 +133,16 @@ def test_update_task_not_found(client): # Assert assert response.status_code == 404 - - raise Exception("Complete test with assertion about response body") + assert response_body == { + "details": "Task ID not found" + } + # raise Exception("Complete test with assertion about response body") # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_delete_task(client, one_task): # Act response = client.delete("/tasks/1") @@ -152,7 +157,7 @@ def test_delete_task(client, one_task): assert Task.query.get(1) == None -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_delete_task_not_found(client): # Act response = client.delete("/tasks/1") @@ -160,8 +165,11 @@ def test_delete_task_not_found(client): # Assert assert response.status_code == 404 + assert response_body == { + "details": "Task ID not found" + } - raise Exception("Complete test with assertion about response body") + # raise Exception("Complete test with assertion about response body") # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** @@ -169,7 +177,7 @@ def test_delete_task_not_found(client): assert Task.query.all() == [] -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_create_task_must_contain_title(client): # Act response = client.post("/tasks", json={ @@ -186,7 +194,7 @@ def test_create_task_must_contain_title(client): assert Task.query.all() == [] -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_create_task_must_contain_description(client): # Act response = client.post("/tasks", json={ From 18e61a36f4eb619e032107b7d83ca39eae93cdb4 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Wed, 10 May 2023 18:24:20 -0400 Subject: [PATCH 14/49] deletes app/routes.py --- app/routes.py | 1 - 1 file changed, 1 deletion(-) delete mode 100644 app/routes.py diff --git a/app/routes.py b/app/routes.py deleted file mode 100644 index 3aae38d49..000000000 --- a/app/routes.py +++ /dev/null @@ -1 +0,0 @@ -from flask import Blueprint \ No newline at end of file From f51a2ac8a06e07e1d1914bf3749a0cc5121900dc Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Wed, 10 May 2023 20:54:30 -0400 Subject: [PATCH 15/49] adds query param sort asc and desc by title --- app/task_routes.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/task_routes.py b/app/task_routes.py index aac6297c9..d854d088e 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -6,10 +6,8 @@ @tasks_bp.route("", methods=["POST"]) def create_task(): - request_body = request.get_json() - if not request_body.get("title") or not request_body.get("description"): abort(make_response( { @@ -38,9 +36,16 @@ def create_task(): @tasks_bp.route("", methods=["GET"]) def read_tasks(): - tasks_response = [] - tasks = Task.query.all() + + title_query = request.args.get("sort") + if title_query == "asc": + tasks = Task.query.order_by(Task.title).all() + elif title_query == "desc": + tasks = Task.query.order_by(Task.title.desc()).all() + else: + tasks = Task.query.all() + tasks_response = [] for task in tasks: tasks_response.append( { From 1ddaf52cc2c5a162a5ceaad06d92a2700c02e8ac Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 11:34:30 -0400 Subject: [PATCH 16/49] adds mark task complete --- app/task_routes.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/app/task_routes.py b/app/task_routes.py index d854d088e..e37a8aa7d 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -1,6 +1,7 @@ from app import db from app.models.task import Task from flask import Blueprint, jsonify, make_response, abort, request +import datetime tasks_bp = Blueprint("tasks", __name__, url_prefix = "/tasks") @@ -29,7 +30,7 @@ def create_task(): "id": task.task_id, "title": task.title, "description": task.description, - "is_complete": True if task.completed_at else False + "is_complete": False if not task.completed_at else True } }, 201 ) @@ -93,6 +94,27 @@ def update_task(task_id): }, 200 ) +@tasks_bp.route("//mark_complete", methods=["PATCH"]) +def mark_task_complete(task_id): + task = validate_task(task_id) + + request_body = request.get_json() + + task.completed_at = datetime.datetime.now() + + db.session.commit() + + return make_response( + { + "task": { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": True + } + }, 200 + ) + @tasks_bp.route("/", methods=["DELETE"]) def delete_task(task_id): task = validate_task(task_id) From db10823044d0cb28c0ed9f79158b047522015bb8 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 11:35:21 -0400 Subject: [PATCH 17/49] adds mark incomplete task --- app/task_routes.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/task_routes.py b/app/task_routes.py index e37a8aa7d..9dbe59e02 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -115,6 +115,27 @@ def mark_task_complete(task_id): }, 200 ) +@tasks_bp.route("//mark_incomplete", methods=["PATCH"]) +def mark_task_incomplete(task_id): + task = validate_task(task_id) + + request_body = request.get_json() + + task.completed_at = None + + db.session.commit() + + return make_response( + { + "task": { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": False + } + }, 200 + ) + @tasks_bp.route("/", methods=["DELETE"]) def delete_task(task_id): task = validate_task(task_id) From 07b3a81ebadc569e1ee4bfc86e6350b88a73edc9 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 11:47:31 -0400 Subject: [PATCH 18/49] adds assertions to test wave 3 --- tests/test_wave_03.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/tests/test_wave_03.py b/tests/test_wave_03.py index 32d379822..cd2e39d4a 100644 --- a/tests/test_wave_03.py +++ b/tests/test_wave_03.py @@ -5,7 +5,7 @@ import pytest -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_mark_complete_on_incomplete_task(client, one_task): # Arrange """ @@ -42,7 +42,7 @@ def test_mark_complete_on_incomplete_task(client, one_task): assert Task.query.get(1).completed_at -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_mark_incomplete_on_complete_task(client, completed_task): # Act response = client.patch("/tasks/1/mark_incomplete") @@ -62,7 +62,7 @@ def test_mark_incomplete_on_complete_task(client, completed_task): assert Task.query.get(1).completed_at == None -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_mark_complete_on_completed_task(client, completed_task): # Arrange """ @@ -99,7 +99,7 @@ def test_mark_complete_on_completed_task(client, completed_task): assert Task.query.get(1).completed_at -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_mark_incomplete_on_incomplete_task(client, one_task): # Act response = client.patch("/tasks/1/mark_incomplete") @@ -119,7 +119,7 @@ def test_mark_incomplete_on_incomplete_task(client, one_task): assert Task.query.get(1).completed_at == None -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_mark_complete_missing_task(client): # Act response = client.patch("/tasks/1/mark_complete") @@ -127,14 +127,17 @@ def test_mark_complete_missing_task(client): # Assert assert response.status_code == 404 + assert response_body == { + "details": "Task ID not found" + } - raise Exception("Complete test with assertion about response body") + # raise Exception("Complete test with assertion about response body") # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_mark_incomplete_missing_task(client): # Act response = client.patch("/tasks/1/mark_incomplete") @@ -142,8 +145,11 @@ def test_mark_incomplete_missing_task(client): # Assert assert response.status_code == 404 + assert response_body == { + "details": "Task ID not found" + } - raise Exception("Complete test with assertion about response body") + # raise Exception("Complete test with assertion about responpyse body") # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** From 986b5cde554f50223adc432a7860401a53f5ed3e Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 11:48:04 -0400 Subject: [PATCH 19/49] uncomments tests --- tests/test_wave_02.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_wave_02.py b/tests/test_wave_02.py index a087e0909..651e3aebd 100644 --- a/tests/test_wave_02.py +++ b/tests/test_wave_02.py @@ -1,7 +1,7 @@ import pytest -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_tasks_sorted_asc(client, three_tasks): # Act response = client.get("/tasks?sort=asc") @@ -29,7 +29,7 @@ def test_get_tasks_sorted_asc(client, three_tasks): ] -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_tasks_sorted_desc(client, three_tasks): # Act response = client.get("/tasks?sort=desc") From d509d0bf9fd03df55a794a8e523ffc94e0fb3b9a Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 16:18:49 -0400 Subject: [PATCH 20/49] adds request to slack api when task marked complete --- app/task_routes.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/task_routes.py b/app/task_routes.py index 9dbe59e02..0eadf0dd2 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -2,6 +2,8 @@ from app.models.task import Task from flask import Blueprint, jsonify, make_response, abort, request import datetime +import requests +import os tasks_bp = Blueprint("tasks", __name__, url_prefix = "/tasks") @@ -103,7 +105,23 @@ def mark_task_complete(task_id): task.completed_at = datetime.datetime.now() db.session.commit() - + + + + slack_channel_id = "C0581Q2TDGQ" + slack_url = "https://slack.com/api/chat.postMessage" + + slack_data = { + "channel": slack_channel_id, + "text": f"Someone completed the {task.title} task!" + } + + headers= { + "Authorization": f"Bearer {os.environ.get('SLACK_KEY')}" + } + + slack_request = requests.post(slack_url, headers=headers, json=slack_data) + print(slack_request.text) return make_response( { "task": { From dcf7a2ccd454ee6972154e902ba0c1188f6a466b Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 16:33:13 -0400 Subject: [PATCH 21/49] creates Goal model --- app/models/goal.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/goal.py b/app/models/goal.py index b0ed11dd8..1263d52fc 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -2,4 +2,5 @@ class Goal(db.Model): - goal_id = db.Column(db.Integer, primary_key=True) + goal_id = db.Column(db.Integer, primary_key=True, autoincrement=True) + title = db.Column(db.String) From 7f07064ae0b6bdb61335396d56a3dea73de96533 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 16:35:18 -0400 Subject: [PATCH 22/49] updates with Goal model --- migrations/versions/d3340559e8b2_.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 migrations/versions/d3340559e8b2_.py diff --git a/migrations/versions/d3340559e8b2_.py b/migrations/versions/d3340559e8b2_.py new file mode 100644 index 000000000..d379036a6 --- /dev/null +++ b/migrations/versions/d3340559e8b2_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: d3340559e8b2 +Revises: 04b4481baf52 +Create Date: 2023-05-11 16:34:20.318105 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'd3340559e8b2' +down_revision = '04b4481baf52' +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=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('goal', 'title') + # ### end Alembic commands ### From 342c5af634e8a917c864e2ca522d5b1b205a8325 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 17:03:42 -0400 Subject: [PATCH 23/49] adds create goal route --- app/goal_routes.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 app/goal_routes.py diff --git a/app/goal_routes.py b/app/goal_routes.py new file mode 100644 index 000000000..168720a36 --- /dev/null +++ b/app/goal_routes.py @@ -0,0 +1,32 @@ +from app import db +from app.models.goal import Goal +from flask import Blueprint, jsonify, make_response, abort, request + +goals_bp = Blueprint("goals'", __name__, url_prefix = "/goals") + +@goals_bp.route("", methods=["POST"]) +def create_goal(): + request_body = request.get_json() + + if not request_body.get("title"): + abort(make_response( + { + "details": "Invalid data" + }, 400 + )) + + goal = Goal( + title = request_body["title"], + ) + + db.session.add(goal) + db.session.commit() + + return make_response( + { + "goal": { + "id": goal.goal_id, + "title": goal.title, + } + }, 201 + ) \ No newline at end of file From 26090f7be3f62a224f7ca63fc082f31b46addff6 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 17:05:03 -0400 Subject: [PATCH 24/49] adds read goals route --- app/goal_routes.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/goal_routes.py b/app/goal_routes.py index 168720a36..df3d8c424 100644 --- a/app/goal_routes.py +++ b/app/goal_routes.py @@ -29,4 +29,19 @@ def create_goal(): "title": goal.title, } }, 201 - ) \ No newline at end of file + ) + +@goals_bp.route("", methods=["GET"]) +def read_goals(): + goals = Goal.query.all() + + goals_response = [] + for goal in goals: + goals_response.append( + { + "id": goal.goal_id, + "title": goal.title, + } + ), 200 + + return jsonify(goals_response) \ No newline at end of file From 756bb549baca5c17ef7dd09d7e0785644ab693ba Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 17:06:07 -0400 Subject: [PATCH 25/49] adds read one saved goal --- app/goal_routes.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/goal_routes.py b/app/goal_routes.py index df3d8c424..f144f23d0 100644 --- a/app/goal_routes.py +++ b/app/goal_routes.py @@ -44,4 +44,15 @@ def read_goals(): } ), 200 - return jsonify(goals_response) \ No newline at end of file + return jsonify(goals_response) + +@goals_bp.route("/", methods=["GET"]) +def read_one_saved_goal(goal_id): + goal = validate_goal(goal_id) + + return { + "goal": { + "id": goal.goal_id, + "title": goal.title, + } + } From 8c42aab6e521de0db26ee22013dfaf0c5840e9da Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 17:07:14 -0400 Subject: [PATCH 26/49] adds validate_goal function --- app/goal_routes.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/goal_routes.py b/app/goal_routes.py index f144f23d0..2741fd705 100644 --- a/app/goal_routes.py +++ b/app/goal_routes.py @@ -56,3 +56,15 @@ def read_one_saved_goal(goal_id): "title": goal.title, } } + + +def validate_goal(goal_id): + goal = Goal.query.get(goal_id) + + if not goal: + abort(make_response( + { + "details": "Goal ID not found" + }, 404 + )) + return goal \ No newline at end of file From 315a44ac8670ed4e7d14e7cde0eedb5e3ce77b44 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 17:20:58 -0400 Subject: [PATCH 27/49] adds update goal route --- app/goal_routes.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/goal_routes.py b/app/goal_routes.py index 2741fd705..bb948e312 100644 --- a/app/goal_routes.py +++ b/app/goal_routes.py @@ -57,6 +57,25 @@ def read_one_saved_goal(goal_id): } } +@goals_bp.route("/", methods=["PUT"]) +def update_goal(goal_id): + goal = validate_goal(goal_id) + + request_body = request.get_json() + + goal.title = request_body["title"] + + db.session.commit() + + return make_response( + { + "goal": { + "id": goal.goal_id, + "title": goal.title, + } + }, 200 + ) + def validate_goal(goal_id): goal = Goal.query.get(goal_id) From 219e8a1ae2b8fa49316f96a836c0b6f7e39a1889 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 17:29:23 -0400 Subject: [PATCH 28/49] adds delete goal route --- app/goal_routes.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/goal_routes.py b/app/goal_routes.py index bb948e312..6e6e1208c 100644 --- a/app/goal_routes.py +++ b/app/goal_routes.py @@ -76,6 +76,19 @@ def update_goal(goal_id): }, 200 ) +@goals_bp.route("/", methods=["DELETE"]) +def delete_goal(goal_id): + goal = validate_goal(goal_id) + + db.session.delete(goal) + db.session.commit() + + return make_response( + { + "details": f"Goal {goal.goal_id} \"{goal.title}\" successfully deleted" + }, 200 + ) + def validate_goal(goal_id): goal = Goal.query.get(goal_id) From 530e0821107a6bcdc4aff73e7d43eae66ca727df Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 17:31:30 -0400 Subject: [PATCH 29/49] created and registered goal blueprint --- app/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 59377428b..64cb286ac 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -33,5 +33,7 @@ def create_app(test_config=None): # Register Blueprints here from .task_routes import tasks_bp app.register_blueprint(tasks_bp) + from .goal_routes import goals_bp + app.register_blueprint(goals_bp) return app From 4a023ecfedbaa39a6931d41842cd73eb6c3f9592 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 17:39:47 -0400 Subject: [PATCH 30/49] adds testing functionality test wave 5 --- tests/test_wave_05.py | 74 ++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/tests/test_wave_05.py b/tests/test_wave_05.py index aee7c52a1..e580e3040 100644 --- a/tests/test_wave_05.py +++ b/tests/test_wave_05.py @@ -1,7 +1,8 @@ +from app.models.goal import Goal import pytest -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_goals_no_saved_goals(client): # Act response = client.get("/goals") @@ -12,7 +13,7 @@ def test_get_goals_no_saved_goals(client): assert response_body == [] -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_goals_one_saved_goal(client, one_goal): # Act response = client.get("/goals") @@ -29,7 +30,7 @@ def test_get_goals_one_saved_goal(client, one_goal): ] -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_goal(client, one_goal): # Act response = client.get("/goals/1") @@ -46,22 +47,24 @@ def test_get_goal(client, one_goal): } -@pytest.mark.skip(reason="test to be completed by student") +# @pytest.mark.skip(reason="test to be completed by student") def test_get_goal_not_found(client): pass # Act response = client.get("/goals/1") response_body = response.get_json() - raise Exception("Complete test") + # raise Exception("Complete test") # Assert # ---- Complete Test ---- - # assertion 1 goes here - # assertion 2 goes here + assert response_body == { + "details": "Goal ID not found" + } + assert response.status_code == 404 # ---- Complete Test ---- -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_create_goal(client): # Act response = client.post("/goals", json={ @@ -80,34 +83,51 @@ def test_create_goal(client): } -@pytest.mark.skip(reason="test to be completed by student") +# @pytest.mark.skip(reason="test to be completed by student") def test_update_goal(client, one_goal): - raise Exception("Complete test") + # raise Exception("Complete test") # Act # ---- Complete Act Here ---- + response = client.put("/goals/1", json={ + "title": "Updated Goal Title", + }) + response_body = response.get_json() # Assert # ---- Complete Assertions Here ---- - # assertion 1 goes here - # assertion 2 goes here - # assertion 3 goes here + assert response.status_code == 200 + assert "goal" in response_body + assert response_body == { + "goal": { + "id": 1, + "title": "Updated Goal Title", + } + } + goal = Goal.query.get(1) + assert goal.title == "Updated Goal Title" # ---- Complete Assertions Here ---- -@pytest.mark.skip(reason="test to be completed by student") +# @pytest.mark.skip(reason="test to be completed by student") def test_update_goal_not_found(client): - raise Exception("Complete test") + # raise Exception("Complete test") # Act # ---- Complete Act Here ---- + response = client.put("/goals/1", json={ + "title": "Updated Goal Title" + }) + response_body = response.get_json() # Assert # ---- Complete Assertions Here ---- - # assertion 1 goes here - # assertion 2 goes here + assert response.status_code == 404 + assert response_body == { + "details": "Goal ID not found" + } # ---- Complete Assertions Here ---- -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_delete_goal(client, one_goal): # Act response = client.delete("/goals/1") @@ -119,32 +139,36 @@ def test_delete_goal(client, one_goal): assert response_body == { "details": 'Goal 1 "Build a habit of going outside daily" successfully deleted' } + assert Goal.query.get(1) == None # Check that the goal was deleted response = client.get("/goals/1") assert response.status_code == 404 - raise Exception("Complete test with assertion about response body") + # raise Exception("Complete test with assertion about response body") # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** -@pytest.mark.skip(reason="test to be completed by student") +# @pytest.mark.skip(reason="test to be completed by student") def test_delete_goal_not_found(client): - raise Exception("Complete test") + # raise Exception("Complete test") # Act # ---- Complete Act Here ---- - + response = client.delete("/goals/1") + response_body = response.get_json() # Assert # ---- Complete Assertions Here ---- - # assertion 1 goes here - # assertion 2 goes here + assert response.status_code == 404 + assert response_body == { + "details": "Goal ID not found" + } # ---- Complete Assertions Here ---- -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_create_goal_missing_title(client): # Act response = client.post("/goals", json={}) From f2d010c45b9d4a88f3f886f1304c4e332a7a87fa Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 19:14:14 -0400 Subject: [PATCH 31/49] creates one to many relationship Task model --- app/models/task.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/models/task.py b/app/models/task.py index 569493509..6bba797fa 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -6,3 +6,22 @@ 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")) + goal = db.relationship("Goal", back_populates ="tasks") + + # @classmethod + # def from_dict(cls, task_data): + # new_task = Task( + # title = task_data["title"], + # description = task_data["description"], + # ) + # return new_task + + # def to_dict(self): + # task_as_dict = {} + # task_as_dict["id"] = self.task_id + # task_as_dict["title"] = self.title + # task_as_dict["description"] = self.description + # task_as_dict["is_complete"] = True if self.completed_at else False + + # return task_as_dict \ No newline at end of file From bf06968efe125b2a10739058225f4d8f95bec5d7 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 19:14:44 -0400 Subject: [PATCH 32/49] creates one to many relationship Goal model --- app/models/goal.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/goal.py b/app/models/goal.py index 1263d52fc..3b6fbb5a1 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -4,3 +4,4 @@ class Goal(db.Model): goal_id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String) + tasks = db.relationship("Task", back_populates="goals", lazy = True) From d0cdaa9bd08da652002ec41ef370844b998ebf30 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 19:16:09 -0400 Subject: [PATCH 33/49] updated migration for one to many relationship db --- migrations/versions/f867c5b0eafb_.py | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 migrations/versions/f867c5b0eafb_.py diff --git a/migrations/versions/f867c5b0eafb_.py b/migrations/versions/f867c5b0eafb_.py new file mode 100644 index 000000000..b49e20f56 --- /dev/null +++ b/migrations/versions/f867c5b0eafb_.py @@ -0,0 +1,30 @@ +"""empty message + +Revision ID: f867c5b0eafb +Revises: d3340559e8b2 +Create Date: 2023-05-11 19:08:50.378057 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f867c5b0eafb' +down_revision = 'd3340559e8b2' +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 ### From 7d310b4d8df5287424f8c4262e5aa4b4d1c65ad2 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 20:03:14 -0400 Subject: [PATCH 34/49] updated goal model --- app/models/goal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/goal.py b/app/models/goal.py index 3b6fbb5a1..2674853d4 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -4,4 +4,4 @@ class Goal(db.Model): goal_id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String) - tasks = db.relationship("Task", back_populates="goals", lazy = True) + tasks = db.relationship("Task", back_populates="goal", lazy = True) From 0c39ebb020bb92320e0283228c683bd0eb68a6ef Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 20:03:32 -0400 Subject: [PATCH 35/49] updated task model --- app/models/task.py | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/app/models/task.py b/app/models/task.py index 6bba797fa..f92743ef1 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -6,22 +6,5 @@ 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")) + goal_id = db.Column(db.Integer, db.ForeignKey("goal.goal_id"), nullable=True) goal = db.relationship("Goal", back_populates ="tasks") - - # @classmethod - # def from_dict(cls, task_data): - # new_task = Task( - # title = task_data["title"], - # description = task_data["description"], - # ) - # return new_task - - # def to_dict(self): - # task_as_dict = {} - # task_as_dict["id"] = self.task_id - # task_as_dict["title"] = self.title - # task_as_dict["description"] = self.description - # task_as_dict["is_complete"] = True if self.completed_at else False - - # return task_as_dict \ No newline at end of file From fb7826026bf562b3198c6187caf6debe6a95b189 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Thu, 11 May 2023 21:54:36 -0400 Subject: [PATCH 36/49] adds task to goal route --- app/goal_routes.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/goal_routes.py b/app/goal_routes.py index 6e6e1208c..4ef38ab16 100644 --- a/app/goal_routes.py +++ b/app/goal_routes.py @@ -1,6 +1,7 @@ from app import db from app.models.goal import Goal from flask import Blueprint, jsonify, make_response, abort, request +from app.task_routes import validate_task goals_bp = Blueprint("goals'", __name__, url_prefix = "/goals") @@ -31,6 +32,27 @@ def create_goal(): }, 201 ) +@goals_bp.route("//tasks", methods=["POST"]) +def add_task_to_goal(goal_id): + goal = validate_goal(goal_id) + + request_body = request.get_json() + task_ids = request_body.get("task_ids") + + for task_id in task_ids: + task = validate_task(task_id) + task.goal_id = goal.goal_id + + db.session.commit() + + return make_response( + { + "id": goal.goal_id, + "task_ids": task_ids + } + ), 200 + + @goals_bp.route("", methods=["GET"]) def read_goals(): goals = Goal.query.all() From 0250f48c541863176dc118c87277bf6c81dbec8b Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Fri, 12 May 2023 10:14:54 -0400 Subject: [PATCH 37/49] adds assertion test wave 6 --- tests/test_wave_06.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/test_wave_06.py b/tests/test_wave_06.py index 8afa4325e..ea0a254f1 100644 --- a/tests/test_wave_06.py +++ b/tests/test_wave_06.py @@ -2,7 +2,7 @@ import pytest -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_post_task_ids_to_goal(client, one_goal, three_tasks): # Act response = client.post("/goals/1/tasks", json={ @@ -23,7 +23,7 @@ def test_post_task_ids_to_goal(client, one_goal, three_tasks): assert len(Goal.query.get(1).tasks) == 3 -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_post_task_ids_to_goal_already_with_goals(client, one_task_belongs_to_one_goal, three_tasks): # Act response = client.post("/goals/1/tasks", json={ @@ -42,7 +42,7 @@ def test_post_task_ids_to_goal_already_with_goals(client, one_task_belongs_to_on assert len(Goal.query.get(1).tasks) == 2 -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_tasks_for_specific_goal_no_goal(client): # Act response = client.get("/goals/1/tasks") @@ -50,14 +50,15 @@ def test_get_tasks_for_specific_goal_no_goal(client): # Assert assert response.status_code == 404 + assert response_body == {'details': 'Goal ID not found'} - raise Exception("Complete test with assertion about response body") + # raise Exception("Complete test with assertion about response body") # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_tasks_for_specific_goal_no_tasks(client, one_goal): # Act response = client.get("/goals/1/tasks") @@ -74,7 +75,7 @@ def test_get_tasks_for_specific_goal_no_tasks(client, one_goal): } -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_tasks_for_specific_goal(client, one_task_belongs_to_one_goal): # Act response = client.get("/goals/1/tasks") @@ -99,7 +100,7 @@ def test_get_tasks_for_specific_goal(client, one_task_belongs_to_one_goal): } -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_get_task_includes_goal_id(client, one_task_belongs_to_one_goal): response = client.get("/tasks/1") response_body = response.get_json() From 96633d9e0336695cd9609dfc41897d92dd6f43c0 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Fri, 12 May 2023 10:57:24 -0400 Subject: [PATCH 38/49] updated comments --- app/task_routes.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/task_routes.py b/app/task_routes.py index 0eadf0dd2..8ef5f7c39 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -1,5 +1,6 @@ from app import db from app.models.task import Task +from app.models.goal import Goal from flask import Blueprint, jsonify, make_response, abort, request import datetime import requests @@ -7,6 +8,7 @@ tasks_bp = Blueprint("tasks", __name__, url_prefix = "/tasks") +###### Create Route ###### @tasks_bp.route("", methods=["POST"]) def create_task(): request_body = request.get_json() @@ -37,6 +39,8 @@ def create_task(): }, 201 ) +###### Read Route ###### + @tasks_bp.route("", methods=["GET"]) def read_tasks(): @@ -65,15 +69,27 @@ def read_tasks(): def read_one_saved_task(task_id): task = validate_task(task_id) - return { + if task.goal_id: + return { "task": { "id": task.task_id, "title": task.title, "description": task.description, - "is_complete": True if task.completed_at else False + "is_complete": True if task.completed_at else False, + "goal_id": task.goal_id + } + } + else: + return { + "task": { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": True if task.completed_at else False, } } +###### Update Route ###### @tasks_bp.route("/", methods=["PUT"]) def update_task(task_id): task = validate_task(task_id) @@ -96,6 +112,7 @@ def update_task(task_id): }, 200 ) +###### Patch Route ###### @tasks_bp.route("//mark_complete", methods=["PATCH"]) def mark_task_complete(task_id): task = validate_task(task_id) @@ -154,6 +171,8 @@ def mark_task_incomplete(task_id): }, 200 ) +###### Delete Route ###### + @tasks_bp.route("/", methods=["DELETE"]) def delete_task(task_id): task = validate_task(task_id) From 9e5c1d6ae30bd08b946d99c14005416e5f5f8d6a Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Fri, 12 May 2023 10:59:13 -0400 Subject: [PATCH 39/49] adds get tasks of one goal route --- app/goal_routes.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/app/goal_routes.py b/app/goal_routes.py index 4ef38ab16..b606504dc 100644 --- a/app/goal_routes.py +++ b/app/goal_routes.py @@ -1,6 +1,8 @@ from app import db from app.models.goal import Goal +from app.models.task import Task from flask import Blueprint, jsonify, make_response, abort, request +import requests from app.task_routes import validate_task goals_bp = Blueprint("goals'", __name__, url_prefix = "/goals") @@ -37,9 +39,8 @@ def add_task_to_goal(goal_id): goal = validate_goal(goal_id) request_body = request.get_json() - task_ids = request_body.get("task_ids") - for task_id in task_ids: + for task_id in request_body["task_ids"]: task = validate_task(task_id) task.goal_id = goal.goal_id @@ -48,7 +49,7 @@ def add_task_to_goal(goal_id): return make_response( { "id": goal.goal_id, - "task_ids": task_ids + "task_ids": request_body["task_ids"] } ), 200 @@ -79,6 +80,27 @@ def read_one_saved_goal(goal_id): } } +@goals_bp.route("//tasks", methods=["GET"]) +def get_tasks_of_one_goal(goal_id): + goal = validate_goal(goal_id) + + tasks_response = [] + for task in goal.tasks: + tasks_response.append({ + "id": task.task_id, + "goal_id": goal.goal_id, + "title": task.title, + "description": task.description, + "is_complete": bool(task.completed_at) + }) + + + return jsonify({ + "id": goal.goal_id, + "title": goal.title, + "tasks": tasks_response + }), 200 + @goals_bp.route("/", methods=["PUT"]) def update_goal(goal_id): goal = validate_goal(goal_id) From 17fdc6643eae4c430a58e6521141d56fc3a30a1f Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Fri, 12 May 2023 12:40:20 -0400 Subject: [PATCH 40/49] adds validate_model helper function --- app/task_routes.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/app/task_routes.py b/app/task_routes.py index 8ef5f7c39..170381c05 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -67,7 +67,7 @@ def read_tasks(): @tasks_bp.route("/", methods=["GET"]) def read_one_saved_task(task_id): - task = validate_task(task_id) + task = validate_model(Task, task_id) if task.goal_id: return { @@ -92,7 +92,7 @@ def read_one_saved_task(task_id): ###### Update Route ###### @tasks_bp.route("/", methods=["PUT"]) def update_task(task_id): - task = validate_task(task_id) + task = validate_model(Task, task_id) request_body = request.get_json() @@ -115,7 +115,7 @@ def update_task(task_id): ###### Patch Route ###### @tasks_bp.route("//mark_complete", methods=["PATCH"]) def mark_task_complete(task_id): - task = validate_task(task_id) + task = validate_model(Task, task_id) request_body = request.get_json() @@ -152,7 +152,7 @@ def mark_task_complete(task_id): @tasks_bp.route("//mark_incomplete", methods=["PATCH"]) def mark_task_incomplete(task_id): - task = validate_task(task_id) + task = validate_model(Task, task_id) request_body = request.get_json() @@ -175,7 +175,7 @@ def mark_task_incomplete(task_id): @tasks_bp.route("/", methods=["DELETE"]) def delete_task(task_id): - task = validate_task(task_id) + task = validate_model(Task, task_id) db.session.delete(task) db.session.commit() @@ -186,13 +186,15 @@ def delete_task(task_id): }, 200 ) -def validate_task(task_id): - task = Task.query.get(task_id) +def validate_model(cls, model_id): + try: + model_id = int(model_id) + except: + abort(make_response({"details":f"Invalid data"}, 400)) - if not task: - abort(make_response( - { - "details": "Task ID not found" - }, 404 - )) - return task \ No newline at end of file + model = cls.query.get(model_id) + + if not model: + abort(make_response({"details":f"{cls.__name__} ID not found"}, 404)) + + return model \ No newline at end of file From 6377c0abb4399a1b7de50e8e4e10b802d7c3f416 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Fri, 12 May 2023 12:41:19 -0400 Subject: [PATCH 41/49] deletes validate_goal function and updates routes with validate_model function --- app/goal_routes.py | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/app/goal_routes.py b/app/goal_routes.py index b606504dc..cbe4a17ab 100644 --- a/app/goal_routes.py +++ b/app/goal_routes.py @@ -3,7 +3,7 @@ from app.models.task import Task from flask import Blueprint, jsonify, make_response, abort, request import requests -from app.task_routes import validate_task +from app.task_routes import validate_model goals_bp = Blueprint("goals'", __name__, url_prefix = "/goals") @@ -36,12 +36,12 @@ def create_goal(): @goals_bp.route("//tasks", methods=["POST"]) def add_task_to_goal(goal_id): - goal = validate_goal(goal_id) + goal = validate_model(Goal, goal_id) request_body = request.get_json() for task_id in request_body["task_ids"]: - task = validate_task(task_id) + task = validate_model(Task, task_id) task.goal_id = goal.goal_id db.session.commit() @@ -71,7 +71,7 @@ def read_goals(): @goals_bp.route("/", methods=["GET"]) def read_one_saved_goal(goal_id): - goal = validate_goal(goal_id) + goal = validate_model(Goal, goal_id) return { "goal": { @@ -82,7 +82,7 @@ def read_one_saved_goal(goal_id): @goals_bp.route("//tasks", methods=["GET"]) def get_tasks_of_one_goal(goal_id): - goal = validate_goal(goal_id) + goal = validate_model(Goal, goal_id) tasks_response = [] for task in goal.tasks: @@ -103,7 +103,7 @@ def get_tasks_of_one_goal(goal_id): @goals_bp.route("/", methods=["PUT"]) def update_goal(goal_id): - goal = validate_goal(goal_id) + goal = validate_model(Goal, goal_id) request_body = request.get_json() @@ -122,7 +122,7 @@ def update_goal(goal_id): @goals_bp.route("/", methods=["DELETE"]) def delete_goal(goal_id): - goal = validate_goal(goal_id) + goal = validate_model(Goal, goal_id) db.session.delete(goal) db.session.commit() @@ -131,16 +131,4 @@ def delete_goal(goal_id): { "details": f"Goal {goal.goal_id} \"{goal.title}\" successfully deleted" }, 200 - ) - - -def validate_goal(goal_id): - goal = Goal.query.get(goal_id) - - if not goal: - abort(make_response( - { - "details": "Goal ID not found" - }, 404 - )) - return goal \ No newline at end of file + ) \ No newline at end of file From 24c223af5fe22f384cbbfab962e8f3cc85b38b7a Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Fri, 12 May 2023 14:34:44 -0400 Subject: [PATCH 42/49] refactored goal routes --- app/goal_routes.py | 48 ++++++++++------------------------------------ 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/app/goal_routes.py b/app/goal_routes.py index cbe4a17ab..9b41ff976 100644 --- a/app/goal_routes.py +++ b/app/goal_routes.py @@ -18,21 +18,14 @@ def create_goal(): }, 400 )) - goal = Goal( + new_goal = Goal( title = request_body["title"], ) - db.session.add(goal) + db.session.add(new_goal) db.session.commit() - return make_response( - { - "goal": { - "id": goal.goal_id, - "title": goal.title, - } - }, 201 - ) + return make_response({"goal": new_goal.to_dict()}, 201) @goals_bp.route("//tasks", methods=["POST"]) def add_task_to_goal(goal_id): @@ -60,24 +53,16 @@ def read_goals(): goals_response = [] for goal in goals: - goals_response.append( - { - "id": goal.goal_id, - "title": goal.title, - } - ), 200 + goals_response.append(goal.to_dict()) - return jsonify(goals_response) + return jsonify(goals_response), 200 @goals_bp.route("/", methods=["GET"]) def read_one_saved_goal(goal_id): goal = validate_model(Goal, goal_id) return { - "goal": { - "id": goal.goal_id, - "title": goal.title, - } + "goal": goal.to_dict() } @goals_bp.route("//tasks", methods=["GET"]) @@ -86,15 +71,8 @@ def get_tasks_of_one_goal(goal_id): tasks_response = [] for task in goal.tasks: - tasks_response.append({ - "id": task.task_id, - "goal_id": goal.goal_id, - "title": task.title, - "description": task.description, - "is_complete": bool(task.completed_at) - }) - - + tasks_response.append(task.to_dict()) + return jsonify({ "id": goal.goal_id, "title": goal.title, @@ -111,14 +89,8 @@ def update_goal(goal_id): db.session.commit() - return make_response( - { - "goal": { - "id": goal.goal_id, - "title": goal.title, - } - }, 200 - ) + return make_response({"goal": goal.to_dict()}), 200 + @goals_bp.route("/", methods=["DELETE"]) def delete_goal(goal_id): From d808524e5f433904519e13173ec2e65f0998b0b0 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Fri, 12 May 2023 14:35:17 -0400 Subject: [PATCH 43/49] adds two class methods --- app/models/goal.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/models/goal.py b/app/models/goal.py index 2674853d4..10661e2f6 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -5,3 +5,17 @@ class Goal(db.Model): goal_id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String) tasks = db.relationship("Task", back_populates="goal", lazy = True) + + def to_dict(self): + + return { + "id": self.goal_id, + "title": self.title, + } + + @classmethod + def from_dict(cls, goal_data): + new_goal = Goal( + title = goal_data["title"], + ) + return new_goal From 6130e9d624bd36b014b6e509a031d54a5ccdccb5 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Fri, 12 May 2023 14:35:53 -0400 Subject: [PATCH 44/49] adds two class methods --- app/models/task.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/app/models/task.py b/app/models/task.py index f92743ef1..341e150fe 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -8,3 +8,30 @@ class Task(db.Model): completed_at = db.Column(db.DateTime, nullable=True) goal_id = db.Column(db.Integer, db.ForeignKey("goal.goal_id"), nullable=True) goal = db.relationship("Goal", back_populates ="tasks") + + def to_dict(self): + if self.goal_id: + return { + "id": self.task_id, + "title": self.title, + "goal_id": self.goal_id, + "description": self.description, + "is_complete": bool(self.completed_at) + } + + else: + return { + "id": self.task_id, + "title": self.title, + "description": self.description, + "is_complete": bool(self.completed_at) + } + + @classmethod + def from_dict(cls, task_data): + new_task = Task( + title = task_data["title"], + description = task_data["description"], + ) + return new_task + From 15d02ecb38b1fb7ce6c3861feaddcf388bbcc22d Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Fri, 12 May 2023 14:36:21 -0400 Subject: [PATCH 45/49] refactored task routes --- app/task_routes.py | 82 +++++----------------------------------------- 1 file changed, 8 insertions(+), 74 deletions(-) diff --git a/app/task_routes.py b/app/task_routes.py index 170381c05..f1e2e23fa 100644 --- a/app/task_routes.py +++ b/app/task_routes.py @@ -20,24 +20,12 @@ def create_task(): }, 400 )) - task = Task( - title = request_body["title"], - description = request_body["description"], - ) + task = Task.from_dict(request_body) db.session.add(task) db.session.commit() - return make_response( - { - "task": { - "id": task.task_id, - "title": task.title, - "description": task.description, - "is_complete": False if not task.completed_at else True - } - }, 201 - ) + return jsonify({"task": task.to_dict()}), 201 ###### Read Route ###### @@ -54,14 +42,7 @@ def read_tasks(): tasks_response = [] for task in tasks: - tasks_response.append( - { - "id": task.task_id, - "title": task.title, - "description": task.description, - "is_complete": True if task.completed_at else False - } - ), 200 + tasks_response.append(task.to_dict()), 200 return jsonify(tasks_response) @@ -69,26 +50,8 @@ def read_tasks(): def read_one_saved_task(task_id): task = validate_model(Task, task_id) - if task.goal_id: - return { - "task": { - "id": task.task_id, - "title": task.title, - "description": task.description, - "is_complete": True if task.completed_at else False, - "goal_id": task.goal_id - } - } - else: - return { - "task": { - "id": task.task_id, - "title": task.title, - "description": task.description, - "is_complete": True if task.completed_at else False, - } - } - + return {"task": task.to_dict()} + ###### Update Route ###### @tasks_bp.route("/", methods=["PUT"]) def update_task(task_id): @@ -101,16 +64,7 @@ def update_task(task_id): db.session.commit() - return make_response( - { - "task": { - "id": task.task_id, - "title": task.title, - "description": task.description, - "is_complete": True if task.completed_at else False - } - }, 200 - ) + return make_response({"task": task.to_dict()},200) ###### Patch Route ###### @tasks_bp.route("//mark_complete", methods=["PATCH"]) @@ -123,8 +77,6 @@ def mark_task_complete(task_id): db.session.commit() - - slack_channel_id = "C0581Q2TDGQ" slack_url = "https://slack.com/api/chat.postMessage" @@ -139,16 +91,7 @@ def mark_task_complete(task_id): slack_request = requests.post(slack_url, headers=headers, json=slack_data) print(slack_request.text) - return make_response( - { - "task": { - "id": task.task_id, - "title": task.title, - "description": task.description, - "is_complete": True - } - }, 200 - ) + return make_response({"task": task.to_dict()}), 200 @tasks_bp.route("//mark_incomplete", methods=["PATCH"]) def mark_task_incomplete(task_id): @@ -160,16 +103,7 @@ def mark_task_incomplete(task_id): db.session.commit() - return make_response( - { - "task": { - "id": task.task_id, - "title": task.title, - "description": task.description, - "is_complete": False - } - }, 200 - ) + return make_response({"task": task.to_dict()}, 200) ###### Delete Route ###### From 82f1005e33929a97bdac61c195a93c411a5f2f0d Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Mon, 15 May 2023 12:46:13 -0400 Subject: [PATCH 46/49] connecting render db --- app/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 64cb286ac..70e552fff 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -16,8 +16,9 @@ def create_app(test_config=None): app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False if test_config is None: - app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( - "SQLALCHEMY_DATABASE_URI") + # app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( + # "SQLALCHEMY_DATABASE_URI") + app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("RENDER_DATABASE_URI") else: app.config["TESTING"] = True app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( From cd04b4525400c8f28ddf4f2cd23a17f41bdc8496 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Tue, 20 Jun 2023 15:03:13 -0400 Subject: [PATCH 47/49] adds flask_cors --- app/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 70e552fff..b66eea8f9 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 from dotenv import load_dotenv import os @@ -13,6 +14,7 @@ def create_app(test_config=None): app = Flask(__name__) + app.config['CORS_HEADERS'] = 'Content-Type' app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False if test_config is None: From 8cd7817a8750df55e7e894815e00e1e2754b5eec Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Tue, 20 Jun 2023 15:10:49 -0400 Subject: [PATCH 48/49] update --- app/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index b66eea8f9..13609e2b8 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,7 +5,7 @@ from dotenv import load_dotenv import os - + db = SQLAlchemy() migrate = Migrate() From 2f7016953c9c311bd98de265371af6a5345f55c4 Mon Sep 17 00:00:00 2001 From: Megan Gonzalez Date: Tue, 20 Jun 2023 15:14:39 -0400 Subject: [PATCH 49/49] update requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 453f0ef6a..9f9912f7d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ chardet==4.0.0 click==7.1.2 Flask==1.1.2 Flask-Migrate==2.6.0 +Flask-Cors==3.0.10 Flask-SQLAlchemy==2.4.4 gunicorn==20.1.0 idna==2.10