Conversation
…or 400 invalid response and 404 not found response.
…tasks, and get one task
… pass test_create_task
mikellewade
left a comment
There was a problem hiding this comment.
Nice work overall, Brianna! Though I would like for you to read through the comments to see the ones I left about refactoring. There are a lot of places where you could have DRY'd up your code. Refactoring is a big part of this project because it helps train your eye for seeing similarities in your codebase as well as practicing maintainability and scalability.
| from .routes.task_routes import tasks_bp | ||
| from .routes.goal_routes import goals_bp |
There was a problem hiding this comment.
Don't forget that the convention for naming blueprints is to name them bp. With each blueprint being named the same thing we will need to import them under an alias like so:
from .routes.task_routes import bp as tasks_bp| 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") |
| title: Mapped[str] | ||
| description: Mapped[str] | ||
| completed_at: Mapped[Optional[datetime]] = mapped_column(nullable=True) | ||
| goal_id: Mapped[Optional[int]] = mapped_column(ForeignKey("goal.id")) | ||
| goal: Mapped[Optional["Goal"]] = relationship(back_populates="tasks") |
| "id":self.id, | ||
| "title":self.title, | ||
| "description":self.description, | ||
| "is_complete":self.completed_at is not None, |
|
|
||
| @classmethod | ||
| def from_dict(cls, task_data): | ||
| completed_at = datetime if task_data.get("is_complete", False) else None |
|
|
||
| try: | ||
| new_task = Task.from_dict(request_body) | ||
|
|
||
| except KeyError as e: | ||
| response = {"details": "Invalid data"} | ||
| abort(make_response(response, 400)) | ||
|
|
||
| db.session.add(new_task) | ||
| db.session.commit() |
There was a problem hiding this comment.
Another place that is a sign for us to refactor this logic like in our previous create route.
| task = validate_task(task_id) | ||
|
|
||
| task.mark_complete() | ||
| db.session.commit() | ||
|
|
||
| post_to_slack(task) | ||
|
|
||
| return {"task": task.to_dict()}, 200 |
| def validate_task(task_id): | ||
| try: | ||
| task_id = int(task_id) | ||
| except: | ||
| response = {"message": f"task {task_id} invalid"} | ||
| abort(make_response(response , 400)) | ||
|
|
||
| query = db.select(Task).where(Task.id == task_id) | ||
| task = db.session.scalar(query) | ||
|
|
||
| if not task: | ||
| response = {"message": "task not found"} | ||
| abort(make_response(response, 404)) | ||
| return task | ||
|
|
||
| def post_to_slack(task): | ||
| headers = { | ||
| "Authorization": f"Bearer {SLACK_API_TOKEN}", | ||
| "Content-Type": "application/json", | ||
|
|
||
| } | ||
| if task.completed_at: | ||
| data = { | ||
| "text": f"Task '{task.title}' has been marked complete", | ||
| "channel": "C080MLHBX5W", | ||
| } | ||
| else: | ||
| data = { | ||
| "text": f"Task '{task.title}' has been marked incomplete", | ||
| "channel": "C080MLHBX5W", | ||
| } | ||
|
|
||
| r = requests.post(SLACK_API_URL, headers=headers, json=data) | ||
|
|
||
| return r No newline at end of file |
There was a problem hiding this comment.
Both of these functions could live in our utilities file to better separate the concerns of our files.
| assert response_body == { | ||
| "message": "task not found" | ||
| } |
| response = client.put("/goals/1", json={ | ||
| "title": "Build a habit of going outside daily" | ||
| }) | ||
| # Assert | ||
| response_body = response.get_json() | ||
| # ---- Complete Assertions Here ---- | ||
| # assertion 1 goes here | ||
| # assertion 2 goes here | ||
| # assertion 3 goes here | ||
| assert response.status_code == 200 | ||
| assert "goal" in response_body | ||
| assert response_body == { | ||
| "goal" : { | ||
| "id": 1, | ||
| "title": "Build a habit of going outside daily" | ||
| } | ||
| } |
No description provided.