8000 Refactor and update docs in manager.py by othmane099 · Pull Request #1476 · fastapi-users/fastapi-users · GitHub
[go: up one dir, main page]

Skip to content

Refactor and update docs in manager.py #1476

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions fastapi_users/manager.py
Original file line number Diff line number Diff line change
10000 Expand Up @@ -57,17 +57,17 @@ def parse_id(self, value: Any) -> models.ID:

:param value: The value to parse.
:raises InvalidID: The models.ID value is invalid.
:return: An models.ID object.
:return: A models.ID object.
"""
raise NotImplementedError() # pragma: no cover

async def get(self, id: models.ID) -> models.UP:
"""
Get a user by id.

:param id: Id. of the user to retrieve.
:param id: Id of the user to retrieve.
:raises UserNotExists: The user does not exist.
:return: A user.
:return: A user of type models.UP.
"""
user = await self.user_db.get(id)

Expand All @@ -82,7 +82,7 @@ async def get_by_email(self, user_email: str) -> models.UP:

:param user_email: E-mail of the user to retrieve.
:raises UserNotExists: The user does not exist.
:return: A user.
:return: A user of type models.UP.
"""
user = await self.user_db.get_by_email(user_email)

Expand All @@ -96,9 +96,9 @@ async def get_by_oauth_account(self, oauth: str, account_id: str) -> models.UP:
Get a user by OAuth account.

:param oauth: Name of the OAuth client.
:param account_id: Id. of the account on the external OAuth service.
:param account_id: Id of the account on the external OAuth service.
:raises UserNotExists: The user does not exist.
:return: A user.
:return: A user of type models.UP.
"""
user = await self.user_db.get_by_oauth_account(oauth, account_id)

Expand All @@ -124,7 +124,7 @@ async def create(
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
:raises UserAlreadyExists: A user already exists with the same e-mail.
:return: A new user.
:return: A new user of type models.UP.
"""
await self.validate_password(user_create.password, user_create)

Expand Down Expand Up @@ -173,7 +173,7 @@ async def oauth_callback(

:param oauth_name: Name of the OAuth client.
:param access_token: Valid access token for the service provider.
:param account_id: models.ID of the user on the service provider.
:param account_id: Id of the account on the external OAuth service.
:param account_email: E-mail of the user on the service provider.
:param expires_at: Optional timestamp at which the access token expires.
:param refresh_token: Optional refresh token to get a
Expand All @@ -186,7 +186,7 @@ async def oauth_callback(
set to `True` on newly created user. Make sure the OAuth Provider you're
using does verify the email address before enabling this flag.
Defaults to False.
:return: A user.
:return: A user of type models.UOAP.
"""
oauth_account_dict = {
"oauth_name": oauth_name,
Expand Down Expand Up @@ -248,14 +248,13 @@ async def oauth_associate_callback(

:param oauth_name: Name of the OAuth client.
:param access_token: Valid access token for the service provider.
:param account_id: models.ID of the user on the service provider.
:param account_email: E-mail of the user on the service provider.
: :param account_id: Id of the account on the external OAuth service. :param account_email: E-mail of the user on the service provider.
:param expires_at: Optional timestamp at which the access token expires.
:param refresh_token: Optional refresh token to get a
fresh access token from the service provider.
:param request: Optional FastAPI request that
triggered the operation, defaults to None
:return: A user.
:return: A user of type models.UOAP.
"""
oauth_account_dict = {
"oauth_name": oauth_name,
Expand Down Expand Up @@ -316,7 +315,7 @@ async def verify(self, token: str, request: Optional[Request] = None) -> models.
triggered the operation, defaults to None.
:raises InvalidVerifyToken: The token is invalid or expired.
:raises UserAlreadyVerified: The user is already verified.
:return: The verified user.
:return: A verified user of type models.UP.
"""
try:
data = decode_jwt(
Expand Down Expand Up @@ -398,7 +397,7 @@ async def reset_password(
:raises InvalidResetPasswordToken: The token is invalid or expired.
:raises UserInactive: The user is inactive.
:raises InvalidPasswordException: The password is invalid.
:return: The user with updated password.
:return: The user of type models.UP with updated password.
"""
try:
data = decode_jwt(
Expand Down Expand Up @@ -456,7 +455,7 @@ async def update(
will be ignored during the update, defaults to False
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
:return: The updated user.
:return: The updated user of type models.UP.
"""
if safe:
updated_user_data = user_update.create_update_dict()
Expand Down Expand Up @@ -642,6 +641,8 @@ async def authenticate(
Will automatically upgrade password hash if necessary.

:param credentials: The user credentials.
:return: The authenticated user of type models.UP if credentials are valid,
otherwise None.
"""
try:
user = await self.get_by_email(credentials.username)
Expand Down Expand Up @@ -683,6 +684,10 @@ async def _update(self, user: models.UP, update_dict: dict[str, Any]) -> models.


class UUIDIDMixin:
"""
Mixin for parsing and validating Id of type UUID.
"""

def parse_id(self, value: Any) -> uuid.UUID:
if isinstance(value, uuid.UUID):
return value
Expand All @@ -693,6 +698,10 @@ def parse_id(self, value: Any) -> uuid.UUID:


class IntegerIDMixin:
"""
Mixin for parsing and validating Id of type Integer.
"""

def parse_id(self, value: Any) -> int:
if isinstance(value, float):
raise exceptions.InvalidID()
Expand Down
0