forked from fastapi/full-stack-fastapi-template
-
Notifications
You must be signed in to change notification settings - Fork 47
Upgrade path to Pydantic 2.0 #35
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
002bfa5
Upgrade path to Pydantic 2.0
turukawa 55e3ef4
Reference fixes
turukawa 85307cd
Fixing Pinia referencing changes
turukawa 9ae8786
Changed token invalidation to removal
turukawa a6b9a83
Update {{cookiecutter.project_slug}}/backend/app/pyproject.toml
turukawa a012b27
Update {{cookiecutter.project_slug}}/backend/backend.dockerfile
turukawa 4d52095
Build fixes
turukawa d991ee5
Update README.md
turukawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 +1 @@ | ||
3.9.4 | ||
3.11.0 |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__="0.1.0" |
72 changes: 72 additions & 0 deletions
72
...er.project_slug}}/backend/app/alembic/versions/fb120f8fc198_token_remove_to_invalidate.py
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Token remove to invalidate | ||
|
||
Revision ID: fb120f8fc198 | ||
Revises: 8188d671489a | ||
Create Date: 2023-07-25 11:39:26.423122 | ||
|
||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "fb120f8fc198" | ||
down_revision = "8188d671489a" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.alter_column("token", "authenticates_id", | ||
existing_type=sa.UUID(), | ||
nullable=False) | ||
op.drop_column("token", "is_valid") | ||
op.alter_column("user", "created", | ||
existing_type=postgresql.TIMESTAMP(), | ||
type_=sa.DateTime(timezone=True), | ||
existing_nullable=False, | ||
existing_server_default=sa.text("now()")) | ||
op.alter_column("user", "modified", | ||
existing_type=postgresql.TIMESTAMP(), | ||
type_=sa.DateTime(timezone=True), | ||
existing_nullable=False, | ||
existing_server_default=sa.text("now()")) | ||
op.alter_column("user", "email_validated", | ||
existing_type=sa.BOOLEAN(), | ||
nullable=False) | ||
op.alter_column("user", "is_active", | ||
existing_type=sa.BOOLEAN(), | ||
nullable=False) | ||
op.alter_column("user", "is_superuser", | ||
existing_type=sa.BOOLEAN(), | ||
nullable=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.alter_column("user", "is_superuser", | ||
existing_type=sa.BOOLEAN(), | ||
nullable=True) | ||
op.alter_column("user", "is_active", | ||
existing_type=sa.BOOLEAN(), | ||
nullable=True) | ||
op.alter_column("user", "email_validated", | ||
existing_type=sa.BOOLEAN(), | ||
nullable=True) | ||
op.alter_column("user", "modified", | ||
existing_type=sa.DateTime(timezone=True), | ||
type_=postgresql.TIMESTAMP(), | ||
existing_nullable=False, | ||
existing_server_default=sa.text("now()")) | ||
op.alter_column("user", "created", | ||
existing_type=sa.DateTime(timezone=True), | ||
type_=postgresql.TIMESTAMP(), | ||
existing_nullable=False, | ||
existing_server_default=sa.text("now()")) | ||
op.add_column("token", sa.Column("is_valid", sa.BOOLEAN(), autoincrement=False, nullable=True)) | ||
op.alter_column("token", "authenticates_id", | ||
existing_type=sa.UUID(), | ||
nullable=True) | ||
# ### end Alembic commands ### |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
33 changes: 33 additions & 0 deletions
33
{{cookiecutter.project_slug}}/backend/app/app/api/sockets.py
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from __future__ import annotations | ||
from fastapi import WebSocket | ||
from starlette.websockets import WebSocketDisconnect | ||
from websockets.exceptions import ConnectionClosedError | ||
|
||
|
||
async def send_response(*, websocket: WebSocket, response: dict): | ||
try: | ||
await websocket.send_json(response) | ||
return True | ||
except (WebSocketDisconnect, ConnectionClosedError): | ||
return False | ||
|
||
|
||
async def receive_request(*, websocket: WebSocket) -> dict: | ||
try: | ||
return await websocket.receive_json() | ||
except (WebSocketDisconnect, ConnectionClosedError): | ||
return {} | ||
|
||
|
||
def sanitize_data_request(data: any) -> any: | ||
# Putting here for want of a better place | ||
if isinstance(data, (list, tuple, set)): | ||
return type(data)(sanitize_data_request(x) for x in data if x or isinstance(x, bool)) | ||
elif isinstance(data, dict): | ||
return type(data)( | ||
(sanitize_data_request(k), sanitize_data_request(v)) | ||
for k, v in data.items() | ||
if k and v or isinstance(v, bool) | ||
) | ||
else: | ||
return data |
This file contains hidden or 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 hidden or 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
50 changes: 19 additions & 31 deletions
50
{{cookiecutter.project_slug}}/backend/app/app/crud/crud_token.py
This file contains hidden or 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,47 +1,35 @@ | ||
from __future__ import annotations | ||
from sqlalchemy.orm import Session | ||
from typing import List | ||
from sqlalchemy import and_ | ||
|
||
from app.crud.base import CRUDBase | ||
from app.models import User, Token | ||
from app.schemas import RefreshTokenCreate, RefreshTokenUpdate | ||
from app.core.config import settings | ||
|
||
|
||
class CRUDToken(CRUDBase[Token, RefreshTokenCreate, RefreshTokenUpdate]): | ||
# Everything is user-dependent | ||
def create(self, db: Session, *, obj_in: str, user_obj: User) -> User: | ||
def create(self, db: Session, *, obj_in: str, user_obj: User) -> Token: | ||
db_obj = db.query(self.model).filter(self.model.token == obj_in).first() | ||
if db_obj and db_obj.authenticates == user_obj: | ||
# In case the token was invalidated, then recreated with the same token key | ||
setattr(db_obj, "is_valid", True) | ||
db.add(db_obj) | ||
db.commit() | ||
db.refresh(db_obj) | ||
return db_obj | ||
if db_obj and db_obj.authenticates != user_obj: | ||
raise ValueError(f"Token mismatch between key and user.") | ||
db_obj = Token(token=obj_in) | ||
db.add(db_obj) | ||
db.commit() | ||
db.refresh(db_obj) | ||
user_obj.refresh_tokens.append(db_obj) | ||
db.commit() | ||
db.refresh(db_obj) | ||
return db_obj | ||
|
||
def cancel_refresh_token(self, db: Session, *, db_obj: Token) -> Token: | ||
setattr(db_obj, "is_valid", False) | ||
db.add(db_obj) | ||
db.commit() | ||
db.refresh(db_obj) | ||
return db_obj | ||
raise ValueError("Token mismatch between key and user.") | ||
obj_in = RefreshTokenCreate(**{"token": obj_in, "authenticates_id": user_obj.id}) | ||
return super().create(db=db, obj_in=obj_in) | ||
|
||
def get(self, *, user: User, token: str) -> Token: | ||
return user.refresh_tokens.filter(and_(self.model.token == token, self.model.is_valid == True)).first() | ||
|
||
def get_multi(self, *, user: User, skip: int = 0, limit: int = 100) -> List[Token]: | ||
return user.refresh_tokens.filter(self.model.is_valid == True).offset(skip).limit(limit).all() | ||
|
||
return user.refresh_tokens.filter(self.model.token == token).first() | ||
|
||
def get_multi(self, *, user: User, page: int = 0, page_break: bool = False) -> list[Token]: | ||
db_objs = user.refresh_tokens | ||
if not page_break: | ||
if page > 0: | ||
db_objs = db_objs.offset(page * settings.MULTI_MAX) | ||
db_objs = db_objs.limit(settings.MULTI_MAX) | ||
return db_objs.all() | ||
|
||
def remove(self, db: Session, *, db_obj: Token) -> None: | ||
db.delete(db_obj) | ||
db.commit() | ||
return None | ||
|
||
token = CRUDToken(Token) |
This file contains hidden or 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 hidden or 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 hidden or 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
8 changes: 4 additions & 4 deletions
8
{{cookiecutter.project_slug}}/backend/app/app/schemas/token.py
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.