Skip to content

Commit

Permalink
Improve API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Serious-senpai committed Sep 20, 2024
1 parent e3d6ac8 commit 02ab135
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 24 deletions.
9 changes: 9 additions & 0 deletions server/models/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class PersonalInfo(pydantic.BaseModel):
phone: Optional[str]
email: Optional[str]

def to_personal_info(self) -> PersonalInfo:
return PersonalInfo(
name=self.name,
room=self.room,
birthday=self.birthday,
phone=self.phone,
email=self.email,
)


class AccountInfo(PersonalInfo, Authorization):
"""Data model for objects holding resident account information"""
Expand Down
28 changes: 16 additions & 12 deletions server/models/reg_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Optional

import aioodbc # type: ignore # dead PR: https://github.com/aio-libs/aioodbc/pull/429
import pyodbc # type: ignore

from .info import HashedAccountInfo
from .residents import Resident
Expand Down Expand Up @@ -72,23 +73,26 @@ async def create(
email: Optional[str],
username: str,
password: str,
) -> RegisterRequest:
) -> Optional[RegisterRequest]:
hashed_password = hash_password(password)

async with Database.instance.pool.acquire() as connection:
async with connection.cursor() as cursor:
request_id = generate_id()
await cursor.execute(
"INSERT INTO register_queue VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
request_id,
name,
room,
birthday,
phone,
email,
username,
hashed_password,
)
try:
await cursor.execute(
"INSERT INTO register_queue VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
request_id,
name,
room,
birthday,
phone,
email,
username,
hashed_password,
)
except pyodbc.DatabaseError:
return None

return cls(
id=request_id,
Expand Down
7 changes: 4 additions & 3 deletions server/routes/api/admin/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
"/admin/login",
name="Administrators login",
tags=["authorization", "admin"],
response_model=None,
responses={status.HTTP_403_FORBIDDEN: {}},
status_code=status.HTTP_200_OK,
status_code=status.HTTP_204_NO_CONTENT,
)
async def admin_login(headers: Annotated[Authorization, Header()]) -> None:
"""Verify administrator authorization data, return 200 on success, 403 on failure"""
"""Verify administrator authorization data, return 204 on success, 403 on failure"""
if await Database.instance.verify_admin(headers.username, headers.password):
return
return None

raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
6 changes: 3 additions & 3 deletions server/routes/api/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from fastapi import HTTPException, Header, status

from ...models import Authorization, Resident
from ...models import Authorization, PersonalInfo, Resident
from ...routers import api_router
from ...utils import check_password

Expand All @@ -17,7 +17,7 @@
responses={status.HTTP_403_FORBIDDEN: {}},
status_code=status.HTTP_200_OK,
)
async def login(headers: Annotated[Authorization, Header()]) -> Resident:
async def login(headers: Annotated[Authorization, Header()]) -> PersonalInfo:
"""Verify authorization data, return resident information on success."""
resident = await Resident.from_username(headers.username)
if resident is None:
Expand All @@ -32,4 +32,4 @@ async def login(headers: Annotated[Authorization, Header()]) -> Resident:
detail="Incorrect password",
)

return resident
return resident.to_personal_info()
24 changes: 20 additions & 4 deletions server/routes/api/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

from typing import Annotated

from fastapi import Header
from fastapi import HTTPException, Header, status

from ...models import Authorization, PersonalInfo, RegisterRequest
from ...routers import api_router


@api_router.post("/register", name="Residents register", tags=["authorization", "resident"])
@api_router.post(
"/register",
name="Residents register",
tags=["authorization", "resident"],
response_model=None,
responses={status.HTTP_400_BAD_REQUEST: {}},
status_code=status.HTTP_204_NO_CONTENT,
)
async def register(
data: PersonalInfo,
headers: Annotated[Authorization, Header()],
) -> RegisterRequest:
) -> None:
"""Register a resident account to be created."""
return await RegisterRequest.create(
request = await RegisterRequest.create(
name=data.name,
room=data.room,
birthday=data.birthday,
Expand All @@ -23,3 +30,12 @@ async def register(
username=headers.username,
password=headers.password,
)

if request is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Failed to create registration request",
)

else:
return None
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exclude =
.venv
__pycache__
__init__.py
extern
app

[mypy]
exclude = extern
exclude = app

0 comments on commit 02ab135

Please sign in to comment.