Skip to content

Commit

Permalink
feat: run pre-commit hooks on backend code
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Jul 6, 2024
1 parent 262934e commit 26d513e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand All @@ -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,
Expand All @@ -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,
Expand Down
39 changes: 24 additions & 15 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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)
Expand All @@ -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."""

Expand Down Expand Up @@ -179,23 +187,24 @@ 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)
return 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
return project
3 changes: 2 additions & 1 deletion src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
3 changes: 2 additions & 1 deletion src/backend/app/users/user_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -94,6 +94,7 @@ async def authenticate(
return None
return db_user


async def get_or_create_user(
db: Database,
user_data: AuthUser,
Expand Down
1 change: 0 additions & 1 deletion src/backend/app/users/user_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from app.users.user_schemas import (
Token,
UserPublic,
UserRegister,
ProfileUpdate,
AuthUser,
)
Expand Down

0 comments on commit 26d513e

Please sign in to comment.