-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
from .routes import tasks_bp | ||
app.register_blueprint(tasks_bp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make sure to also import and register |
||
|
||
# Register Blueprints here | ||
|
||
return app | ||
return app |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks good for |
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") | ||||||||||||||||||
|
||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"], | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"] | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||
return jsonify(new_task), 200 | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||
|
||||||||||||||||||
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) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be "is_complete"
Suggested change
|
||||||||||||||||||
}) | ||||||||||||||||||
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": | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"] | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't forget to add the same for |
||||||||||||||||||
db.session.commit() | ||||||||||||||||||
return make_response(f"Task #{task.task_id} has been updated.", 201) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you already imported this