Exemple #1
0
def get_current_active_user(
    token: str = fastapi.Depends(oauth2_scheme),
    token_service: auth.TokenService = fastapi.Depends(
        service_locator.default().token_service
    ),
    user_service: auth.UserService = fastapi.Depends(
        service_locator.default().user_service
    ),
) -> auth.User:
    username = token_service.username(token=token)
    if active_user := user_service.get_user(username):
        return active_user
Exemple #2
0
async def register(
    username: str,
    email: pydantic.EmailStr,
    password: str,
    user_service: auth.UserService = fastapi.Depends(
        service_locator.default().user_service
    ),
    password_hasher: auth.PasswordHashService = fastapi.Depends(
        service_locator.default().password_hasher
    ),
) -> response.UserResponse:
    pw_hash = password_hasher.create(pydantic.SecretStr(password))
    user = user_service.create_user(
        username=username, email=email, password_hash=pw_hash
    )
    return response.UserResponse.from_domain(user)
Exemple #3
0
async def delete_todo(
    todo_id: int,
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> None:
    todo_service.delete_todo(user_id=current_user.user_id, todo_id=todo_id)
Exemple #4
0
async def all_todos(
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> typing.List[response.TodoResponse]:
    todos = todo_service.all(current_user.user_id)
    return [response.TodoResponse.from_domain(t) for t in todos]
Exemple #5
0
async def update_yearly_todo(
    todo_id: int,
    description: typing.Optional[str] = None,
    start_date: typing.Optional[datetime.date] = None,
    month: typing.Optional[int] = None,
    day: typing.Optional[int] = None,
    note: typing.Optional[str] = None,
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> response.TodoResponse:
    updates: typing.Dict[str, typing.Any] = {}
    if description is not None:
        updates["description"] = description
    if month is not None:
        updates["month"] = month
    if day is not None:
        updates["day"] = month
    if note is not None:
        updates["note"] = note
    if start_date is not None:
        updates["date"] = start_date

    return update_todo(
        user_id=current_user.user_id,
        todo_id=todo_id,
        todo_service=todo_service,
        updates=updates,
    )
Exemple #6
0
async def add_daily_todo(
    description: str,
    note: typing.Optional[str] = None,
    start_date: typing.Optional[datetime.date] = None,
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> response.TodoResponse:
    if start_date is None:
        start_date = datetime.date.today()
    if note is None:
        note = ""
    daily_todo = todo.Daily(
        advance_days=0,
        category=core.TodoCategory.Todo,
        date_added=datetime.date.today(),
        date_completed=None,
        description=description,
        note=note,
        start_date=start_date,
        todo_id=-1,
        user_id=current_user.user_id,
    )
    new_todo = todo_service.add_todo(user_id=current_user.user_id,
                                     todo=daily_todo)
    return response.TodoResponse.from_domain(new_todo)
Exemple #7
0
async def update_monthly_todo(
    todo_id: int,
    description: typing.Optional[str] = None,
    advance_days: typing.Optional[int] = None,
    month_day: typing.Optional[int] = None,
    note: typing.Optional[str] = None,
    start_date: typing.Optional[datetime.date] = None,
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> response.TodoResponse:
    if month_day and month_day not in range(1, 28):
        raise fastapi.HTTPException(
            status_code=HTTP_400_BAD_REQUEST,
            detail="month_day must be between 1 and 28",
        )

    updates: typing.Dict[str, typing.Any] = {}
    if description is not None:
        updates["description"] = description
    if advance_days is not None:
        updates["advance_days"] = advance_days
    if month_day is not None:
        updates["month_day"] = month_day
    if note is not None:
        updates["note"] = note
    if start_date is not None:
        updates["start_date"] = start_date

    return update_todo(
        user_id=current_user.user_id,
        todo_id=todo_id,
        todo_service=todo_service,
        updates=updates,
    )
Exemple #8
0
async def add_monthly_todo(
    description: str,
    advance_days: int,
    month_day: int,
    note: typing.Optional[str] = None,
    start_date: typing.Optional[datetime.date] = None,
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> response.TodoResponse:
    if month_day not in range(1, 28):
        raise fastapi.HTTPException(
            status_code=HTTP_400_BAD_REQUEST,
            detail="month_day must be between 1 and 28",
        )

    if start_date is None:
        start_date = datetime.date.today()
    if note is None:
        note = ""
    monthly_todo = todo.Monthly(
        advance_days=advance_days,
        category=core.TodoCategory.Todo,
        date_added=datetime.date.today(),
        date_completed=None,
        description=description,
        note=note,
        start_date=start_date,
        todo_id=-1,
        user_id=current_user.user_id,
        month_day=month_day,
    )
    new_todo = todo_service.add_todo(user_id=current_user.user_id,
                                     todo=monthly_todo)
    return response.TodoResponse.from_domain(new_todo)
Exemple #9
0
async def login(
    form_data: OAuth2PasswordRequestForm = fastapi.Depends(),
    user_service: auth.UserService = fastapi.Depends(
        service_locator.default().user_service
    ),
    password_hasher: auth.PasswordHashService = fastapi.Depends(
        service_locator.default().password_hasher
    ),
    token_service: auth.TokenService = fastapi.Depends(
        service_locator.default().token_service
    ),
) -> auth.Token:
    try:
        user = user_service.get_user(form_data.username)
    except core.exception.UserNotFound:
        raise exceptions.CREDENTIALS_EXCEPTION
    else:
        if password_hasher.verify(
            hashed_password=user.password_hash,
            plain_password=pydantic.SecretStr(form_data.password),
        ):
            return token_service.create({"sub": user.username})
        else:
            raise exceptions.CREDENTIALS_EXCEPTION
Exemple #10
0
async def add_irregular_todo(
    description: str,
    advance_days: int,
    month: int,
    week_day: int,
    week: int,
    note: typing.Optional[str] = None,
    start_date: typing.Optional[datetime.date] = None,
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> response.TodoResponse:
    validation_errors: typing.List[str] = []
    if month not in range(1, 12):
        validation_errors.append("month must be between 1 and 12")
    if week_day not in range(1, 7):
        validation_errors.append("week_day must be between 1 and 7")
    if week not in range(1, 5):
        validation_errors.append("week must be between 1 and 5")
    if validation_errors:
        errors_str = "; ".join(validation_errors)
        msg = f"Validation errors: {errors_str}"
        raise fastapi.HTTPException(status_code=HTTP_400_BAD_REQUEST,
                                    detail=msg)
    if start_date is None:
        start_date = datetime.date.today()
    if note is None:
        note = ""
    irregular_todo = todo.Irregular(
        advance_days=advance_days,
        category=core.TodoCategory.Todo,
        date_added=datetime.date.today(),
        date_completed=None,
        description=description,
        note=note,
        start_date=start_date,
        todo_id=-1,
        user_id=current_user.user_id,
        month=todo.Month(month),
        week_day=todo.Weekday(week_day),
        week_number=week,
    )
    new_todo = todo_service.add_todo(user_id=current_user.user_id,
                                     todo=irregular_todo)
    return response.TodoResponse.from_domain(new_todo)