forked from AdaGold/task-list-api
-
Notifications
You must be signed in to change notification settings - Fork 97
Cedar Katie's Task-List-API #69
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
katiediaz
wants to merge
7
commits into
Ada-C16:master
Choose a base branch
from
katiediaz:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8779ec7
Passes wave 1
katiediaz 8a6e8d2
Wave 2 passed
katiediaz 3ca67fa
Wave 3 passed
katiediaz 8d384c3
Passes wave 4
katiediaz 5461f31
Wave 5 passes
katiediaz 75f098d
Passes wave 6
katiediaz 3a2e478
Added Procfile
katiediaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| web: gunicorn 'app:create_app()' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,95 @@ | ||||||
| from app.models.goal import Goal | ||||||
| from flask import jsonify | ||||||
| from flask import Blueprint, make_response, request, jsonify, abort | ||||||
| from app import db | ||||||
| from app.models.task import Task | ||||||
|
|
||||||
|
|
||||||
| #helper functions | ||||||
| goal_bp = Blueprint("goal", __name__,url_prefix="/goals") | ||||||
| def valid_int(number, parameter_type): | ||||||
| try: | ||||||
| int(number) | ||||||
| except: | ||||||
| abort(make_response({"error": f"{parameter_type} must be an int"})), 400 | ||||||
|
|
||||||
| def get_goal_from_id(goal_id): | ||||||
| valid_int(goal_id, "goal_id") | ||||||
| return Goal.query.get_or_404(goal_id, description="{goal not found}") | ||||||
| # get all goal | ||||||
|
|
||||||
| @goal_bp.route("", methods=["GET", "POST"]) | ||||||
| def handle_goals(): | ||||||
| if request.method == "GET": | ||||||
| goals = Goal.query.all() | ||||||
| goals_response = [] | ||||||
| for goal in goals: | ||||||
| goal = goal.to_dict() | ||||||
| goals_response.append(goal) | ||||||
| return jsonify(goals_response), 200 | ||||||
|
|
||||||
| #write query to fetch all goals | ||||||
|
|
||||||
|
|
||||||
| elif request.method == "POST": | ||||||
| request_body = request.get_json() | ||||||
| if not "title" in request_body: | ||||||
| return jsonify({"details":"Invalid data"}), 400 | ||||||
| new_goal = Goal(title=request_body["title"]) | ||||||
|
|
||||||
| db.session.add(new_goal) | ||||||
| db.session.commit() | ||||||
| return jsonify({"goal": new_goal.to_dict()}), 201 | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| @goal_bp.route("/<goal_id>", methods=["GET", "PUT", "DELETE"]) | ||||||
| def handle_goal(goal_id): | ||||||
| goal_id = int(goal_id) | ||||||
| goal = Goal.query.get(goal_id) | ||||||
| if goal is None: | ||||||
| return make_response("", 404) | ||||||
|
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. It's helpful to provide detailed error messages for debugging:
Suggested change
|
||||||
| if request.method == "GET": | ||||||
|
|
||||||
| return jsonify({"goal": goal.to_dict()}), 200 | ||||||
|
|
||||||
| elif request.method == "PUT": | ||||||
| request_body = request.get_json() | ||||||
|
|
||||||
| goal.title = request_body["title"] | ||||||
|
|
||||||
| db.session.commit() | ||||||
| return jsonify({"goal": goal.to_dict()}), 200 | ||||||
|
|
||||||
| elif request.method == "DELETE": | ||||||
| db.session.delete(goal) | ||||||
| db.session.commit() | ||||||
| return jsonify({"details":f'Goal {goal.goal_id} "{goal.title}" successfully deleted'}), 200 | ||||||
|
|
||||||
| ###WAVE 6 routes### | ||||||
| @goal_bp.route("/<goal_id>/tasks", methods=["POST"]) | ||||||
| def create_one_to_many(goal_id): | ||||||
| request_body = request.get_json() | ||||||
| goal = Goal.query.get(goal_id) | ||||||
| task_ids = request_body["task_ids"] | ||||||
| for task_id in task_ids: | ||||||
| task=Task.query.get(task_id) | ||||||
| goal.tasks.append(task) #list of task objects | ||||||
|
|
||||||
| db.session.commit() | ||||||
| return jsonify({"id": goal.goal_id, | ||||||
| "task_ids":[task.task_id for task in goal.tasks]}), 200 | ||||||
|
|
||||||
| @goal_bp.route("/<goal_id>/tasks", methods=["GET"]) | ||||||
| def get_task_for_goal(goal_id): | ||||||
| # request_body = request.get_json() | ||||||
| goal = Goal.query.get(goal_id) | ||||||
| if goal is None: | ||||||
| return make_response("", 404) | ||||||
|
|
||||||
| db.session.commit() | ||||||
| return jsonify({ | ||||||
| "id": goal.goal_id, | ||||||
| "title": goal.title, | ||||||
| "tasks": [task.to_dict() for task in goal.tasks] | ||||||
| }), 200 | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| from app.models.task import Task | ||
| from flask import jsonify | ||
| from flask import Blueprint, make_response, request, jsonify, abort | ||
| from app import db, SLACK_TOKEN | ||
| from datetime import datetime | ||
| import requests | ||
|
|
||
|
|
||
| #helper functions | ||
| task_bp = Blueprint("task", __name__,url_prefix="/tasks") | ||
| def valid_int(number, parameter_type): | ||
| try: | ||
| int(number) | ||
| except: | ||
| abort(make_response({"error": f"{parameter_type} must be an int"})), 400 | ||
|
|
||
| def get_task_from_id(task_id): | ||
| valid_int(task_id, "task_id") | ||
| return Task.query.get_or_404(task_id, description="{task not found}") | ||
| # get all tasks | ||
|
|
||
| @task_bp.route("", methods=["GET", "POST"]) | ||
| def handle_tasks(): | ||
| if request.method == "GET": | ||
|
|
||
| #write query to fetch all tasks | ||
| sort_query = request.args.get("sort") ###WAVE 2### | ||
|
|
||
| if sort_query == "asc": | ||
| tasks = Task.query.order_by(Task.title.asc()) | ||
| elif sort_query == "desc": | ||
| tasks = Task.query.order_by(Task.title.desc()) | ||
| else: | ||
| tasks = Task.query.all() | ||
| tasks_response = [task.to_dict() for task in tasks] | ||
| return jsonify(tasks_response), 200 | ||
|
|
||
| elif request.method == "POST": | ||
| request_body = request.get_json() | ||
| if not "title" in request_body or not "description" in request_body or not "completed_at" in request_body: | ||
| return jsonify({"details":"Invalid data"}), 400 | ||
| new_task = Task(title=request_body["title"], | ||
| description=request_body["description"], | ||
| completed_at=request_body["completed_at"], | ||
|
|
||
| ) | ||
|
|
||
| db.session.add(new_task) | ||
| db.session.commit() | ||
| return jsonify({"task": new_task.to_dict()}), 201 | ||
|
|
||
|
|
||
|
|
||
| @task_bp.route("/<task_id>", methods=["GET", "PUT", "DELETE"]) | ||
| def handle_task(task_id): | ||
| task_id = int(task_id) | ||
| task = Task.query.get(task_id) | ||
| if task is None: | ||
| return make_response("", 404) | ||
| if request.method == "GET": | ||
|
|
||
| return jsonify({"task": task.to_dict()}), 200 | ||
|
|
||
| elif request.method == "PUT": | ||
| request_body = request.get_json() | ||
|
|
||
| task.title = request_body["title"] | ||
| task.description = request_body["description"] | ||
|
|
||
| db.session.commit() | ||
| return jsonify({"task": task.to_dict()}), 200 | ||
|
|
||
| elif request.method == "DELETE": | ||
| db.session.delete(task) | ||
| db.session.commit() | ||
| return jsonify({"details":f'Task {task.task_id} "{task.title}" successfully deleted'}), 200 | ||
|
|
||
| ##WAVE 4 Slack Helper Function### | ||
| def post_complete_task_to_slack(task): | ||
| url = "https://slack.com/api/chat.postMessage" | ||
| message = f"Someone just completed the task {task.title}" | ||
| query_params = { | ||
| "token": SLACK_TOKEN, | ||
| "channel": 'task-list-api', | ||
| "text" : message | ||
| } | ||
| return requests.post(url, data=query_params).json() | ||
|
|
||
| ##wave 3 complete/incomplete## | ||
| @task_bp.route("/<task_id>/mark_complete", methods=["PATCH"]) | ||
| def update_task_completion(task_id): | ||
| task= get_task_from_id(task_id) | ||
| task.is_complete=True | ||
| task.completed_at = datetime.now() | ||
| db.session.commit() | ||
| post_complete_task_to_slack(task) | ||
| return jsonify({"task": task.to_dict()}), 200 | ||
|
|
||
|
|
||
| @task_bp.route("/<task_id>/mark_incomplete", methods=["PATCH"]) | ||
| def update_task_incomplete(task_id): | ||
| task= get_task_from_id(task_id) | ||
| task.is_complete=False | ||
| task.completed_at = None | ||
| db.session.commit() | ||
| return jsonify({"task": task.to_dict()}), 200 | ||
|
|
||
| # @task_bp.route("/<task_id>", methods=["GET"]) | ||
| # def handle_task(task_id): | ||
| # task_id = int(task_id) | ||
| # task = Task.query.get(task_id) | ||
| # if not task: | ||
| # return make_response(f"Task {task_id} Bad data", 400) | ||
|
|
||
| # if request.method == GET | ||
|
|
||
| # for task in tasks: | ||
| # if task.id == task_id: | ||
| # return vars(task) | ||
|
|
||
| # return "Not found", 404 | ||
|
Comment on lines
+108
to
+121
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. Style: Clean up commented out code. |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Generic single-database configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I like that you included a parameter name in the message!