From 26d513e24ac1344883731f1701ff5fed8b56c089 Mon Sep 17 00:00:00 2001 From: Pradip-p Date: Sat, 6 Jul 2024 15:33:46 +0545 Subject: [PATCH] feat: run pre-commit hooks on backend code --- src/backend/app/projects/project_crud.py | 5 +-- src/backend/app/projects/project_routes.py | 39 +++++++++++++-------- src/backend/app/projects/project_schemas.py | 3 +- src/backend/app/users/user_crud.py | 3 +- src/backend/app/users/user_routes.py | 1 - 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/backend/app/projects/project_crud.py b/src/backend/app/projects/project_crud.py index f2473cf1..21d4d8d0 100644 --- a/src/backend/app/projects/project_crud.py +++ b/src/backend/app/projects/project_crud.py @@ -65,7 +65,7 @@ async def create_project_with_project_info( async def get_project_by_id( - db: Database, author_id: uuid.UUID, project_id: Optional[int] = None + db: Database, author_id: uuid.UUID, project_id: Optional[int] = None ): """Get a single project & all associated tasks by ID.""" raw_sql = """ @@ -87,7 +87,7 @@ async def get_project_by_id( project_record.tasks = task_records project_record.task_count = len(task_records) return project_record - + async def get_projects( db: Database, @@ -105,6 +105,7 @@ async def get_projects( db_projects = await db.fetch_all(raw_sql, {"skip": skip, "limit": limit}) return db_projects + async def create_tasks_from_geojson( db: Database, project_id: uuid.UUID, diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index bd882910..26c5e915 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -26,7 +26,11 @@ @router.delete("/{project_id}", tags=["Projects"]) -def delete_project_by_id(project_id: uuid.UUID, db: Session = Depends(database.get_db), AuthUser = Depends(login_required)): +def delete_project_by_id( + project_id: uuid.UUID, + db: Session = Depends(database.get_db), + AuthUser=Depends(login_required), +): """ Delete a project by its ID, along with all associated tasks. @@ -70,11 +74,13 @@ def delete_project_by_id(project_id: uuid.UUID, db: Session = Depends(database.g async def create_project( project_info: project_schemas.ProjectIn, db: Database = Depends(database.encode_db), - user_data: AuthUser = Depends(login_required) + user_data: AuthUser = Depends(login_required), ): """Create a project in database.""" author_id = user_data.id - project = await project_crud.create_project_with_project_info(db,author_id, project_info) + project = await project_crud.create_project_with_project_info( + db, author_id, project_info + ) if not project: raise HTTPException( status_code=HTTPStatus.BAD_REQUEST, detail="Project creation failed" @@ -87,8 +93,7 @@ async def upload_project_task_boundaries( project_id: uuid.UUID, task_geojson: UploadFile = File(...), db: Database = Depends(database.encode_db), - AuthUser = Depends(login_required) - + AuthUser=Depends(login_required), ): """Set project task boundaries using split GeoJSON from frontend. @@ -100,12 +105,14 @@ async def upload_project_task_boundaries( Returns: dict: JSON containing success message, project ID, and number of tasks. - """ - #check the project in Database + """ + # check the project in Database raw_sql = f"""SELECT id FROM projects WHERE id = '{project_id}' LIMIT 1;""" - project = await db.fetch_one(query=raw_sql) + project = await db.fetch_one(query=raw_sql) if not project: - raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Project not found.") + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, detail="Project not found." + ) # read entire file content = await task_geojson.read() task_boundaries = json.loads(content) @@ -119,8 +126,9 @@ async def upload_project_task_boundaries( @router.post("/preview-split-by-square/", tags=["Projects"]) async def preview_split_by_square( - project_geojson: UploadFile = File(...), dimension: int = Form(100), - AuthUser = (login_required) + project_geojson: UploadFile = File(...), + dimension: int = Form(100), + AuthUser=(login_required), ): """Preview splitting by square.""" @@ -179,7 +187,7 @@ async def read_projects( skip: int = 0, limit: int = 100, db: Database = Depends(database.encode_db), - user_data: AuthUser = Depends(login_required) + user_data: AuthUser = Depends(login_required), ): "Return all projects" projects = await project_crud.get_projects(db, skip, limit) @@ -187,15 +195,16 @@ async def read_projects( @router.get( - "/{project_id}", tags=["Projects"], response_model=project_schemas.ProjectOut) + "/{project_id}", tags=["Projects"], response_model=project_schemas.ProjectOut +) async def read_project( project_id: uuid.UUID, db: Database = Depends(database.encode_db), - user_data: AuthUser = Depends(login_required) + user_data: AuthUser = Depends(login_required), ): """Get a specific project and all associated tasks by ID.""" author_id = user_data.id project = await project_crud.get_project_by_id(db, author_id, project_id) if project is None: raise HTTPException(status_code=404, detail="Project not found") - return project \ No newline at end of file + return project diff --git a/src/backend/app/projects/project_schemas.py b/src/backend/app/projects/project_schemas.py index c2020a98..bed79e05 100644 --- a/src/backend/app/projects/project_schemas.py +++ b/src/backend/app/projects/project_schemas.py @@ -73,7 +73,8 @@ def outline_geojson(self) -> Optional[Feature]: geom = wkb.loads(wkb_data) bbox = geom.bounds # Calculate bounding box return str_to_geojson(self.outline, {"id": self.id, "bbox": bbox}, str(self.id)) - + + class ProjectOut(BaseModel): """Base project model.""" diff --git a/src/backend/app/users/user_crud.py b/src/backend/app/users/user_crud.py index 697faa6a..28fbbf1f 100644 --- a/src/backend/app/users/user_crud.py +++ b/src/backend/app/users/user_crud.py @@ -4,7 +4,7 @@ from typing import Any from passlib.context import CryptContext from app.db import db_models -from app.users.user_schemas import UserCreate, AuthUser, ProfileUpdate +from app.users.user_schemas import AuthUser, ProfileUpdate from databases import Database from fastapi import HTTPException from app.models.enums import UserRole @@ -94,6 +94,7 @@ async def authenticate( return None return db_user + async def get_or_create_user( db: Database, user_data: AuthUser, diff --git a/src/backend/app/users/user_routes.py b/src/backend/app/users/user_routes.py index 9f2dd103..7badc219 100644 --- a/src/backend/app/users/user_routes.py +++ b/src/backend/app/users/user_routes.py @@ -6,7 +6,6 @@ from app.users.user_schemas import ( Token, UserPublic, - UserRegister, ProfileUpdate, AuthUser, )