forked from AdaGold/task-list-api
-
Notifications
You must be signed in to change notification settings - Fork 97
Pine-Tirhas G #72
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
tirhas20
wants to merge
11
commits into
Ada-C16:master
Choose a base branch
from
tirhas20: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
Pine-Tirhas G #72
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ef2d89f
test wave 01 and test wave 02 passed
tirhas20 12ae947
test wave 03 passed
tirhas20 77555c6
adds goal model and POST routecreated
tirhas20 c4ea281
test wave 05 passed
tirhas20 348a86b
test wave 06 passed
tirhas20 fd09bf0
adds slack notification helper function
tirhas20 70a248d
adds slack-sdk-3.11.2 to requirement.txt
tirhas20 af725da
adds procfile
tirhas20 a6a4998
add requirements
tirhas20 2d88c08
modify slack api
tirhas20 e89bb8c
requirements updated
tirhas20 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,31 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| from flask import current_app | ||||||||||||||||||||||||||||||||||||||||||||||||
| from app import db | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| class Task(db.Model): | ||||||||||||||||||||||||||||||||||||||||||||||||
| task_id = db.Column(db.Integer, primary_key=True) | ||||||||||||||||||||||||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||||||||||||||||||||||||
| goal_id = db.Column(db.Integer, db.ForeignKey('goal.id')) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| def check_for_completed_task(self): | ||||||||||||||||||||||||||||||||||||||||||||||||
| if self.completed_at: | ||||||||||||||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| def to_dict(self): | ||||||||||||||||||||||||||||||||||||||||||||||||
| if self.goal_id is None: | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return {"id":self.id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| "title":self.title, | ||||||||||||||||||||||||||||||||||||||||||||||||
| "description":self.description, | ||||||||||||||||||||||||||||||||||||||||||||||||
| "is_complete":self.check_for_completed_task() | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||
| return {"id":self.id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| "title":self.title, | ||||||||||||||||||||||||||||||||||||||||||||||||
| "description":self.description, | ||||||||||||||||||||||||||||||||||||||||||||||||
| "is_complete":self.check_for_completed_task(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| "goal_id": self.goal_id | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+30
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 can be dried up a bit.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
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 |
|---|---|---|
| @@ -1,2 +1,157 @@ | ||
| from flask import Blueprint | ||
| from app import db | ||
| from app.models.task import Task | ||
| from flask import Blueprint, jsonify,request, make_response, abort | ||
| from datetime import date | ||
| from app.models.goal import Goal | ||
| import os | ||
| import requests | ||
| from dotenv import load_dotenv | ||
| from slack_sdk import WebClient | ||
| from slack_sdk.errors import SlackApiError | ||
|
|
||
|
|
||
| def valid_int(number,parameter_type): | ||
| try: | ||
| int(number) | ||
| except: | ||
| abort(make_response({"error":f"{parameter_type} must be an int"},400)) | ||
|
|
||
| def slack_notification(): | ||
| load_dotenv() | ||
| slack_token = os.environ["SLACK_TOKENS"] | ||
| client = WebClient(token=slack_token) | ||
| try: | ||
| response = client.chat_postMessage( | ||
| channel ="CNEEJDLAW", | ||
| text = "Task completed" | ||
| ) | ||
| except SlackApiError as e: | ||
| return jsonify({"Error": "chanel not found"}) | ||
|
Comment on lines
+19
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. Great helper function |
||
|
|
||
| tasks_bp = Blueprint("tasks",__name__,url_prefix="/tasks") | ||
| goals_bp = Blueprint("goals", __name__,url_prefix="/goals") | ||
|
|
||
| @tasks_bp.route("",methods=["GET"]) | ||
| def handle_tasks(): | ||
| sort_query = request.args.get("sort") | ||
| 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 =[] | ||
| for task in tasks: | ||
| tasks_response.append(task.to_dict()) | ||
| return jsonify(tasks_response),200 | ||
|
|
||
| @tasks_bp.route("/<task_id>",methods=["GET","put","DELETE"]) | ||
| def get_task(task_id): | ||
| valid_int(task_id, "task_id") | ||
| task = Task.query.get_or_404(task_id) | ||
| if request.method == "GET": | ||
| return jsonify({"task":task.to_dict()}),200 | ||
| elif request.method == "PUT": | ||
| request_body = request.get_json() | ||
| if "title" in request_body: | ||
| task.title = request_body["title"] | ||
| if "description" in request_body: | ||
| task.description = request_body["description"] | ||
| if "completed_at" in request_body: | ||
| task.completed_at = request_body["completed_at"] | ||
| 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_id} "{task.title}" successfully deleted'}),200 | ||
|
|
||
| @tasks_bp.route("",methods=["POST"]) | ||
| def create_task(): | ||
| request_body = request.get_json() | ||
| if 'title' not in request_body or 'description' not in request_body or\ | ||
| 'completed_at' not 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 | ||
|
|
||
| @tasks_bp.route("/<task_id>/mark_complete",methods=["PATCH"]) | ||
| def mark_complete_task(task_id): | ||
| valid_int(task_id, "task_id") | ||
| task = Task.query.get_or_404(task_id) | ||
| task.completed_at = date.today() | ||
| db.session.commit() | ||
| slack_notification() | ||
| return jsonify({"task":task.to_dict()}),200 | ||
|
|
||
| @tasks_bp.route("/<task_id>/mark_incomplete",methods=["PATCH"]) | ||
| def mark_incomplete_task(task_id): | ||
| valid_int(task_id, "task_id") | ||
| task = Task.query.get_or_404(task_id) | ||
| task.completed_at = None | ||
| db.session.commit() | ||
| return jsonify({"task":task.to_dict()}),200 | ||
|
|
||
| @goals_bp.route("", methods=["POST"]) | ||
| def create_goal(): | ||
| request_body = request.get_json() | ||
| if "title" not 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 | ||
|
|
||
| @goals_bp.route("", methods=["GET"]) | ||
| def handle_goals(): | ||
| goals = Goal.query.all() | ||
| goals_response = [] | ||
| for goal in goals: | ||
| goals_response.append(goal.to_dict()) | ||
| return jsonify(goals_response), 200 | ||
|
|
||
| @goals_bp.route("/<goal_id>", methods=["GET", "PUT","DELETE"]) | ||
| def get_goal(goal_id): | ||
| valid_int(goal_id,"goal_id") | ||
| goal = Goal.query.get_or_404(goal_id) | ||
| if request.method == "GET": | ||
| return jsonify({"goal":goal.to_dict()}),200 | ||
| elif request.method == "DELETE": | ||
| db.session.delete(goal) | ||
| db.session.commit() | ||
| return jsonify({"details":f"Goal {goal_id} \"{goal.title}\" successfully deleted"}) | ||
| elif request.method == "PUT": | ||
| request_body = request.get_json() | ||
| goal.title = request_body["title"] | ||
| db.session.commit() | ||
| return jsonify({"goal":goal.to_dict()}),200 | ||
|
|
||
| @goals_bp.route("/<goal_id>/tasks", methods=["POST"]) | ||
| def post_task_ids_to_goal(goal_id): | ||
| valid_int(goal_id,"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) | ||
| db.session.commit() | ||
| return jsonify({"id":goal.id, "task_ids": [task.id for task in goal.tasks]}),200 | ||
|
|
||
| @goals_bp.route("/<goal_id>/tasks", methods=["GET"]) | ||
| def get_tasks_for_goal(goal_id): | ||
| valid_int(goal_id,"goal_id") | ||
| goal = Goal.query.get_or_404(goal_id) | ||
| response_body = {"id":goal.id, | ||
| "title":goal.title, | ||
| "tasks":goal.task_lists() | ||
| } | ||
| print(response_body) | ||
| return jsonify(response_body),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 @@ | ||
| 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 these helper methods