Skip to content

Commit

Permalink
Merge pull request #6650 from hotosm/fastapi-refactor
Browse files Browse the repository at this point in the history
Remove db connection dependency in routers
  • Loading branch information
prabinoid authored Dec 3, 2024
2 parents 0dc540a + e313c15 commit 94b0f8f
Show file tree
Hide file tree
Showing 36 changed files with 6 additions and 76 deletions.
1 change: 0 additions & 1 deletion backend/api/annotations/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/campaigns/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
router = APIRouter(
prefix="/campaigns",
tags=["campaigns"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
8 changes: 4 additions & 4 deletions backend/api/comments/resources.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from backend.models.postgis.utils import timestamp
from databases import Database
from fastapi import APIRouter, Depends, Request, BackgroundTasks
from loguru import logger
from fastapi import APIRouter, BackgroundTasks, Depends, Request
from fastapi.responses import JSONResponse
from loguru import logger

from backend.db import get_db, get_session
from backend.models.dtos.mapping_dto import TaskCommentDTO
from backend.models.dtos.message_dto import ChatMessageDTO
from backend.models.dtos.user_dto import AuthUserDTO
from backend.models.postgis.utils import timestamp
from backend.services.mapping_service import MappingService, MappingServiceError
from backend.services.messaging.chat_service import ChatService
from backend.services.project_service import ProjectService
Expand All @@ -19,7 +20,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/countries/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
router = APIRouter(
prefix="/countries",
tags=["countries"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/interests/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
router = APIRouter(
prefix="/interests",
tags=["interests"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/issues/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
router = APIRouter(
prefix="/tasks",
tags=["issues"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/licenses/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
router = APIRouter(
prefix="/licenses",
tags=["licenses"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/licenses/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
router = APIRouter(
prefix="/licenses",
tags=["licenses"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/notifications/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
router = APIRouter(
prefix="/notifications",
tags=["notifications"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/notifications/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
router = APIRouter(
prefix="/notifications",
tags=["notifications"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/organisations/campaigns.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
router = APIRouter(
prefix="/organisations",
tags=["organisations"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/organisations/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
router = APIRouter(
prefix="/organisations",
tags=["organisations"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/partners/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
router = APIRouter(
prefix="/partners",
tags=["partners"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/partners/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def is_valid_group_id(group_id: Optional[str]) -> bool:
router = APIRouter(
prefix="/partners",
tags=["partners"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
5 changes: 2 additions & 3 deletions backend/api/projects/actions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from databases import Database
from fastapi import APIRouter, Body, Depends, Request, BackgroundTasks
from fastapi import APIRouter, BackgroundTasks, Body, Depends, Request
from fastapi.responses import JSONResponse
from loguru import logger
from shapely import GEOSException
from shapely.errors import TopologicalError

from backend.db import get_db, db_connection
from backend.db import db_connection, get_db
from backend.models.dtos.grid_dto import GridDTO
from backend.models.dtos.message_dto import MessageDTO
from backend.models.dtos.user_dto import AuthUserDTO
Expand All @@ -23,7 +23,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/projects/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/projects/campaigns.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/projects/contributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/projects/favorites.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/projects/partnerships.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/projects/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/projects/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/projects/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/system/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
router = APIRouter(
prefix="/system",
tags=["system"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/system/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
router = APIRouter(
prefix="/system",
tags=["system"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/system/banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
router = APIRouter(
prefix="/system",
tags=["system"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/system/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
router = APIRouter(
prefix="/system",
tags=["system"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
2 changes: 0 additions & 2 deletions backend/api/system/image_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
from fastapi.responses import JSONResponse

from backend.config import settings
from backend.db import get_db
from backend.models.dtos.user_dto import AuthUserDTO
from backend.services.users.authentication_service import login_required

router = APIRouter(
prefix="/system",
tags=["system"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/system/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
router = APIRouter(
prefix="/system",
tags=["system"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/tasks/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
router = APIRouter(
prefix="/projects",
tags=["tasks"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/tasks/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
router = APIRouter(
prefix="/projects",
tags=["projects"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/teams/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
router = APIRouter(
prefix="/teams",
tags=["teams"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/teams/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
router = APIRouter(
prefix="/teams",
tags=["teams"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/users/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
router = APIRouter(
prefix="/users",
tags=["users"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
1 change: 0 additions & 1 deletion backend/api/users/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
router = APIRouter(
prefix="/users",
tags=["users"],
dependencies=[Depends(get_db)],
responses={404: {"description": "Not found"}},
)

Expand Down
35 changes: 0 additions & 35 deletions backend/services/stats_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,10 @@ async def _update_tasks_stats(

# Increment counters for the new state
if new_state == TaskStatus.MAPPED:
print(type(project_stats["tasks_mapped"]))
print(project_stats["tasks_mapped"], "Task mapped before...")

project_stats["tasks_mapped"] += 1

print(project_stats["tasks_mapped"], "Task mapped after...")

elif new_state == TaskStatus.VALIDATED:
print(project_stats["tasks_validated"], "Task validated before...")

project_stats["tasks_validated"] += 1

print(project_stats["tasks_validated"], "Task validated after...")

elif new_state == TaskStatus.BADIMAGERY:
project_stats["tasks_bad_imagery"] += 1

Expand All @@ -123,38 +113,13 @@ async def _update_tasks_stats(

# Decrement counters for the old state
if last_state == TaskStatus.MAPPED:
print(
project_stats["tasks_mapped"], "Last state mapped decrement before..."
)

project_stats["tasks_mapped"] -= 1

print(project_stats["tasks_mapped"], "Last state mapped decrement after...")

elif last_state == TaskStatus.VALIDATED:
print(
project_stats["tasks_mapped"],
"Last state validation decrement before...",
)

project_stats["tasks_validated"] -= 1

print(
project_stats["tasks_mapped"],
"Last state validation decrement after...",
)

elif last_state == TaskStatus.BADIMAGERY:
print(
project_stats["tasks_mapped"], "Last state bad_img decrement before..."
)

project_stats["tasks_bad_imagery"] -= 1

print(
project_stats["tasks_mapped"], "Last state bad_img decrement after..."
)

# Undo user stats if action is "undo"
if action == "undo":
if last_state == TaskStatus.MAPPED:
Expand Down

0 comments on commit 94b0f8f

Please sign in to comment.