forked from AdaGold/task-list-api
-
Couldn't load subscription status.
- Fork 44
Dragonfly - Marina A. #37
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
MarinaAdams
wants to merge
13
commits into
Ada-C23:main
Choose a base branch
from
MarinaAdams:main
base: main
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
13 commits
Select commit
Hold shift + click to select a range
afca48e
Added task routes, route helper functions; validated models, tested w…
MarinaAdams 40e63a0
Completed Wave 2: Added sorting by title with query params. Passed tests
MarinaAdams 1e01873
Completed Wave 3: Added mark_complete and mark_incomplete route. All …
MarinaAdams eb67c68
Finished Wave 4: Edited task routes. Implemented Slack Bot. Refactore…
MarinaAdams 79b01af
Created Goal Model. Added migration and upgrade.
MarinaAdams def7518
Completed Wave: added goals CRUD. Changed tests. All tests passed.
MarinaAdams 667474d
Completed Wave 6: added one-to-many relationship bw goals and tasks. …
MarinaAdams 9fe6f7b
Added comments to all waves
MarinaAdams fcc8bb9
Resolved: removed unnecessary comments per review
MarinaAdams bf75dce
Fixed: white spacing, blank lines, imports according PEP8
MarinaAdams b9f21bc
Fixed: refactored models, routes, added helper functions; completed t…
MarinaAdams eef3ccc
Fixed: final refactoring
MarinaAdams 14d2e2a
N
MarinaAdams 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
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,8 @@ | ||
| from flask_sqlalchemy import SQLAlchemy | ||
| from flask_migrate import Migrate | ||
|
|
||
| from .models.base import Base | ||
|
|
||
|
|
||
| db = SQLAlchemy(model_class=Base) | ||
| migrate = Migrate() | ||
| migrate = Migrate() |
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,4 +1,5 @@ | ||
| from sqlalchemy.orm import DeclarativeBase | ||
|
|
||
|
|
||
| class Base(DeclarativeBase): | ||
| pass | ||
| pass |
MarinaAdams marked this conversation as resolved.
Show resolved
Hide resolved
|
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,5 +1,18 @@ | ||
| from sqlalchemy.orm import Mapped, mapped_column | ||
| from typing import List | ||
|
|
||
| from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
|
|
||
| from ..db import db | ||
|
|
||
|
|
||
| class Goal(db.Model): | ||
| id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) | ||
| title: Mapped[str] | ||
| tasks: Mapped[List["Task"]] = relationship(back_populates="goal") | ||
|
|
||
MarinaAdams marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def to_dict(self): | ||
| return {"id": self.id, "title": self.title} | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, data): | ||
| return cls(title=data["title"]) | ||
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,5 +1,36 @@ | ||
| from sqlalchemy.orm import Mapped, mapped_column | ||
| from datetime import datetime | ||
| from typing import Optional | ||
|
|
||
| from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
|
|
||
| from ..db import db | ||
|
|
||
|
|
||
| class Task(db.Model): | ||
| id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) | ||
| title: Mapped[str] | ||
| description: Mapped[str] | ||
| completed_at: Mapped[Optional[datetime]] | ||
| goal_id: Mapped[Optional[int]] = mapped_column(db.ForeignKey("goal.id")) | ||
| goal: Mapped[Optional["Goal"]] = relationship(back_populates="tasks") | ||
|
|
||
| def to_dict(self): | ||
| task_dict = { | ||
| "id": self.id, | ||
| "title": self.title, | ||
| "description": self.description, | ||
| "is_complete": self.completed_at is not None, | ||
| } | ||
|
|
||
| if self.goal_id: | ||
| task_dict["goal_id"] = self.goal_id | ||
|
|
||
| return task_dict | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, task_data): | ||
| return cls( | ||
| title=task_data["title"], | ||
| description=task_data["description"], | ||
| completed_at=task_data.get("completed_at"), | ||
| ) |
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 +1,68 @@ | ||
| from flask import Blueprint | ||
| from flask import Blueprint, request, Response | ||
|
|
||
| from app.models.goal import Goal | ||
| from app.models.task import Task | ||
| from .. import db | ||
| from .route_utilities import validate_model, create_model, get_models_with_filters | ||
MarinaAdams marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| bp = Blueprint("goals_bp", __name__, url_prefix="/goals") | ||
|
|
||
|
|
||
| @bp.post("") | ||
| def create_goal(): | ||
| request_body = request.get_json() | ||
| return create_model(Goal, request_body) | ||
|
|
||
|
|
||
| @bp.post("/<goal_id>/tasks") | ||
| def add_tasks_to_goal(goal_id): | ||
| goal = validate_model(Goal, goal_id) | ||
| request_body = request.get_json() | ||
| task_ids = request_body.get("task_ids", []) | ||
| goal.tasks = [] | ||
|
|
||
| for task_id in task_ids: | ||
| task = validate_model(Task, task_id) | ||
| goal.tasks.append(task) | ||
|
|
||
| db.session.commit() | ||
| return {"id": goal.id, "task_ids": task_ids} | ||
|
|
||
|
|
||
| @bp.get("/<goal_id>/tasks") | ||
| def get_tasks_of_goal(goal_id): | ||
| goal = validate_model(Goal, goal_id) | ||
| tasks = [task.to_dict() for task in goal.tasks] | ||
| return {"id": goal.id, "title": goal.title, "tasks": tasks} | ||
|
|
||
|
|
||
| @bp.get("") | ||
| def get_all_goals(): | ||
| return get_models_with_filters(Goal, request.args) | ||
|
|
||
|
|
||
| @bp.get("/<goal_id>") | ||
| def get_one_goal(goal_id): | ||
| goal = validate_model(Goal, goal_id) | ||
| return {"goal": goal.to_dict()} | ||
|
|
||
|
|
||
| @bp.put("/<goal_id>") | ||
| def update_goal(goal_id): | ||
| goal = validate_model(Goal, goal_id) | ||
| request_body = request.get_json() | ||
|
|
||
| if "title" not in request_body: | ||
| return {"details": "Invalid data. 'title' is required."}, 400 | ||
|
|
||
| goal.title = request_body["title"] | ||
MarinaAdams marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| db.session.commit() | ||
| return Response(status=204, mimetype="application/json") | ||
|
|
||
|
|
||
| @bp.delete("/<goal_id>") | ||
| def delete_goal(goal_id): | ||
| goal = validate_model(Goal, goal_id) | ||
| db.session.delete(goal) | ||
| db.session.commit() | ||
| return Response(status=204, mimetype="application/json") | ||
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,75 @@ | ||
| import os, requests | ||
| from flask import abort, make_response | ||
|
|
||
| from ..db import db | ||
|
|
||
|
|
||
| def validate_model(cls, model_id): | ||
| try: | ||
| model_id = int(model_id) | ||
| except: | ||
| response = {"details": f"{cls.__name__} id {model_id} invalid"} | ||
| abort(make_response(response, 400)) | ||
|
|
||
| query = db.select(cls).where(cls.id == model_id) | ||
| model = db.session.scalar(query) | ||
|
|
||
| if not model: | ||
| response = {"details": f"{cls.__name__} id {model_id} not found"} | ||
| abort(make_response(response, 404)) | ||
|
|
||
| return model | ||
|
|
||
|
|
||
| def create_model(cls, model_data): | ||
| try: | ||
| new_model = cls.from_dict(model_data) | ||
|
|
||
| except KeyError as error: | ||
| response = {"details": "Invalid data"} | ||
| abort(make_response(response, 400)) | ||
|
|
||
| db.session.add(new_model) | ||
| db.session.commit() | ||
| return ({cls.__name__.lower(): new_model.to_dict()}), 201 | ||
MarinaAdams marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def get_models_with_filters(cls, filters=None): | ||
| query = db.select(cls) | ||
| sort_param = None | ||
|
|
||
| if filters: | ||
| filters_dict = dict(filters) | ||
| sort_param = filters_dict.pop("sort", None) | ||
|
|
||
| for attribute, value in filters_dict.items(): | ||
| if hasattr(cls, attribute): | ||
| query = query.where(getattr(cls, attribute).ilike(f"%{value}%")) | ||
|
|
||
| if sort_param == "asc": | ||
| query = query.order_by(cls.title.asc()) | ||
| elif sort_param == "desc": | ||
| query = query.order_by(cls.title.desc()) | ||
| else: | ||
| query = query.order_by(cls.id) | ||
|
|
||
| models = db.session.scalars(query) | ||
| return [model.to_dict() for model in models] | ||
|
|
||
|
|
||
| def call_slack_api(task): | ||
| slack_url = "https://slack.com/api/chat.postMessage" | ||
| slack_token = os.environ.get("SLACKBOT_TOKEN") | ||
|
|
||
| headers = { | ||
| "Authorization": f"Bearer {slack_token}", | ||
| "Content-Type": "application/json", | ||
| } | ||
|
|
||
| data = { | ||
| "channel": "test-slack-api", | ||
| "text": f"Someone just completed the task {task.title}", | ||
| } | ||
|
|
||
| responce = requests.post(slack_url, headers=headers, json=data) | ||
| return responce | ||
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 +1,73 @@ | ||
| from flask import Blueprint | ||
| from datetime import datetime | ||
|
|
||
| from flask import Blueprint, request, Response, make_response | ||
|
|
||
| from app.models.task import Task | ||
| from .route_utilities import ( | ||
| validate_model, | ||
| create_model, | ||
| get_models_with_filters, | ||
| call_slack_api, | ||
| ) | ||
| from ..db import db | ||
|
|
||
|
|
||
| bp = Blueprint("tasks_bp", __name__, url_prefix="/tasks") | ||
|
|
||
|
|
||
| @bp.post("") | ||
| def create_task(): | ||
| request_body = request.get_json() | ||
|
|
||
| return create_model(Task, request_body) | ||
|
|
||
|
|
||
| @bp.get("") | ||
| def get_all_tasks(): | ||
| return get_models_with_filters(Task, request.args) | ||
|
|
||
|
|
||
| @bp.patch("/<task_id>/mark_complete") | ||
| def mark_complete(task_id): | ||
| task = validate_model(Task, task_id) | ||
| task.completed_at = datetime.today() | ||
| db.session.commit() | ||
|
|
||
| call_slack_api(task) | ||
| return make_response(task.to_dict(), 204) | ||
|
|
||
|
|
||
| @bp.patch("/<task_id>/mark_incomplete") | ||
| def mark_incomplete(task_id): | ||
| task = validate_model(Task, task_id) | ||
| task.completed_at = None | ||
| db.session.commit() | ||
| return Response(status=204, mimetype="application/json") | ||
|
|
||
|
|
||
| @bp.get("/<task_id>") | ||
| def get_one_task(task_id): | ||
| task = validate_model(Task, task_id) | ||
| return {"task": task.to_dict()} | ||
|
|
||
|
|
||
| @bp.put("/<task_id>") | ||
| def update_task(task_id): | ||
| task = validate_model(Task, task_id) | ||
| request_body = request.get_json() | ||
|
|
||
| if "title" not in request_body or "description" not in request_body: | ||
| return make_response({"details": "Invalid data"}, 400) | ||
|
|
||
| task.title = request_body["title"] | ||
| task.description = request_body["description"] | ||
MarinaAdams marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| db.session.commit() | ||
| return Response(status=204, mimetype="application/json") | ||
|
|
||
|
|
||
| @bp.delete("/<task_id>") | ||
| def delete_task(task_id): | ||
| task = validate_model(Task, task_id) | ||
| db.session.delete(task) | ||
| db.session.commit() | ||
| return Response(status=204, mimetype="application/json") | ||
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.
Uh oh!
There was an error while loading. Please reload this page.