Skip to content

Commit

Permalink
Add check for preset limit
Browse files Browse the repository at this point in the history
Added a limit on how many presets a user can store at
a time (currently 10)

Signed-off-by: Devansh Singh <[email protected]>
  • Loading branch information
Devansh3712 committed Aug 8, 2024
1 parent 54099fc commit c3506eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/teuthology_api/routes/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def read_all_presets(username: str, db: Session = Depends(get_db)):
@router.post("/add", status_code=status.HTTP_201_CREATED)
def add_preset(
preset: Presets,
replace: bool = False,
db: Session = Depends(get_db),
access_token: str = Depends(get_token),
):
Expand All @@ -46,6 +47,18 @@ def add_preset(
detail="You need to be logged in",
headers={"WWW-Authenticate": "Bearer"},
)

db_presets = PresetsService(db).get_by_username(preset.username)
if len(db_presets) == 1:
if not replace:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Only 10 presets for a user can be stored at a time.",
)
# If replace parameter is set true, delete the
# oldest preset of the user and create a new one
PresetsService(db).delete(db_presets[0].id)

db_preset_exists = PresetsService(db).get_by_username_and_name(
preset.username, preset.name
)
Expand Down
4 changes: 3 additions & 1 deletion src/teuthology_api/services/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def __init__(self, db: Session) -> None:
self.db = db

def get_by_username(self, username: str):
statement = select(Presets).where(Presets.username == username)
statement = (
select(Presets).where(Presets.username == username).order_by(Presets.id)
)
db_presets = self.db.exec(statement).all()
return db_presets

Expand Down

0 comments on commit c3506eb

Please sign in to comment.