Skip to content

C22 Sphinx - Weixi He#36

Open
weixi1 wants to merge 18 commits intoAda-C22:mainfrom
weixi1:main
Open

C22 Sphinx - Weixi He#36
weixi1 wants to merge 18 commits intoAda-C22:mainfrom
weixi1:main

Conversation

@weixi1
Copy link

@weixi1 weixi1 commented Nov 8, 2024

No description provided.

Copy link
Collaborator

@kelsey-steven-ada kelsey-steven-ada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work overall! There are some items I would like to see you revisit, particularly around establishing the one-to-many relationship and tests passing, I'll leave more details on that in Learn.

app/__init__.py Outdated
Comment on lines 2 to 3
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use these modules directly in __init__.py so should remove these imports so the file doesn't load up dependencies that it will not use.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str] = mapped_column()

#tasks: Mapped[list["Task"]] = relationship("Task", backref="goal", lazy="dynamic")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In hello-books we created a relationship attribute between an Author and their list of Books with syntax like:

books: Mapped[list["Book"]] = relationship(back_populates="author")

How could we use the same structure of syntax here to create a relationship attribute between a Goal and a list of Tasks?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


class Goal(db.Model):
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str] = mapped_column()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we aren't doing any customization inside the mapped_column call, we could leave it off:

title: Mapped[str]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 9 to 11
title: Mapped[str] = mapped_column()
description: Mapped[str] = mapped_column()
completed_at: Mapped[Optional[datetime]] = mapped_column(nullable=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the feedback in Goal, we could remove the mapped_column calls here:

    title: Mapped[str]
    description: Mapped[str]
    completed_at: Mapped[Optional[datetime]]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

description: Mapped[str] = mapped_column()
completed_at: Mapped[Optional[datetime]] = mapped_column(nullable=True)

goal_id: Mapped[Optional[int]] = mapped_column(ForeignKey("goal.id"), nullable=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since goal_id is Optional, we can leave off the nullable argument in mapped_column:

goal_id: Mapped[Optional[int]] = mapped_column(ForeignKey('goal.id'))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 138 to 157
def patch_complete(task_id):
task = validate_task(task_id)
task.completed_at = datetime.now().isoformat()
db.session.commit()

url = "https://slack.com/api/chat.postMessage"
headers = {
"Authorization": f"Bearer {os.environ.get('SLACK_API_KEY')}",
"Content-Type": "application/json"
}
data = {
"channel" : "#api-test-channel",
"text": f"Someone just completed the task {task.title}"
}

requests.post(url, headers=headers, json=data)

response = {"task": task.to_dict()}

return response, 200
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this function doesn't have a route annotation, what pieces were you still working on?

Comment on lines 29 to 31
goals_response =[]
for goal in goals:
goals_response.append(goal.to_dict())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be another great place for a list comprehension:

goals_response = [goal.to_dict() for goal in goals]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

query = db.select(Goal)
goals = db.session.scalars(query)

goals_response =[]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
goals_response =[]
goals_response = []

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"goal":goal.to_dict()
}, 200

def validate_goal(goal_id):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very similar to our validate_task function, how could we refactor these functions to reduce repetition?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I can create validate_models in bass.py then impor it in goal_routes and task_routes!

"title": "Updated Goal Title",
}
}
goal = Goal.query.get(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you may have seen this syntax in the tests and used the same. These tests need to be updated, the <model>.query syntax has been deprecated since SQLAlchemy 2.0 and should not be used in any new development.

There is a similar structure we can use through the db.session object to look up a single record by its id that we could use here instead (though we would need to import db into the test file).

db.session.get(Goal, 1)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I got it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants