-
Notifications
You must be signed in to change notification settings - Fork 720
Crow_lexy Le task_list_api #89
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: main
Are you sure you want to change the base?
Changes from 6 commits
bd34383
61c3168
98afc1c
eb9c199
c6e7a13
769ad1a
e7c70f0
d340937
f49cafa
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| .vscode | ||
| .DS_Store | ||
| .env | ||
|
|
||
| # Byte-compiled / optimized / DLL files | ||
| __pycache__/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,25 @@ | ||
| from sqlalchemy.orm import Mapped, mapped_column | ||
| from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
| from ..db import db | ||
| from sqlalchemy import String | ||
|
|
||
|
|
||
|
|
||
| class Goal(db.Model): | ||
|
|
||
| id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) | ||
| title: Mapped[str] = mapped_column(String(100), nullable=False) | ||
|
Contributor
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 many applications, it may make sense to add a limit to the Especially when working with others, we want to avoid changing or adding behavior that has already been decided for a product, unless those changes have been discussed and signed off on by stakeholders for the project. We are working on this in isolation, but in a project with other folks, the choice to have no title limit may have come from user research on how folks are using the product. Introducing a limit that other developers were not expecting could lead to issues for other developers features or customer complaints when things do not work as expected or advertised.
Contributor
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.
|
||
| tasks: Mapped[list["Task"]] = relationship( | ||
| back_populates="goal") | ||
|
|
||
|
|
||
| def to_dict(self): | ||
| return { | ||
| "id": self.id, | ||
| "title": self.title | ||
| } | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, data_dict): | ||
| if "title" not in data_dict: | ||
| raise KeyError("title") | ||
|
Comment on lines
+23
to
+24
Contributor
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 pattern of checking for a key and raising a KeyError ourselves is an anti-pattern we should avoid. The Python language will raise a KeyError for us if we try to access the title key, so we should simplify our code by relying on that.
|
||
| return cls(title=data_dict["title"]) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,49 @@ | ||
| from sqlalchemy.orm import Mapped, mapped_column | ||
| from flask_sqlalchemy import SQLAlchemy | ||
| from sqlalchemy.orm import Mapped, mapped_column,relationship | ||
| from sqlalchemy import ForeignKey | ||
| from ..db import db | ||
| from sqlalchemy import String, Date | ||
| from datetime import datetime | ||
| from flask import Blueprint, abort, make_response, request, Response,jsonify | ||
| from typing import Optional | ||
|
Comment on lines
1
to
8
Contributor
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 like to see a response to these questions. |
||
|
|
||
| class Task(db.Model): | ||
| id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) | ||
| title: Mapped[str] = mapped_column(String(100), nullable=False) | ||
| description: Mapped[str] = mapped_column(String(255)) | ||
| completed_at:Mapped[datetime.date] = mapped_column(Date, nullable=True) | ||
|
||
|
|
||
| goal_id: Mapped[Optional[int]] = mapped_column(ForeignKey("goal.id")) | ||
| goal: Mapped[Optional["Goal"]] = relationship(back_populates="tasks") | ||
|
|
||
| def to_dict(self): # json | ||
|
|
||
|
|
||
| task_dict = {"id": self.id, | ||
| "title":self.title, | ||
| "description":self.description, | ||
| #"is_complete":self.completed_at if self.completed_at else False | ||
| "is_complete": self.completed_at is not None} | ||
|
|
||
| if self.goal_id is not None: | ||
| task_dict["goal_id"] = self.goal_id | ||
|
|
||
|
|
||
| return task_dict | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, data_dict): | ||
| if "title" not in data_dict: | ||
|
|
||
| raise KeyError("title") | ||
| if "description" not in data_dict: | ||
| #abort(make_response({"details": "Invalid data"}, 400)) | ||
| raise KeyError("description") | ||
| return cls( | ||
| title=data_dict["title"], | ||
| description=data_dict["description"], | ||
| completed_at=None # default to None when creating | ||
| ) | ||
|
Comment on lines
35
to
46
Contributor
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. Using the feedback on the |
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,101 @@ | ||
| from flask import Blueprint | ||
| from flask import Blueprint, request, abort, make_response,Response | ||
| from ..db import db | ||
| from ..models.goal import Goal | ||
| from ..models.task import Task | ||
| from .route_utilities import validate_model,create_model,get_models_with_filters | ||
|
|
||
|
|
||
| bp = Blueprint("goals_bp", __name__, url_prefix="/goals") | ||
|
|
||
|
|
||
|
|
||
| @bp.post("") | ||
| def create_goal(): | ||
| request_body = request.get_json() | ||
| # try: | ||
| # new_goal = Goal.from_dict(request_body) | ||
| # except KeyError: | ||
| # return jsonify({"details": "Invalid data"}), 400 | ||
|
|
||
| # db.session.add(new_goal) | ||
| # db.session.commit() | ||
|
|
||
| #return jsonify(new_goal.to_dict()), 201 | ||
| return create_model(Goal,request_body) | ||
|
Contributor
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. Nice update to use
Author
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. just updated |
||
|
|
||
|
|
||
| @bp.get("") | ||
| def get_all_goals(): | ||
| filters = request.args.to_dict() | ||
| return get_models_with_filters(Goal,filters) | ||
|
|
||
| @bp.get("/<id>") | ||
| def get_one_goal(id): | ||
| goal = validate_model(Goal, id) | ||
| return goal.to_dict() | ||
|
|
||
|
|
||
|
|
||
|
|
||
| @bp.put("/<id>") | ||
| def update_goal(id): | ||
| goal = validate_model(Goal, id) | ||
| request_body = request.get_json() | ||
|
|
||
| goal.title = request_body["title"] | ||
|
|
||
| db.session.commit() | ||
| return goal.to_dict() | ||
|
|
||
|
|
||
|
|
||
| @bp.delete("/<id>") | ||
| def delete_goal(id): | ||
| goal = validate_model(Goal, id) | ||
|
|
||
| db.session.delete(goal) | ||
| db.session.commit() | ||
|
|
||
| #return jsonify({"message": f'Goal {goal.id} successfully deleted'}), 204 | ||
| return Response(status=204, mimetype="application/json") | ||
|
|
||
| #nested | ||
| @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", []) | ||
|
|
||
| for task in goal.tasks: | ||
| task.goal_id = None | ||
|
|
||
| for task_id in task_ids: | ||
| task = validate_model(Task, task_id) | ||
| task.goal_id = goal.id | ||
|
|
||
| db.session.commit() | ||
|
|
||
| return { | ||
| "id": goal.id, | ||
| "task_ids": task_ids | ||
| }, 200 | ||
|
|
||
|
|
||
|
|
||
| @bp.get("/<goal_id>/tasks") | ||
| def get_tasks_for_goal(goal_id): | ||
| goal = validate_model(Goal, goal_id) | ||
|
|
||
| tasks_response = [task.to_dict() for task in goal.tasks] | ||
|
|
||
| response_body = { | ||
| "id": goal.id, | ||
| "title": goal.title, | ||
| "tasks": tasks_response | ||
| } | ||
|
|
||
| return response_body,200 | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from flask import abort, make_response | ||
| from ..db import db | ||
|
|
||
| def validate_model(cls, id): | ||
| try: | ||
| id = int(id) | ||
| except ValueError: | ||
| invalid = {"message": f"{cls.__name__} id({id}) is invalid."} | ||
| abort(make_response(invalid, 400)) | ||
|
|
||
| query = db.select(cls).where(cls.id == id) | ||
| model = db.session.scalar(query) | ||
|
|
||
| if not model: | ||
| not_found = {"message": f"{cls.__name__} with id({id}) not found."} | ||
| abort(make_response(not_found, 404)) | ||
|
|
||
| return model | ||
|
|
||
|
|
||
| def create_model(cls, model_data): | ||
| try: | ||
| new_model = cls.from_dict(model_data) | ||
| except KeyError as e: | ||
| #response = {"message": f"Invalid request: missing {e.args[0]}"} | ||
| #abort(make_response(response, 400)) | ||
|
||
| abort(make_response({"details": "Invalid data"}, 400)) | ||
|
|
||
| db.session.add(new_model) | ||
| db.session.commit() | ||
|
|
||
| return new_model.to_dict(), 201 | ||
|
Comment on lines
20
to
29
Contributor
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. Since this both creates a model and creates an http response, I'd consider renaming this to
|
||
|
|
||
| def get_models_with_filters(cls, filters=None): | ||
| query = db.select(cls) | ||
|
|
||
| if filters: | ||
| for attribute, value in filters.items(): | ||
| if hasattr(cls, attribute): | ||
| query = query.where(getattr(cls, attribute).ilike(f"%{value}%")) | ||
|
|
||
| models = db.session.scalars(query.order_by(cls.id)) | ||
| models_response = [model.to_dict() for model in models] | ||
| return models_response | ||
|
Comment on lines
20
to
41
Contributor
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. These two functions are not implemented in the relevant routes. How can we update the task and goal create routes to use
|
||
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.
Thanks for including the image ^_^