-
-
Notifications
You must be signed in to change notification settings - Fork 901
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update flaskr and todo to use SQLAlchemy 2.0 style (#1253)
- Loading branch information
Showing
6 changed files
with
53 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
from werkzeug.security import check_password_hash | ||
from werkzeug.security import generate_password_hash | ||
|
||
from flaskr import db | ||
|
||
if TYPE_CHECKING: | ||
from ..blog.models import Post | ||
|
||
class User(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
username = db.Column(db.String, unique=True, nullable=False) | ||
password_hash = db.Column(db.String, nullable=False) | ||
|
||
posts = db.relationship("Post", back_populates="author") | ||
class User(db.Model): | ||
id: Mapped[int] = mapped_column(primary_key=True) | ||
username: Mapped[str] = mapped_column(unique=True) | ||
password_hash: Mapped[str] | ||
posts: Mapped[list[Post]] = relationship("Post", back_populates="author") | ||
|
||
def set_password(self, value): | ||
def set_password(self, value: str) -> None: | ||
"""Store the password as a hash for security.""" | ||
self.password_hash = generate_password_hash(value) | ||
|
||
# allow password = "..." to set a password | ||
password = property(fset=set_password) | ||
|
||
def check_password(self, value): | ||
def check_password(self, value: str) -> bool: | ||
return check_password_hash(self.password_hash, value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters