forked from fastapi/full-stack-fastapi-template
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from whythawk/upgrade-path-to-pydantic-2.0
Complete update of stack to latest long-term releases. - `frontend`: - Node 16 -> 18 - Nuxt 3.2 -> 3.6.5 - Latest Pinia requires changes in stores, where imports are not required (cause actual errors), and parameter declaration must happen in functions. - `backend` and `celeryworker`: - Python 3.9 -> 3.11 - FastAPI 0.88 -> 0.99 (Inboard 0.37 -> 0.51) - Poetry -> Hatch - Postgres 14 -> 15
- Loading branch information
Showing
47 changed files
with
2,727 additions
and
5,035 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.9.4 | ||
3.11.0 |
72 changes: 72 additions & 0 deletions
72
...er.project_slug}}/backend/app/alembic/versions/fb120f8fc198_token_remove_to_invalidate.py
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 |
---|---|---|
@@ -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 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" |
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
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
33 changes: 33 additions & 0 deletions
33
{{cookiecutter.project_slug}}/backend/app/app/api/sockets.py
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 |
---|---|---|
@@ -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 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
50 changes: 19 additions & 31 deletions
50
{{cookiecutter.project_slug}}/backend/app/app/crud/crud_token.py
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,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 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
Oops, something went wrong.