Skip to content

Commit

Permalink
update:google auth module
Browse files Browse the repository at this point in the history
  • Loading branch information
nrjadkry committed Jun 28, 2024
1 parent 8075f75 commit 5f3d5a1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
21 changes: 12 additions & 9 deletions src/backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
from app.users import user_routes
from loguru import logger as log
from fastapi.templating import Jinja2Templates
from fastapi.responses import RedirectResponse


root = os.path.dirname(os.path.abspath(__file__))
templates = Jinja2Templates(directory=f'templates')
templates = Jinja2Templates(directory="templates")


class InterceptHandler(logging.Handler):
Expand Down Expand Up @@ -77,7 +76,7 @@ def get_application() -> FastAPI:
debug=settings.DEBUG,
docs_url="/api/docs",
openapi_url="/api/openapi.json",
redoc_url="/api/redoc"
redoc_url="/api/redoc",
)

# Set custom logger
Expand Down Expand Up @@ -107,14 +106,17 @@ def get_application() -> FastAPI:
async def home(request: Request):
try:
"""Return Frontend HTML"""
return templates.TemplateResponse(name="index.html", context={"request": request})
except:
return templates.TemplateResponse(
name="index.html", context={"request": request}
)
except Exception:
"""Fall back if tempalate missing. Redirect home to docs."""
return RedirectResponse(f"{settings.API_PREFIX}/docs")


known_browsers = ["Mozilla", "Chrome", "Safari", "Opera", "Edge", "Firefox"]


@api.exception_handler(404)
async def custom_404_handler(request: Request, _):
"""Return Frontend HTML or throw 404 Response on 404 requests."""
Expand All @@ -125,9 +127,10 @@ async def custom_404_handler(request: Request, _):
is_browser = any(browser in user_agent for browser in known_browsers)
if format == "json" or not is_browser:
return JSONResponse(status_code=404, content={"detail": "Not found"})
return templates.TemplateResponse(name="index.html", context={"request": request})

except:
return templates.TemplateResponse(
name="index.html", context={"request": request}
)

except Exception:
"""Fall back if tempalate missing. Redirect home to docs."""
return JSONResponse(status_code=404, content={"detail": "Not found"})

6 changes: 5 additions & 1 deletion src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from sqlalchemy.orm import Session
from app.projects import project_schemas
from app.db import db_models
from app.models.enums import HTTPStatus
from loguru import logger as log
import shapely.wkb as wkblib
from shapely.geometry import shape
Expand All @@ -15,6 +14,7 @@
from fastapi import Depends
from asyncio import gather


async def get_project_by_id(
db: Session = Depends(database.get_db), project_id: Optional[int] = None
) -> db_models.DbProject:
Expand All @@ -26,6 +26,7 @@ async def get_project_by_id(
)
return await convert_to_app_project(db_project)


async def convert_to_app_project(db_project: db_models.DbProject):
"""Legacy function to convert db models --> Pydantic.
Expand All @@ -44,6 +45,7 @@ async def convert_to_app_project(db_project: db_models.DbProject):
app_project.tasks = db_project.tasks
return app_project


async def get_projects(
db: Session,
skip: int = 0,
Expand All @@ -60,6 +62,7 @@ async def get_projects(
project_count = db.query(db_models.DbProject).count()
return project_count, await convert_to_app_projects(db_projects)


async def convert_to_app_projects(
db_projects: List[db_models.DbProject],
) -> List[project_schemas.ProjectOut]:
Expand All @@ -79,6 +82,7 @@ async def convert_project(project):
else:
return []


async def create_project_with_project_info(
db: Session, project_metadata: project_schemas.ProjectIn
):
Expand Down
9 changes: 6 additions & 3 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from app.s3 import s3_client
from app.config import settings
from app.db import db_models
from app.config import settings


router = APIRouter(
Expand Down Expand Up @@ -130,6 +129,7 @@ async def generate_presigned_url(data: project_schemas.PresignedUrlRequest):
detail=f"Failed to generate pre-signed URL. {e}",
)


@router.get("/", tags=["Projects"], response_model=list[project_schemas.ProjectOut])
async def read_projects(
skip: int = 0,
Expand All @@ -140,10 +140,13 @@ async def read_projects(
total_count, projects = await project_crud.get_projects(db, skip, limit)
return projects

@router.get("/{project_id}", tags=["Projects"], response_model=project_schemas.ProjectOut)

@router.get(
"/{project_id}", tags=["Projects"], response_model=project_schemas.ProjectOut
)
async def read_project(
db: Session = Depends(database.get_db),
project: db_models.DbProject = Depends(project_crud.get_project_by_id),
):
"""Get a specific project by ID."""
return project
return project
3 changes: 1 addition & 2 deletions src/backend/app/users/oauth_routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import json
from loguru import logger as log
from fastapi import Depends, Request
from fastapi.responses import JSONResponse
Expand Down Expand Up @@ -40,7 +39,7 @@ async def callback(request: Request, google_auth=Depends(init_google_auth)):

callback_url = str(request.url)
access_token = google_auth.callback(callback_url).get("access_token")
return json.loads(access_token)
return access_token


@router.get("/my-info/")
Expand Down

0 comments on commit 5f3d5a1

Please sign in to comment.