-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow updating resident login information
- Loading branch information
1 parent
9dfa869
commit cf8b5f1
Showing
7 changed files
with
199 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .me import * | ||
from .update_authorization import * | ||
from .update import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Annotated, Optional | ||
|
||
import pydantic | ||
from fastapi import Depends, Response, status | ||
|
||
from ...app import api_v1 | ||
from ...models import Resident, Result | ||
from ...utils import check_password | ||
|
||
|
||
__all__ = ("residents_update_authorization",) | ||
|
||
|
||
class _Payload(pydantic.BaseModel): | ||
new_username: Annotated[str, pydantic.Field(description="The new authorization username")] | ||
old_password: Annotated[str, pydantic.Field(description="The old authorization password")] | ||
new_password: Annotated[str, pydantic.Field(description="The new authorization password")] | ||
|
||
|
||
@api_v1.post( | ||
"/residents/update-authorization", | ||
name="Residents authorization info update", | ||
description="Update authorization information of a resident", | ||
tags=["resident"], | ||
responses={ | ||
status.HTTP_200_OK: { | ||
"description": "The operation completed successfully", | ||
"model": Result[Resident], | ||
}, | ||
status.HTTP_400_BAD_REQUEST: { | ||
"description": "Incorrect authorization data", | ||
"model": Result[None], | ||
}, | ||
}, | ||
) | ||
async def residents_update_authorization( | ||
resident: Annotated[Result[Optional[Resident]], Depends(Resident.from_token)], | ||
response: Response, | ||
payload: _Payload, | ||
) -> Result[Optional[Resident]]: | ||
if resident.data is None or not check_password(payload.old_password, hashed=resident.data.hashed_password): | ||
response.status_code = status.HTTP_400_BAD_REQUEST | ||
return Result(code=402, data=None) | ||
|
||
result = await resident.data.update_authorization(payload.new_username, payload.new_password) | ||
if result.data is None: | ||
response.status_code = status.HTTP_400_BAD_REQUEST | ||
|
||
return result |