Conversation
| 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") |
| if self.tasks: | ||
| goal_dict["tasks"] = [task.to_dict() for task in self.tasks] |
| new_goal = cls(title=goal_data["title"]) | ||
| return new_goal |
There was a problem hiding this comment.
You could directly return the object since you create the variable new_goal and then immediately return it without accessing it.
| new_goal = cls(title=goal_data["title"]) | |
| return new_goal | |
| return cls(title=goal_data["title"]) |
| title: Mapped[str] | ||
| description: Mapped[str] | ||
| completed_at: Mapped[Optional[datetime]] | ||
| goal_id: Mapped[Optional[int]] = mapped_column(ForeignKey("goal.id")) | ||
| goal: Mapped[Optional["Goal"]] = relationship(back_populates="tasks") |
| "description": self.description, | ||
| "is_complete": self.completed_at is not None | ||
| } | ||
| if self.goal_id is not None: |
There was a problem hiding this comment.
It would be more Pythonic to do a truthy check, like:
| if self.goal_id is not None: | |
| if self.goal_id: |
| @bp.patch("/<task_id>/mark_complete") | ||
| def mark_complete(task_id): | ||
| task = validate_model(Task, task_id) | ||
| # response = task.to_dict() |
There was a problem hiding this comment.
| # response = task.to_dict() |
❗️take care to remove commented out code. It clutters the codebase, can be confusing to other devs, and if it was accidentally uncommented and the code executed unintentionally then we'd have a bug.
| json_payload = { | ||
| 'channel': 'task-notifications', | ||
| 'text':f"Someone just completed the task, {task.title}", | ||
| 'Content-Type':'application/json; charset=utf-8' | ||
| } | ||
| requests.post('https://slack.com/api/chat.postMessage', | ||
| json=json_payload, | ||
| headers={'Authorization': 'Bearer ' + os.environ.get('SLACK_TOKEN')}) |
There was a problem hiding this comment.
Prefer this logic to be in a helper function like make_request_to_slack to keep this route concise and single-responsibility.
There was a problem hiding this comment.
The channel name and slack URL that appear here as magic strings should be avoided by using constant variables instead.
| json_payload = { | |
| 'channel': 'task-notifications', | |
| 'text':f"Someone just completed the task, {task.title}", | |
| 'Content-Type':'application/json; charset=utf-8' | |
| } | |
| requests.post('https://slack.com/api/chat.postMessage', | |
| json=json_payload, | |
| headers={'Authorization': 'Bearer ' + os.environ.get('SLACK_TOKEN')}) | |
| SLACK_CHANNEL = "task-notifications" | |
| SLACK_ENDPOINT = "https://slack.com/api/chat.postMessage" | |
| json_payload = { | |
| 'channel': SLACK_CHANNEL, | |
| 'text':f"Someone just completed the task, {task.title}", | |
| 'Content-Type':'application/json; charset=utf-8' | |
| } | |
| requests.post(SLACK_ENDPOINT, | |
| json=json_payload, | |
| headers={'Authorization': 'Bearer ' + os.environ.get('SLACK_TOKEN')}) |
| def mark_incomplete(task_id): | ||
| task = validate_model(Task, task_id) | ||
|
|
||
| if task.completed_at is not None: |
There was a problem hiding this comment.
It would be more Pythonic to check that the value is truthy:
| if task.completed_at is not None: | |
| if task.completed_at: |
| from flask import Flask | ||
| from .db import db, migrate | ||
| from .models import task, goal | ||
| #from .models import task, goal |
There was a problem hiding this comment.
| #from .models import task, goal |
Delete unused imports
There was a problem hiding this comment.
👍 Your added assertions for this test file look good to me.
No description provided.