Skip to content

Commit

Permalink
Return resident ID from API when logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
Serious-senpai committed Sep 21, 2024
1 parent ac1d269 commit 9935647
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 42 deletions.
1 change: 1 addition & 0 deletions server/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .info import *
from .reg_request import *
from .residents import *
from .snowflake import *
22 changes: 13 additions & 9 deletions server/models/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import pydantic

from .auth import Authorization, HashedAuthorization
from .snowflake import Snowflake


__all__ = ("PersonalInfo", "AccountInfo", "HashedAccountInfo")
__all__ = ("PersonalInfo", "PublicInfo")


class PersonalInfo(pydantic.BaseModel):
Expand All @@ -30,11 +30,15 @@ def to_personal_info(self) -> PersonalInfo:
)


class AccountInfo(PersonalInfo, Authorization):
"""Data model for objects holding resident account information"""
pass
class PublicInfo(Snowflake, PersonalInfo):
"""Data model for objects holding personal info along with a snowflake ID."""


class HashedAccountInfo(PersonalInfo, HashedAuthorization):
"""Data model for objects holding resident account information with hashed password."""
pass
def to_public_info(self) -> PublicInfo:
return PublicInfo(
id=self.id,
name=self.name,
room=self.room,
birthday=self.birthday,
phone=self.phone,
email=self.email,
)
15 changes: 7 additions & 8 deletions server/models/reg_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
import aioodbc # type: ignore # dead PR: https://github.com/aio-libs/aioodbc/pull/429
import pyodbc # type: ignore

from .info import HashedAccountInfo
from .auth import HashedAuthorization
from .info import PublicInfo
from .residents import Resident
from ..database import Database
from ..utils import generate_id, hash_password, snowflake_time
from ..utils import generate_id, hash_password


__all__ = ("RegisterRequest",)


class RegisterRequest(HashedAccountInfo):
"""Data model for objects holding information about a registration request."""
class RegisterRequest(PublicInfo, HashedAuthorization):
"""Data model for objects holding information about a registration request.
id: int
Each object of this class corresponds to a database row."""

@property
def created_at(self) -> datetime:
return snowflake_time(self.id)
id: int

async def __remove_from_db(self, *, cursor: aioodbc.Cursor) -> None:
await cursor.execute("DELETE FROM register_queue WHERE request_id = ?", self.id)
Expand Down
15 changes: 5 additions & 10 deletions server/models/residents.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
from __future__ import annotations

import asyncio
from datetime import datetime
from typing import Any, ClassVar, Dict, Optional, TypeVar

from .info import HashedAccountInfo
from .auth import HashedAuthorization
from .info import PublicInfo
from ..database import Database
from ..utils import snowflake_time


__all__ = ("Resident",)
T = TypeVar("T")


class Resident(HashedAccountInfo):
"""Data model for objects holding information about a resident."""
class Resident(PublicInfo, HashedAuthorization):
"""Data model for objects holding information about a resident.
id: int

@property
def created_at(self) -> datetime:
return snowflake_time(self.id)
Each object of this class corresponds to a database row."""

__cache_by_id: ClassVar[Dict[int, Resident]] = {}
__cache_by_username: ClassVar[Dict[str, Resident]] = {}
Expand Down
21 changes: 21 additions & 0 deletions server/models/snowflake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

from datetime import datetime

import pydantic

from ..utils import snowflake_time


__all__ = ("Snowflake",)


class Snowflake(pydantic.BaseModel):
"""Data model for snowflake IDs"""

id: int

@property
def created_at(self) -> datetime:
"""The time at which this snowflake was created"""
return snowflake_time(self.id)
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, PersonalInfo, Resident
from ...models import Authorization, PublicInfo, 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()]) -> PersonalInfo:
async def login(headers: Annotated[Authorization, Header()]) -> PublicInfo:
"""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()]) -> PersonalInfo:
detail="Incorrect password",
)

return resident.to_personal_info()
return resident.to_public_info()
12 changes: 0 additions & 12 deletions server/snowflake.py

This file was deleted.

0 comments on commit 9935647

Please sign in to comment.