Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Furrior committed Mar 1, 2025
1 parent 6a41be4 commit e54291d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
22 changes: 20 additions & 2 deletions app/routes/v1/donate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from app.database.models import Donation, Player
from app.deps import SessionDep, verify_bearer
from app.routes.v1.player import get_or_create_player_by_discord_id
from app.schemas.donate import NewDonationDiscord
from app.schemas.donate import DonationPatch, NewDonationDiscord
from app.schemas.generic import PaginatedResponse, paginate_selection

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -46,6 +46,10 @@ async def get_donations(session: SessionDep,
return paginate_selection(session, selection, request, page, page_size)


@router.get("/{id}", status_code=status.HTTP_200_OK)
async def get_donation_by_id(session: SessionDep, id: int) -> Donation:
return session.exec(select(Donation).where(Donation.id == id)).first()

async def create_donation_helper(session: SessionDep, donation: Donation) -> Donation:
session.add(donation)
session.commit()
Expand All @@ -70,7 +74,21 @@ async def create_donation_by_discord(session: SessionDep, new_donation: NewDonat

donation = Donation(
player_id=player.id,
tier=new_donation.tier
tier=new_donation.tier,
issue_time=datetime.datetime.now(),
expiration_time=datetime.datetime.now() + datetime.timedelta(days=new_donation.duration_days),
valid=True
)

return await create_donation_helper(session, donation)


@router.patch("/{id}", status_code=status.HTTP_200_OK, dependencies=[Depends(verify_bearer)])
async def update_donation(session: SessionDep, id: int, donation_patch: DonationPatch) -> Donation: # pylint: disable=redefined-builtin
donation = await get_donation_by_id(session, id)
update_data = donation_patch.model_dump(exclude_unset=True)

for key, value in update_data.items():
setattr(donation, key, value)

session.commit()
13 changes: 10 additions & 3 deletions app/routes/v1/whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from app.database.models import Player, Whitelist, WhitelistBan
from app.deps import BEARER_DEP_RESPONSES, SessionDep, verify_bearer
from app.routes.v1.player import get_player_by_discord_id
from app.schemas.generic import PaginatedResponse, paginate_selection
from app.schemas.whitelist import (NEW_WHITELIST_BAN_TYPES,
NEW_WHITELIST_TYPES, WhitelistPatch,
Expand All @@ -30,6 +31,7 @@ def select_only_active_whitelists(selection: SelectOfScalar[Whitelist]):
def filter_whitelists(selection: SelectOfScalar[Whitelist],
ckey: str | None = None,
discord_id: str | None = None,
admin_id: int | None = None,
server_type: str | None = None,
active_only: bool = True) -> SelectOfScalar[Whitelist]:
if active_only:
Expand All @@ -38,6 +40,8 @@ def filter_whitelists(selection: SelectOfScalar[Whitelist],
selection = selection.where(Player.ckey == ckey)
if discord_id is not None:
selection = selection.where(Player.discord_id == discord_id)
if admin_id is not None:
selection = selection.where(Whitelist.admin_id == admin_id)
if server_type is not None:
selection = selection.where(Whitelist.server_type == server_type)
return selection
Expand Down Expand Up @@ -71,15 +75,18 @@ async def get_whitelists(session: SessionDep,
request: Request,
ckey: str | None = None,
discord_id: str | None = None,
admin_discord_id: str | None = None,
server_type: str | None = None,
active_only: bool = True,
page: int = 1,
page_size: int = 50) -> PaginatedResponse[Whitelist]:
selection = select(Whitelist).join(
Player, Player.id == Whitelist.player_id) # type: ignore

admin_id = get_player_by_discord_id(session, admin_discord_id)

selection = filter_whitelists(
selection, ckey, discord_id, server_type, active_only)
selection, ckey, discord_id, admin_id, server_type, active_only)

return paginate_selection(session, selection, request, page, page_size)

Expand All @@ -96,7 +103,7 @@ async def get_whitelisted_ckeys(session: SessionDep,
page: int = 1,
page_size: int = 50) -> PaginatedResponse[str]:
selection = select(Player.ckey).join(
Whitelist, Player.id == Whitelist.player_id).distinct() # type: ignore
Whitelist, Player.id == Whitelist.player_id).where(Player.ckey != None).distinct() # type: ignore

selection = filter_whitelists(selection,
server_type=server_type,
Expand All @@ -112,7 +119,7 @@ async def get_whitelisted_ckeys(session: SessionDep,
status.HTTP_404_NOT_FOUND: {"description": "Whitelist not found"},
})
def get_whitelist(session: SessionDep,
id: int): # pylint: disable=redefined-builtin
id: int) -> Whitelist: # pylint: disable=redefined-builtin
wl = session.exec(select(Whitelist).where(Whitelist.id == id)).first()

if wl is None:
Expand Down
5 changes: 4 additions & 1 deletion app/schemas/donate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

class NewDonationBase(BaseModel):
tier: int

duration_days: int = 30

class NewDonationDiscord(NewDonationBase):
discord_id: str

class DonationPatch(BaseModel):
expiration_time: datetime.datetime

0 comments on commit e54291d

Please sign in to comment.