Skip to content

Commit f5b5c45

Browse files
committed
fix: resolved pre-commit errors
1 parent 0c611a3 commit f5b5c45

File tree

5 files changed

+40
-21
lines changed

5 files changed

+40
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# FastAPI + SQLModel + Alembic + Celery + MongoDB + Redis + jwt Auth
1+
# FastAPI + Async SQLAlchemy + Alembic + Celery + MongoDB + Redis + jwt Auth
22

33
This project is an opinionated boilerplate for **FastAPI** micro framework that uses,
44
_**Asynchronous SQLAlchemy**_, **_PostgresSQL_**, _**Alembic**_, **_Celery_**, **_MongoDB_**, _**Redis**_, **_Docker_** and **_jwt Authentication_**. You can use this ready to

app/auth/handlers.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ class TokenData(BaseModel):
4141

4242

4343
def verify_password(plain_password, hashed_password):
44-
return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password)
44+
return bcrypt.checkpw(plain_password.encode("utf-8"), hashed_password)
4545

4646

4747
def get_password_hash(password: str):
4848
salt = bcrypt.gensalt(rounds=14)
49-
return bcrypt.hashpw(password.encode('utf-8'), salt)
49+
return bcrypt.hashpw(password.encode("utf-8"), salt)
5050

5151

5252
async def get_user(username: str | None):
@@ -77,7 +77,7 @@ def create_access_token(data: dict, expires_delta: timedelta | None = None):
7777

7878

