Skip to content

Commit

Permalink
Convert username to lowercase, update request models for presets
Browse files Browse the repository at this point in the history
Removed the suite attribute from PresetArgs, created a new model
for /update with optional preset fields

Signed-off-by: Devansh Singh <[email protected]>
  • Loading branch information
Devansh3712 committed Sep 4, 2024
1 parent 72c88d9 commit 1b9424f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
25 changes: 18 additions & 7 deletions src/teuthology_api/routes/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlmodel import Session

from teuthology_api.models import get_db, Presets
from teuthology_api.schemas.preset import PresetArgs
from teuthology_api.schemas.preset import PresetArgs, PresetUpdateArgs
from teuthology_api.services.helpers import get_token, get_username
from teuthology_api.services.presets import PresetsDatabaseException, PresetsService

Expand All @@ -16,6 +16,8 @@

@router.get("/", status_code=status.HTTP_200_OK)
def read_preset(username: str, name: str, db: Session = Depends(get_db)):
# GitHub usernames are case-insensitive
username = username.lower()
db_preset = PresetsService(db).get_by_username_and_name(username, name)
if db_preset is None:
raise HTTPException(
Expand All @@ -29,6 +31,7 @@ def read_preset(username: str, name: str, db: Session = Depends(get_db)):
def read_all_presets(
username: str, suite: Optional[str] = None, db: Session = Depends(get_db)
):
username = username.lower()
if suite:
db_presets = PresetsService(db).get_by_username_and_suite(username, suite)
else:
Expand Down Expand Up @@ -56,7 +59,7 @@ def add_preset(
headers={"WWW-Authenticate": "Bearer"},
)

username = get_username(request)
username = get_username(request).lower()
db_presets = PresetsService(db).get_by_username(username)
if len(db_presets) == 10:
if not replace:
Expand All @@ -77,14 +80,16 @@ def add_preset(
detail=f"Preset with name {preset.name} exists",
)

db_preset = Presets(**preset.model_dump(), username=username)
db_preset = Presets(
**preset.model_dump(), username=username, suite=preset.cmd["--suite"]
)
return PresetsService(db).create(db_preset)


@router.put("/edit/{preset_id}", status_code=status.HTTP_200_OK)
def update_preset(
preset_id: int,
updated_preset: PresetArgs,
preset: PresetUpdateArgs,
db: Session = Depends(get_db),
access_token: str = Depends(get_token),
):
Expand All @@ -95,9 +100,15 @@ def update_preset(
headers={"WWW-Authenticate": "Bearer"},
)
try:
return PresetsService(db).update(
preset_id, updated_preset.model_dump(exclude_unset=True)
)
updated_preset = preset.model_dump(exclude_unset=True)
if updated_preset == {}:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Nothing new to update",
)
if preset.cmd:
updated_preset["suite"] = preset.cmd["--suite"]
return PresetsService(db).update(preset_id, updated_preset)
except PresetsDatabaseException as exc:
raise HTTPException(status_code=exc.code, detail=str(exc))

Expand Down
14 changes: 12 additions & 2 deletions src/teuthology_api/schemas/preset.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
from typing import Optional

from pydantic import BaseModel

from teuthology_api.schemas.suite import SuiteArgs


class PresetArgs(BaseModel):
name: str
suite: str
cmd: SuiteArgs

def model_post_init(self, __context):
self.cmd = self.cmd.model_dump()
self.cmd = self.cmd.model_dump(by_alias=True, exclude_unset=True)


class PresetUpdateArgs(BaseModel):
name: Optional[str] = None
cmd: Optional[SuiteArgs] = None

def model_post_init(self, __context):
if self.cmd:
self.cmd = self.cmd.model_dump(by_alias=True, exclude_unset=True)

0 comments on commit 1b9424f

Please sign in to comment.