Skip to content
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

Sourcery Starbot ⭐ refactored zxenonx/fastapi-project-template #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions fastapi_project_template/routes/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@

@router.get("/", response_model=List[ContentResponse])
async def list_contents(*, session: Session = ActiveSession):
contents = session.exec(select(Content)).all()
return contents
return session.exec(select(Content)).all()
Copy link
Author

Choose a reason for hiding this comment

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

Function list_contents refactored with the following changes:



@router.get("/{id_or_slug}/", response_model=ContentResponse)
async def query_content(
*, id_or_slug: Union[str, int], session: Session = ActiveSession
):
content = session.query(Content).where(
if content := session.query(Content).where(
or_(
Content.id == id_or_slug,
Content.slug == id_or_slug,
)
)
if not content:
):
return content.first()
else:
raise HTTPException(status_code=404, detail="Content not found")
return content.first()
Comment on lines -24 to -32
Copy link
Author

Choose a reason for hiding this comment

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

Function query_content refactored with the following changes:



@router.post(
Expand Down
14 changes: 6 additions & 8 deletions fastapi_project_template/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

@router.get("/", response_model=List[UserResponse], dependencies=[AdminUser])
async def list_users(*, session: Session = ActiveSession):
users = session.exec(select(User)).all()
return users
return session.exec(select(User)).all()
Copy link
Author

Choose a reason for hiding this comment

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

Function list_users refactored with the following changes:



@router.post("/", response_model=UserResponse, dependencies=[AdminUser])
Expand Down Expand Up @@ -58,7 +57,7 @@ async def update_user_password(
status_code=403, detail="You can't update this user password"
)

if not patch.password == patch.password_confirm:
if patch.password != patch.password_confirm:
Comment on lines -61 to +60
Copy link
Author

Choose a reason for hiding this comment

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

Function update_user_password refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

raise HTTPException(status_code=400, detail="Passwords don't match")

# Update the password
Expand All @@ -78,16 +77,15 @@ async def update_user_password(
async def query_user(
*, session: Session = ActiveSession, user_id_or_username: Union[str, int]
):
user = session.query(User).where(
if user := session.query(User).where(
or_(
User.id == user_id_or_username,
User.username == user_id_or_username,
)
)

if not user:
):
return user.first()
else:
raise HTTPException(status_code=404, detail="User not found")
return user.first()
Comment on lines -81 to -90
Copy link
Author

Choose a reason for hiding this comment

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

Function query_user refactored with the following changes:



@router.get("/me/", response_model=UserResponse)
Expand Down
13 changes: 5 additions & 8 deletions fastapi_project_template/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,17 @@ def create_access_token(
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
to_encode["exp"] = expire
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
Comment on lines -119 to +120
Copy link
Author

Choose a reason for hiding this comment

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

Function create_access_token refactored with the following changes:



def authenticate_user(
get_user: Callable, username: str, password: str
) -> Union[User, bool]:
user = get_user(username)
if not user:
return False
if not verify_password(password, user.password):
if user := get_user(username):
return user if verify_password(password, user.password) else False
else:
return False
return user
Comment on lines -127 to -132
Copy link
Author

Choose a reason for hiding this comment

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

Function authenticate_user refactored with the following changes:



def get_user(username) -> Optional[User]:
Expand Down