From 5f3d5a1a124076b7b13984c24630908cbb557cb7 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Fri, 28 Jun 2024 12:56:07 +0545 Subject: [PATCH] update:google auth module --- src/backend/app/main.py | 21 ++++++++++++--------- src/backend/app/projects/project_crud.py | 6 +++++- src/backend/app/projects/project_routes.py | 9 ++++++--- src/backend/app/users/oauth_routes.py | 3 +-- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/backend/app/main.py b/src/backend/app/main.py index 73dae3e6..79ce87ce 100644 --- a/src/backend/app/main.py +++ b/src/backend/app/main.py @@ -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): @@ -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 @@ -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.""" @@ -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"}) - \ No newline at end of file diff --git a/src/backend/app/projects/project_crud.py b/src/backend/app/projects/project_crud.py index 136d5c8b..69e06d46 100644 --- a/src/backend/app/projects/project_crud.py +++ b/src/backend/app/projects/project_crud.py @@ -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 @@ -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: @@ -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. @@ -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, @@ -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]: @@ -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 ): diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index 25e170a7..880d878d 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -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( @@ -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, @@ -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 \ No newline at end of file + return project diff --git a/src/backend/app/users/oauth_routes.py b/src/backend/app/users/oauth_routes.py index d2341488..0952bf0b 100644 --- a/src/backend/app/users/oauth_routes.py +++ b/src/backend/app/users/oauth_routes.py @@ -1,5 +1,4 @@ import os -import json from loguru import logger as log from fastapi import Depends, Request from fastapi.responses import JSONResponse @@ -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/")