Files
Matyáš Richter 48d1be87a4 Added codestyle check and lint to the build action (#798)
* Applied `make format`

* Added format-check to makefile

* Added a "Check codestyle" step to the build action.

* Rerun `make format`

* Added a "lint" target to makefile

* Added lint step to the build action

* Added W503 to the list of ignored flake8 errors.

See https://www.flake8rules.com/rules/W503.html
2021-11-23 08:13:11 +01:00

53 lines
1.5 KiB
Python

from typing import Type
from fastapi import APIRouter, Depends, HTTPException, Request, status
from fastapi_users import models
from fastapi_users.manager import (
BaseUserManager,
InvalidPasswordException,
UserAlreadyExists,
UserManagerDependency,
)
from fastapi_users.router.common import ErrorCode
def get_register_router(
get_user_manager: UserManagerDependency[models.UC, models.UD],
user_model: Type[models.U],
user_create_model: Type[models.UC],
) -> APIRouter:
"""Generate a router with the register route."""
router = APIRouter()
@router.post(
"/register",
response_model=user_model,
status_code=status.HTTP_201_CREATED,
name="register:register",
)
async def register(
request: Request,
user: user_create_model, # type: ignore
user_manager: BaseUserManager[models.UC, models.UD] = Depends(get_user_manager),
):
try:
created_user = await user_manager.create(user, safe=True, request=request)
except UserAlreadyExists:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ErrorCode.REGISTER_USER_ALREADY_EXISTS,
)
except InvalidPasswordException as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={
"code": ErrorCode.REGISTER_INVALID_PASSWORD,
"reason": e.reason,
},
)
return created_user
return router