7979
async def get_current_user(
80-
security_scopes: SecurityScopes, token: Annotated[str, Depends(oauth2_scheme)]
80+
security_scopes: SecurityScopes, token: Annotated[str, Depends(oauth2_scheme)]
8181
):
8282
if security_scopes.scopes:
8383
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
@@ -111,7 +111,7 @@ async def get_current_user(
111111

112112

113113
async def get_current_active_user(
114-
current_user: Annotated[User, Security(get_current_user, scopes=["me"])],
114+
current_user: Annotated[User, Security(get_current_user, scopes=["me"])],
115115
):
116116
if current_user.disabled:
117117
raise HTTPException(status_code=400, detail="Inactive user")
@@ -120,7 +120,7 @@ async def get_current_active_user(
120120

121121
@router.post("/token")
122122
async def login_for_access_token(
123-
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
123+
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
124124
) -> Token:
125125
user = await authenticate_user(form_data.username, form_data.password)
126126
if not user:
@@ -139,14 +139,16 @@ async def login_for_access_token(
139139

140140
@router.get("/user", response_model=UserRead)
141141
async def get_current_active_user_from_token(
142-
current_user: Annotated[User, Depends(get_current_active_user)],
142+
current_user: Annotated[User, Depends(get_current_active_user)],
143143
):
144144
return current_user
145145

146146

147147
@router.post("/user", response_model=UserRead)
148-
async def register_user(user: UserCreate, session: AsyncSession = Depends(get_db_session),
149-
):
148+
async def register_user(
149+
user: UserCreate,
150+
session: AsyncSession = Depends(get_db_session),
151+
):
150152
new_user = User(
151153
username=user.username,
152154
full_name=user.full_name,

app/songs/handlers.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from sqlalchemy.orm import selectinload
66

77
from ..db import get_db_session
8+
from ..redis import r
89
from .models import City, Song, Tag
910
from .schemas import CityCreate, CityRead, SongCreate, SongRead, TagCreate, TagRead
1011

@@ -14,9 +15,10 @@
1415

1516

1617
@router.get("/songs", response_model=list[SongRead])
17-
async def get_songs(
18-
session: AsyncSession = Depends(get_db_session)):
19-
result = await session.scalars(select(Song).options(selectinload(Song.tags), selectinload(Song.city)))
18+
async def get_songs(session: AsyncSession = Depends(get_db_session)):
19+
result = await session.scalars(
20+
select(Song).options(selectinload(Song.tags), selectinload(Song.city))
21+
)
2022
songs = result.all()
2123
return songs
2224

@@ -35,7 +37,9 @@ async def add_song(song: SongCreate, session: AsyncSession = Depends(get_db_sess
3537

3638

3739
@router.post("/city", response_model=CityRead)
38-
async def create_city(*, session: AsyncSession = Depends(get_db_session), city: CityCreate):
40+
async def create_city(
41+
*, session: AsyncSession = Depends(get_db_session), city: CityCreate
42+
):
3943
# new_city = City(**city.dict())
4044
new_city = City(name=city.name)
4145
session.add(new_city)
@@ -46,11 +50,13 @@ async def create_city(*, session: AsyncSession = Depends(get_db_session), city:
4650

4751
@router.post("/connect_city_with_song")
4852
async def connect_city_with_song(
49-
city_title: str, song_title: str, session: AsyncSession = Depends(get_db_session)
53+
city_title: str, song_title: str, session: AsyncSession = Depends(get_db_session)
5054
):
5155
city_in_db = await session.scalars(select(City).where(City.name == city_title))
5256
city = city_in_db.first()
53-
song_in_db = await session.scalars(select(Song).where(Song.name.like(f"%{song_title}%")))
57+
song_in_db = await session.scalars(
58+
select(Song).where(Song.name.like(f"%{song_title}%"))
59+
)
5460
song = song_in_db.first()
5561
song.city = city
5662
session.add(song)
@@ -60,7 +66,9 @@ async def connect_city_with_song(
6066

6167

6268
@router.post("/tags", response_model=TagRead)
63-
async def create_tag(*, session: AsyncSession = Depends(get_db_session), tag: TagCreate):
69+
async def create_tag(
70+
*, session: AsyncSession = Depends(get_db_session), tag: TagCreate
71+
):
6472
input_tag = TagCreate.model_validate(tag)
6573
new_tag = Tag(**input_tag.dict())
6674
session.add(new_tag)
@@ -71,14 +79,17 @@ async def create_tag(*, session: AsyncSession = Depends(get_db_session), tag: Ta
7179

7280
@router.post("/attach-tags")
7381
async def attach_tag_to_song(
74-
tag_title: str, song_name: str, session: AsyncSession = Depends(get_db_session)
82+
tag_title: str, song_name: str, session: AsyncSession = Depends(get_db_session)
7583
):
7684
tag_in_db = await session.scalars(
7785
select(Tag).where(Tag.title.like(f"%{tag_title}%"))
7886
)
7987
tag = tag_in_db.first()
8088
song_in_db = await session.scalars(
81-
select(Song).options(selectinload(Song.tags)).where(Song.name.like(f"%{song_name}%")))
89+
select(Song)
90+
.options(selectinload(Song.tags))
91+
.where(Song.name.like(f"%{song_name}%"))
92+
)
8293
song = song_in_db.first()
8394
song.tags.append(tag)
8495
session.add(tag)

app/songs/models.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class Song(Base):
2222
city_id: Mapped[int | None] = mapped_column(ForeignKey("cities.id"))
2323

2424
city: Mapped["City"] = relationship("City", back_populates="songs")
25-
tags: Mapped[list["Tag"]] = relationship("Tag", back_populates="songs", secondary=SongTag.__table__)
25+
tags: Mapped[list["Tag"]] = relationship(
26+
"Tag", back_populates="songs", secondary=SongTag.__table__
27+
)
2628

2729

2830
class Tag(Base):
@@ -32,7 +34,9 @@ class Tag(Base):
3234
title: Mapped[str] = Column(String)
3335
description: Mapped[str] = Column(String)
3436

35-
songs: Mapped[list["Song"]] = relationship(back_populates="tags", secondary=SongTag.__table__)
37+
songs: Mapped[list["Song"]] = relationship(
38+
back_populates="tags", secondary=SongTag.__table__
39+
)
3640

3741

3842
class City(Base):

app/tasks/tasks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
async def get_songs():
1414
async with async_session() as session:
15-
await session.execute(update(Song).where(Song.id == 1).values(year=Song.year + 1))
15+
await session.execute(
16+
update(Song).where(Song.id == 1).values(year=Song.year + 1)
17+
)
1618
await session.commit()
1719

1820

0 commit comments

Comments
 (0)