From e313c15bacd95854644b57f8efe4e0f24e3220d0 Mon Sep 17 00:00:00 2001 From: prabinoid <38830224+prabinoid@users.noreply.github.com> Date: Tue, 3 Dec 2024 11:11:20 +0545 Subject: [PATCH] Remove db connection dependency in routers --- backend/api/annotations/resources.py | 1 - backend/api/campaigns/resources.py | 1 - backend/api/comments/resources.py | 8 +++--- backend/api/countries/resources.py | 1 - backend/api/interests/resources.py | 1 - backend/api/issues/resources.py | 1 - backend/api/licenses/actions.py | 1 - backend/api/licenses/resources.py | 1 - backend/api/notifications/actions.py | 1 - backend/api/notifications/resources.py | 1 - backend/api/organisations/campaigns.py | 1 - backend/api/organisations/resources.py | 1 - backend/api/partners/resources.py | 1 - backend/api/partners/statistics.py | 1 - backend/api/projects/actions.py | 5 ++-- backend/api/projects/activities.py | 1 - backend/api/projects/campaigns.py | 1 - backend/api/projects/contributions.py | 1 - backend/api/projects/favorites.py | 1 - backend/api/projects/partnerships.py | 1 - backend/api/projects/resources.py | 1 - backend/api/projects/statistics.py | 1 - backend/api/projects/teams.py | 1 - backend/api/system/applications.py | 1 - backend/api/system/authentication.py | 1 - backend/api/system/banner.py | 1 - backend/api/system/general.py | 1 - backend/api/system/image_upload.py | 2 -- backend/api/system/statistics.py | 1 - backend/api/tasks/actions.py | 1 - backend/api/tasks/resources.py | 1 - backend/api/teams/actions.py | 1 - backend/api/teams/resources.py | 1 - backend/api/users/statistics.py | 1 - backend/api/users/tasks.py | 1 - backend/services/stats_service.py | 35 -------------------------- 36 files changed, 6 insertions(+), 76 deletions(-) diff --git a/backend/api/annotations/resources.py b/backend/api/annotations/resources.py index ba23a6ef5c..1a909771b1 100644 --- a/backend/api/annotations/resources.py +++ b/backend/api/annotations/resources.py @@ -14,7 +14,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/campaigns/resources.py b/backend/api/campaigns/resources.py index d773314fd5..5efa21b394 100644 --- a/backend/api/campaigns/resources.py +++ b/backend/api/campaigns/resources.py @@ -15,7 +15,6 @@ router = APIRouter( prefix="/campaigns", tags=["campaigns"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/comments/resources.py b/backend/api/comments/resources.py index ffdfd2b70f..00eedf26c7 100644 --- a/backend/api/comments/resources.py +++ b/backend/api/comments/resources.py @@ -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 @@ -19,7 +20,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/countries/resources.py b/backend/api/countries/resources.py index e7ea079d6d..6fb50752c8 100644 --- a/backend/api/countries/resources.py +++ b/backend/api/countries/resources.py @@ -8,7 +8,6 @@ router = APIRouter( prefix="/countries", tags=["countries"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/interests/resources.py b/backend/api/interests/resources.py index 2745e62686..531b7b478f 100644 --- a/backend/api/interests/resources.py +++ b/backend/api/interests/resources.py @@ -13,7 +13,6 @@ router = APIRouter( prefix="/interests", tags=["interests"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/issues/resources.py b/backend/api/issues/resources.py index 9a8b43c63f..9c0563c941 100644 --- a/backend/api/issues/resources.py +++ b/backend/api/issues/resources.py @@ -12,7 +12,6 @@ router = APIRouter( prefix="/tasks", tags=["issues"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/licenses/actions.py b/backend/api/licenses/actions.py index 8aed376d6f..5d11278f4a 100644 --- a/backend/api/licenses/actions.py +++ b/backend/api/licenses/actions.py @@ -10,7 +10,6 @@ router = APIRouter( prefix="/licenses", tags=["licenses"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/licenses/resources.py b/backend/api/licenses/resources.py index 8677153fbd..78e2a85490 100644 --- a/backend/api/licenses/resources.py +++ b/backend/api/licenses/resources.py @@ -9,7 +9,6 @@ router = APIRouter( prefix="/licenses", tags=["licenses"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/notifications/actions.py b/backend/api/notifications/actions.py index 815f52edab..beb5d50522 100644 --- a/backend/api/notifications/actions.py +++ b/backend/api/notifications/actions.py @@ -10,7 +10,6 @@ router = APIRouter( prefix="/notifications", tags=["notifications"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/notifications/resources.py b/backend/api/notifications/resources.py index 3ad656f540..f3d94b8c28 100644 --- a/backend/api/notifications/resources.py +++ b/backend/api/notifications/resources.py @@ -16,7 +16,6 @@ router = APIRouter( prefix="/notifications", tags=["notifications"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/organisations/campaigns.py b/backend/api/organisations/campaigns.py index c2803c2acb..2f5eba6751 100644 --- a/backend/api/organisations/campaigns.py +++ b/backend/api/organisations/campaigns.py @@ -12,7 +12,6 @@ router = APIRouter( prefix="/organisations", tags=["organisations"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/organisations/resources.py b/backend/api/organisations/resources.py index 7ca800708c..ba45ff75db 100644 --- a/backend/api/organisations/resources.py +++ b/backend/api/organisations/resources.py @@ -23,7 +23,6 @@ router = APIRouter( prefix="/organisations", tags=["organisations"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/partners/resources.py b/backend/api/partners/resources.py index 1a0382cd82..2fd34e527d 100644 --- a/backend/api/partners/resources.py +++ b/backend/api/partners/resources.py @@ -14,7 +14,6 @@ router = APIRouter( prefix="/partners", tags=["partners"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/partners/statistics.py b/backend/api/partners/statistics.py index d5be58d008..40179ffc44 100644 --- a/backend/api/partners/statistics.py +++ b/backend/api/partners/statistics.py @@ -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"}}, ) diff --git a/backend/api/projects/actions.py b/backend/api/projects/actions.py index 8171d72496..246901e514 100644 --- a/backend/api/projects/actions.py +++ b/backend/api/projects/actions.py @@ -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 @@ -23,7 +23,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/projects/activities.py b/backend/api/projects/activities.py index ce2484b673..94e2061331 100644 --- a/backend/api/projects/activities.py +++ b/backend/api/projects/activities.py @@ -8,7 +8,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/projects/campaigns.py b/backend/api/projects/campaigns.py index 648145e8e2..c6a5ef889e 100644 --- a/backend/api/projects/campaigns.py +++ b/backend/api/projects/campaigns.py @@ -17,7 +17,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/projects/contributions.py b/backend/api/projects/contributions.py index 5c1ffcfefd..913924de9f 100644 --- a/backend/api/projects/contributions.py +++ b/backend/api/projects/contributions.py @@ -11,7 +11,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/projects/favorites.py b/backend/api/projects/favorites.py index 617fdc8a3c..8ac1871786 100644 --- a/backend/api/projects/favorites.py +++ b/backend/api/projects/favorites.py @@ -10,7 +10,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/projects/partnerships.py b/backend/api/projects/partnerships.py index 48dc21c9c2..1bca1c7ab0 100644 --- a/backend/api/projects/partnerships.py +++ b/backend/api/projects/partnerships.py @@ -16,7 +16,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/projects/resources.py b/backend/api/projects/resources.py index 0949795320..7a6ada3962 100644 --- a/backend/api/projects/resources.py +++ b/backend/api/projects/resources.py @@ -44,7 +44,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/projects/statistics.py b/backend/api/projects/statistics.py index 2caaf7abbc..5861a9bd79 100644 --- a/backend/api/projects/statistics.py +++ b/backend/api/projects/statistics.py @@ -9,7 +9,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/projects/teams.py b/backend/api/projects/teams.py index f51a5ddb51..38ad48385c 100644 --- a/backend/api/projects/teams.py +++ b/backend/api/projects/teams.py @@ -13,7 +13,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/system/applications.py b/backend/api/system/applications.py index 3a5ac39ec9..dab728776d 100644 --- a/backend/api/system/applications.py +++ b/backend/api/system/applications.py @@ -10,7 +10,6 @@ router = APIRouter( prefix="/system", tags=["system"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/system/authentication.py b/backend/api/system/authentication.py index b720ef6c5c..c335c6681c 100644 --- a/backend/api/system/authentication.py +++ b/backend/api/system/authentication.py @@ -17,7 +17,6 @@ router = APIRouter( prefix="/system", tags=["system"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/system/banner.py b/backend/api/system/banner.py index dbf736cc95..7f73285b52 100644 --- a/backend/api/system/banner.py +++ b/backend/api/system/banner.py @@ -14,7 +14,6 @@ router = APIRouter( prefix="/system", tags=["system"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/system/general.py b/backend/api/system/general.py index 21c215b186..ba2845e827 100644 --- a/backend/api/system/general.py +++ b/backend/api/system/general.py @@ -12,7 +12,6 @@ router = APIRouter( prefix="/system", tags=["system"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/system/image_upload.py b/backend/api/system/image_upload.py index 4415b8a929..43484649b3 100644 --- a/backend/api/system/image_upload.py +++ b/backend/api/system/image_upload.py @@ -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"}}, ) diff --git a/backend/api/system/statistics.py b/backend/api/system/statistics.py index 851046e87b..61bf08c912 100644 --- a/backend/api/system/statistics.py +++ b/backend/api/system/statistics.py @@ -8,7 +8,6 @@ router = APIRouter( prefix="/system", tags=["system"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/tasks/actions.py b/backend/api/tasks/actions.py index dbf04148d1..25977ce9ea 100644 --- a/backend/api/tasks/actions.py +++ b/backend/api/tasks/actions.py @@ -35,7 +35,6 @@ router = APIRouter( prefix="/projects", tags=["tasks"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/tasks/resources.py b/backend/api/tasks/resources.py index 0a02c2fb0c..67eec795fe 100644 --- a/backend/api/tasks/resources.py +++ b/backend/api/tasks/resources.py @@ -21,7 +21,6 @@ router = APIRouter( prefix="/projects", tags=["projects"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/teams/actions.py b/backend/api/teams/actions.py index d765df266b..91dfb8be1d 100644 --- a/backend/api/teams/actions.py +++ b/backend/api/teams/actions.py @@ -17,7 +17,6 @@ router = APIRouter( prefix="/teams", tags=["teams"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/teams/resources.py b/backend/api/teams/resources.py index bcc7f9f378..7f4071b803 100644 --- a/backend/api/teams/resources.py +++ b/backend/api/teams/resources.py @@ -17,7 +17,6 @@ router = APIRouter( prefix="/teams", tags=["teams"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/users/statistics.py b/backend/api/users/statistics.py index d8ce97b0c6..aa696137c3 100644 --- a/backend/api/users/statistics.py +++ b/backend/api/users/statistics.py @@ -17,7 +17,6 @@ router = APIRouter( prefix="/users", tags=["users"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/api/users/tasks.py b/backend/api/users/tasks.py index 94f2263ed2..edb2ef4e5e 100644 --- a/backend/api/users/tasks.py +++ b/backend/api/users/tasks.py @@ -12,7 +12,6 @@ router = APIRouter( prefix="/users", tags=["users"], - dependencies=[Depends(get_db)], responses={404: {"description": "Not found"}}, ) diff --git a/backend/services/stats_service.py b/backend/services/stats_service.py index 6c88c27e2d..f4bdce41b0 100644 --- a/backend/services/stats_service.py +++ b/backend/services/stats_service.py @@ -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 @@ -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: