-
Notifications
You must be signed in to change notification settings - Fork 44
C22 Sphinx - Brianna R. #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
base: main
Are you sure you want to change the base?
Conversation
…or 400 invalid response and 404 not found response.
…tasks, and get one task
… pass test_create_task
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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") |
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.
⭐️
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") |
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.
👍🏿
"id":self.id, | ||
"title":self.title, | ||
"description":self.description, | ||
"is_complete":self.completed_at is not None, |
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.
Nice work here!
|
||
@classmethod | ||
def from_dict(cls, task_data): | ||
completed_at = datetime if task_data.get("is_complete", False) else None |
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.
✨
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another place that is a sign for us to refactor this logic like in our previous create route.
|
||
task.mark_complete() | ||
db.session.commit() | ||
|
||
post_to_slack(task) | ||
|
||
return {"task": task.to_dict()}, 200 |
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.
✨
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 |
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.
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" | ||
} |
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.
Great work!
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" | ||
} | ||
} |
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.
Nice test, very robust!
No description provided.