From f0562425165e3c71b442e2b2ba1473b036d9047c Mon Sep 17 00:00:00 2001 From: Vallari Agrawal Date: Mon, 11 Sep 2023 18:23:48 +0530 Subject: [PATCH] Add github auth to /add /edit /delete Signed-off-by: Vallari Agrawal --- src/models/presets.py | 5 ++--- src/routes/presets.py | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/models/presets.py b/src/models/presets.py index f51d2d0..b11a0da 100644 --- a/src/models/presets.py +++ b/src/models/presets.py @@ -1,6 +1,5 @@ from sqlalchemy import Column, Integer, String, UniqueConstraint from sqlalchemy.orm import Session -from schemas.presets import PresetSchema from . import Base @@ -21,8 +20,8 @@ def __init__(self, message, code): self.code = code -def create_preset(db: Session, preset: PresetSchema): - new_preset = Presets(**preset.model_dump()) +def create_preset(db: Session, preset): + new_preset = Presets(**preset) db.add(new_preset) db.commit() db.refresh(new_preset) diff --git a/src/routes/presets.py b/src/routes/presets.py index 98356e3..74dc6b4 100644 --- a/src/routes/presets.py +++ b/src/routes/presets.py @@ -1,10 +1,12 @@ from fastapi import APIRouter, HTTPException, Depends, Response +from sqlalchemy.orm import Session +import logging + +from services.helpers import get_token from models import get_db from models.presets import PresetsDatabaseException from models import presets as presets_model from schemas.presets import PresetSchema -from sqlalchemy.orm import Session -import logging log = logging.getLogger(__name__) @@ -31,7 +33,17 @@ def read_preset(username: str, db: Session = Depends(get_db)): @router.post("/add", status_code=200) -def add_preset(preset: PresetSchema, db: Session = Depends(get_db)): +def add_preset( + preset: PresetSchema, + db: Session = Depends(get_db), + access_token: str = Depends(get_token), +): + if not access_token: + raise HTTPException( + status_code=401, + detail="You need to be logged in", + headers={"WWW-Authenticate": "Bearer"}, + ) db_preset = presets_model.get_preset_by_username_name( db, username=preset.username, preset_name=preset.name ) @@ -39,13 +51,22 @@ def add_preset(preset: PresetSchema, db: Session = Depends(get_db)): raise HTTPException( status_code=400, detail=f"Preset of this username & name already exists." ) - return presets_model.create_preset(db, preset) + return presets_model.create_preset(db, preset.model_dump()) @router.put("/edit/{preset_id}", status_code=200) def update_preset( - preset_id: int, updated_data: PresetSchema, db: Session = Depends(get_db) + preset_id: int, + updated_data: PresetSchema, + db: Session = Depends(get_db), + access_token: str = Depends(get_token), ): + if not access_token: + raise HTTPException( + status_code=401, + detail="You need to be logged in", + headers={"WWW-Authenticate": "Bearer"}, + ) try: return presets_model.update_preset( db, preset_id, updated_data.model_dump(exclude_unset=True) @@ -58,7 +79,17 @@ def update_preset( @router.delete("/delete/{preset_id}", status_code=204) -def delete_preset(preset_id: int, db: Session = Depends(get_db)): +def delete_preset( + preset_id: int, + db: Session = Depends(get_db), + access_token: str = Depends(get_token), +): + if not access_token: + raise HTTPException( + status_code=401, + detail="You need to be logged in", + headers={"WWW-Authenticate": "Bearer"}, + ) try: presets_model.delete_preset(db, preset_id) except PresetsDatabaseException as exc: