-
Notifications
You must be signed in to change notification settings - Fork 111
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
Sea Turtles - Nina G. #117
base: master
Are you sure you want to change the base?
Changes from all commits
0f73d39
97882fa
a1ffe4d
4badd55
8b630aa
bb56a2f
a8b158c
45de7ec
f261b52
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
web: gunicorn 'app:create_app()' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from flask import Blueprint, jsonify, make_response, request | ||
from .models.goal import Goal | ||
from .routes_helper_functions import * | ||
from app import db | ||
|
||
goal_bp = Blueprint("goals", __name__, url_prefix="/goals") | ||
|
||
@goal_bp.route("", methods=("POST",)) | ||
def post_one_goal(): | ||
request_body = request.get_json() | ||
|
||
try: | ||
new_goal = Goal(title=request_body["title"]) | ||
except KeyError: | ||
error_message(f"Invalid data", 400) | ||
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. I would suggest adding explicit messaging for the user to help them understand what's missing in their request error_message("Invalid data: title is missing", 400) |
||
|
||
db.session.add(new_goal) | ||
db.session.commit() | ||
|
||
return jsonify(new_goal.one_goal_to_dict()), 201 | ||
|
||
@goal_bp.route("", methods=("GET",)) | ||
def get_goals(): | ||
|
||
goals = Goal.query.all() | ||
|
||
result_list = [goal.to_dict() for goal in goals] | ||
|
||
return jsonify(result_list), 200 | ||
|
||
@goal_bp.route("/<goal_id>", methods=("GET",)) | ||
def get_one_goal(goal_id): | ||
goal = validate_goal(goal_id) | ||
|
||
return jsonify(goal.one_goal_to_dict()), 200 | ||
|
||
@goal_bp.route("/<goal_id>", methods=("PUT",)) | ||
def put_one_goal(goal_id): | ||
goal = validate_goal(goal_id) | ||
|
||
request_body = request.get_json() | ||
|
||
goal.replace_details(request_body) | ||
|
||
db.session.commit() | ||
|
||
return jsonify(goal.one_goal_to_dict()), 200 | ||
|
||
@goal_bp.route("/<goal_id>", methods=("DELETE",)) | ||
def delete_one_goal(goal_id): | ||
goal = validate_goal(goal_id) | ||
|
||
db.session.delete(goal) | ||
db.session.commit() | ||
|
||
return make_response(jsonify(dict(details=f'Goal {goal.goal_id} "{goal.title}" successfully deleted'))), 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. great informative message here! |
||
|
||
@goal_bp.route("/<goal_id>/tasks", methods=["POST"]) | ||
def post_tasks_to_goal(goal_id): | ||
request_body = request.get_json() | ||
|
||
goal = validate_goal(goal_id) | ||
|
||
for task_id in request_body["task_ids"]: | ||
task = validate_task(task_id) | ||
task.goal = goal | ||
|
||
db.session.commit() | ||
|
||
return make_response(jsonify({"id": goal.goal_id, "task_ids": request_body["task_ids"]})), 200 | ||
|
||
@goal_bp.route("/<goal_id>/tasks", methods=["GET"]) | ||
def get_tasks_of_goal(goal_id): | ||
goal = validate_goal(goal_id) | ||
tasks_dict = [task.to_dict() for task in goal.tasks] | ||
|
||
result = goal.to_dict() | ||
result["tasks"] = tasks_dict | ||
|
||
return jsonify(result), 200 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,20 @@ | |
|
||
|
||
class Goal(db.Model): | ||
goal_id = db.Column(db.Integer, primary_key=True) | ||
goal_id = db.Column(db.Integer, primary_key=True, nullable=False) | ||
title = db.Column(db.String, nullable=False) | ||
tasks = db.relationship("Task", back_populates="goal") | ||
|
||
def to_dict(self): | ||
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. looks good! |
||
return dict( | ||
id = self.goal_id, | ||
title = self.title, | ||
) | ||
|
||
def one_goal_to_dict(self): | ||
result = {} | ||
result["goal"] = self.to_dict() | ||
return result | ||
|
||
def replace_details(self, data_dict): | ||
self.title = data_dict["title"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,31 @@ | ||
from app import db | ||
|
||
|
||
class Task(db.Model): | ||
task_id = db.Column(db.Integer, primary_key=True) | ||
task_id = db.Column(db.Integer, primary_key=True, nullable = False) | ||
title = db.Column(db.String, nullable = False) | ||
description = db.Column(db.String, nullable = False) | ||
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): | ||
task_dict = { | ||
"id": self.task_id, | ||
"title": self.title, | ||
"description": self.description, | ||
"is_complete": bool(self.completed_at) | ||
} | ||
|
||
if self.goal_id: | ||
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 way to handle the optional attribute |
||
task_dict["goal_id"] = self.goal_id | ||
|
||
return task_dict | ||
|
||
def one_task_to_dict(self): | ||
result = {} | ||
result["task"] = self.to_dict() | ||
return result | ||
|
||
def replace_details(self, data_dict): | ||
self.title = data_dict["title"] | ||
self.description = data_dict["description"] |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from flask import jsonify, abort, make_response | ||
from .models.task import Task | ||
from .models.goal import Goal | ||
import requests, os | ||
|
||
def error_message(message, status_code): | ||
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. The helper functions look good! I also like that you put them in a separate file. |
||
abort(make_response(jsonify(dict(details=message)), status_code)) | ||
|
||
def validate_task(task_id): | ||
try: | ||
task_id = int(task_id) | ||
except: | ||
error_message(f"task {task_id} invalid", 400) | ||
|
||
task = Task.query.get(task_id) | ||
|
||
if not task: | ||
error_message(f"task {task_id} not found", 404) | ||
|
||
return task | ||
|
||
def post_completion_message_in_slack(task_id): | ||
task = validate_task(task_id) | ||
|
||
SLACK_PATH = "https://slack.com/api/chat.postMessage" | ||
SLACK_API_KEY = os.environ.get("SLACK_BOT_TOKEN") | ||
|
||
request_headers = {"Authorization": f"Bearer {SLACK_API_KEY}"} | ||
request_body = { | ||
"channel": "task-notifications", | ||
"text": f"Someone just completed the task {task.title}" | ||
} | ||
|
||
requests.post(SLACK_PATH, headers=request_headers, json=request_body) | ||
|
||
def validate_goal(goal_id): | ||
try: | ||
goal_id = int(goal_id) | ||
except: | ||
error_message(f"goal {goal_id} invalid", 400) | ||
|
||
goal = Goal.query.get(goal_id) | ||
|
||
if not goal: | ||
error_message(f"goal {goal_id} not found", 404) | ||
|
||
return goal |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from flask import Blueprint, jsonify, make_response, request | ||
from .models.task import Task | ||
from .routes_helper_functions import * | ||
from app import db | ||
from datetime import datetime | ||
|
||
task_bp = Blueprint("tasks", __name__, url_prefix="/tasks") | ||
|
||
@task_bp.route("", methods=("POST",)) | ||
def post_one_task(): | ||
request_body = request.get_json() | ||
|
||
try: | ||
new_task = Task(title=request_body["title"], | ||
description=request_body["description"]) | ||
except KeyError: | ||
error_message(f"Invalid data", 400) | ||
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. Same suggestion as above about adding a more descriptive message for the user. |
||
|
||
if "completed_at" in request_body: | ||
new_task.completed_at = request_body["completed_at"] | ||
|
||
db.session.add(new_task) | ||
db.session.commit() | ||
|
||
return jsonify(new_task.one_task_to_dict()), 201 | ||
|
||
@task_bp.route("", methods=("GET",)) | ||
def get_tasks(): | ||
sort_query = request.args.get("sort") | ||
|
||
if sort_query == "asc": | ||
tasks = Task.query.order_by(Task.title).all() | ||
elif sort_query == "desc": | ||
tasks = Task.query.order_by(Task.title.desc()).all() | ||
else: | ||
tasks = Task.query.all() | ||
|
||
result_list = [task.to_dict() for task in tasks] | ||
|
||
return jsonify(result_list), 200 | ||
|
||
@task_bp.route("/<task_id>", methods=("GET",)) | ||
def get_one_task(task_id): | ||
task = validate_task(task_id) | ||
|
||
return jsonify(task.one_task_to_dict()), 200 | ||
|
||
@task_bp.route("/<task_id>", methods=("PUT",)) | ||
def put_one_task(task_id): | ||
task = validate_task(task_id) | ||
|
||
request_body = request.get_json() | ||
|
||
task.replace_details(request_body) | ||
|
||
db.session.commit() | ||
|
||
return jsonify(task.one_task_to_dict()), 200 | ||
|
||
@task_bp.route("/<task_id>", methods=("DELETE",)) | ||
def delete_one_task(task_id): | ||
task = validate_task(task_id) | ||
|
||
db.session.delete(task) | ||
db.session.commit() | ||
|
||
return make_response(jsonify(dict(details=f'Task {task.task_id} "{task.title}" successfully deleted'))), 200 | ||
|
||
@task_bp.route("/<task_id>/mark_complete", methods=("PATCH",)) | ||
def patch_one_task_as_complete(task_id): | ||
task = validate_task(task_id) | ||
task.completed_at = datetime.utcnow() | ||
|
||
db.session.commit() | ||
post_completion_message_in_slack(task_id) | ||
|
||
return jsonify(task.one_task_to_dict()), 200 | ||
|
||
@task_bp.route("/<task_id>/mark_incomplete", methods=("PATCH",)) | ||
def patch_one_task_as_incomplete(task_id): | ||
task = validate_task(task_id) | ||
task.completed_at = None | ||
|
||
db.session.commit() | ||
|
||
return jsonify(task.one_task_to_dict()), 200 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Generic single-database configuration. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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.
good idea to create two seperate files for goals and tasks