-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
||
|
||
@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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
@router.post( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
@router.post("/", response_model=UserResponse, dependencies=[AdminUser]) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
raise HTTPException(status_code=400, detail="Passwords don't match") | ||
|
||
# Update the password | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
@router.get("/me/", response_model=UserResponse) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def get_user(username) -> Optional[User]: | ||
|
There was a problem hiding this comment.
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:inline-immediately-returned-variable
)