Skip to content

accelerate - lilia #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@
import os
from dotenv import load_dotenv


db = SQLAlchemy()
migrate = Migrate()
load_dotenv()


def create_app(test_config=None):
app = Flask(__name__)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

if test_config is None:
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"SQLALCHEMY_DATABASE_URI")
else:
app.config["TESTING"] = True
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"SQLALCHEMY_TEST_DATABASE_URI")

# Import models here for Alembic setup

from app.models.task import Task
from app.models.goal import Goal

db.init_app(app)
migrate.init_app(app, db)
from app.models.task import Task
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you already imported this

Suggested change
from app.models.task import Task

from .routes import tasks_bp
app.register_blueprint(tasks_bp)
Copy link

@tgoslee tgoslee Jun 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to also import and register goals_bp


# Register Blueprints here

return app
return app
5 changes: 4 additions & 1 deletion app/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,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, default=None)
Comment on lines 5 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks good for task.py go ahead and do the same for goal.py

70 changes: 70 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,72 @@
from flask import Blueprint
from app.models.task import Task
from app import db
from flask import request, Blueprint, make_response, jsonify

tasks_bp = Blueprint("tasks", __name__, url_prefix="/tasks")

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the same for goals_bp


@tasks_bp.route("", methods=["POST","GET"])
def handle_tasks():
if request.method == "POST":
request_body = request.get_json()
if "title" not in request_body or "description" not in request_body or "completed_at" not in request_body:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good check

return {"details": f"Invalid data"}, 400

new_task = Task(
id=request_body["id"],
title=request_body["title"],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need id here because it is created for you

description=request_body["description"],
completed_at=request_body["completed_at"]
Copy link

@tgoslee tgoslee Jun 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove completed at here and do your check then add it

Suggested change
completed_at=request_body["completed_at"]

)

db.session.add(new_task)
db.session.commit()

if new_task.completed_at == None:
new_task.completed_at = False
else:
new_task.completed_at = True
Comment on lines +26 to +29
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this above line 23 and edit it to add to the Task call. For example:

Suggested change
if new_task.completed_at == None:
new_task.completed_at = False
else:
new_task.completed_at = True
if "completed_at" in request_body:
new_task.completed_at = request_body["completed_at"]

return jsonify(new_task), 200
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this response, you want to make sure you are adding "task" before the response. So it should look like

Suggested change
return jsonify(new_task), 200
return jsonify({"task":new_task)}, 200


elif request.method == "GET":
tasks = Task.query.all()
tasks_response = []
for task in tasks:
tasks_response.append({
"id": task.task_id,
"title": task.title,
"description": task.description,
"completed_at": bool(task.completed_at)
Copy link

@tgoslee tgoslee Jun 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be "is_complete"

Suggested change
"completed_at": bool(task.completed_at)
"is_complete": bool(task.completed_at)

})
return jsonify(tasks), 200


@tasks_bp.route("/<task_id>", methods=["GET", "PUT", "DELETE"])
def handle_task(task_id):
task = Task.query.get(task_id)

if task is None:
return make_response("", 404)

if request.method == "GET":
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great!

return { "task":{
"id": task.task_id,
"title": task.title,
"description": task.description,
"is_complete": bool(task.completed_at)
}
}, 200

elif request.method == "PUT":
form_data = request.get_json()
task.title = form_data["title"]
task.description = form_data["description"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't forget to add the same for task.completed_at

db.session.commit()
return make_response(f"Task #{task.task_id} has been updated.", 201)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing return. Similar for the return for GET method


elif request.method == "DELETE":
db.session.delete(task)
db.session.commit()

return {"details":f"Task {task.task_id} \"{task.title}\" successfully deleted"}
Comment on lines +68 to +72
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great!