From ec7a03b584db59f529d31467ceac8d6f10e2e419 Mon Sep 17 00:00:00 2001 From: prabinoid Date: Tue, 22 Oct 2024 13:31:48 +0545 Subject: [PATCH] Bulk action being applied for all tasks fixed, due date exception handled and queries bbox geojson serialized --- backend/api/projects/resources.py | 3 +- backend/models/postgis/project.py | 6 ++- backend/models/postgis/task.py | 56 +++++++++++++++++++-------- backend/services/validator_service.py | 1 - 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/backend/api/projects/resources.py b/backend/api/projects/resources.py index 17d2c8e979..e16f14a48a 100644 --- a/backend/api/projects/resources.py +++ b/backend/api/projects/resources.py @@ -143,6 +143,7 @@ async def get_project( finally: # this will try to unlock tasks that have been locked too long try: + # TODO await ProjectService.auto_unlock_tasks(project_id, db) except Exception as e: logger.critical(str(e)) @@ -827,7 +828,7 @@ async def get( ) try: geojson = await ProjectSearchService.get_projects_geojson(search_dto, db) - return geojson, 200 + return JSONResponse(content=geojson, status_code=200) except BBoxTooBigError as e: return JSONResponse( content={"Error": str(e).split("-")[1], "SubCode": str(e).split("-")[0]}, diff --git a/backend/models/postgis/project.py b/backend/models/postgis/project.py index 7cdb71414d..490257b2b4 100644 --- a/backend/models/postgis/project.py +++ b/backend/models/postgis/project.py @@ -487,7 +487,9 @@ async def update(self, project_dto: ProjectDTO, db: Database): self.private = project_dto.private self.difficulty = ProjectDifficulty[project_dto.difficulty.upper()].value self.changeset_comment = project_dto.changeset_comment - self.due_date = project_dto.due_date.replace(tzinfo=None) + self.due_date = ( + project_dto.due_date.replace(tzinfo=None) if project_dto.due_date else None + ) self.imagery = project_dto.imagery self.josm_preset = project_dto.josm_preset self.id_presets = project_dto.id_presets @@ -1359,7 +1361,7 @@ async def get_project_summary( ProjectTeamDTO( team_id=team["team_id"], team_name=team["team_name"], - role=TeamRoles(team["role"]).value, + role=TeamRoles(team["role"]), ) for team in teams ] diff --git a/backend/models/postgis/task.py b/backend/models/postgis/task.py index 7f8150549f..1512d4e4cd 100644 --- a/backend/models/postgis/task.py +++ b/backend/models/postgis/task.py @@ -917,7 +917,7 @@ async def auto_unlock_tasks(project_id: int, session): for old_task_id in old_tasks: task = await session.get(Task, (old_task_id, project_id)) if task: - await task.auto_unlock_expired_tasks( + await Task.auto_unlock_expired_tasks( expiry_date, lock_duration, session ) @@ -1104,13 +1104,14 @@ async def reset_task(task_id: int, project_id: int, user_id: int, db: Database): mapped_by = NULL, validated_by = NULL, locked_by = NULL - WHERE id = :task_id + WHERE id = :task_id AND project_id = :project_id """ await db.execute( query=update_task_query, values={ "task_id": task_id, "ready_status": TaskStatus.READY.value, + "project_id": project_id, }, ) @@ -1228,22 +1229,29 @@ async def unlock_task( update_query = """ UPDATE tasks SET validated_by = NULL - WHERE id = :task_id + WHERE id = :task_id AND project_id = :project_id """ - await db.execute(query=update_query, values={"task_id": task_id}) + await db.execute( + query=update_query, + values={"task_id": task_id, "project_id": project_id}, + ) elif new_state == TaskStatus.READY: update_query = """ UPDATE tasks SET mapped_by = NULL - WHERE id = :task_id + WHERE id = :task_id AND project_id = :project_id """ - await db.execute(query=update_query, values={"task_id": task_id}) + await db.execute( + query=update_query, + values={"task_id": task_id, "project_id": project_id}, + ) else: current_status_query = """ - SELECT task_status FROM tasks WHERE id = :task_id + SELECT task_status FROM tasks WHERE id = :task_id AND project_id = :project_id """ current_status_result = await db.fetch_one( - query=current_status_query, values={"task_id": task_id} + query=current_status_query, + values={"task_id": task_id, "project_id": project_id}, ) current_status = TaskStatus(current_status_result["task_status"]) # Handle specific state changes @@ -1254,10 +1262,15 @@ async def unlock_task( update_query = """ UPDATE tasks SET validated_by = :user_id - WHERE id = :task_id + WHERE id = :task_id AND project_id = :project_id """ await db.execute( - query=update_query, values={"user_id": user_id, "task_id": task_id} + query=update_query, + values={ + "user_id": user_id, + "task_id": task_id, + "project_id": project_id, + }, ) elif new_state == TaskStatus.INVALIDATED: @@ -1267,9 +1280,12 @@ async def unlock_task( update_query = """ UPDATE tasks SET mapped_by = NULL, validated_by = NULL - WHERE id = :task_id + WHERE id = :task_id AND project_id = :project_id """ - await db.execute(query=update_query, values={"task_id": task_id}) + await db.execute( + query=update_query, + values={"task_id": task_id, "project_id": project_id}, + ) # Set `mapped_by` for MAPPED or BADIMAGERY states when not locked for validation elif new_state in [TaskStatus.MAPPED, TaskStatus.BADIMAGERY]: @@ -1277,11 +1293,15 @@ async def unlock_task( update_query = """ UPDATE tasks SET mapped_by = :user_id - WHERE id = :task_id + WHERE id = :task_id AND project_id = :project_id """ await db.execute( query=update_query, - values={"user_id": user_id, "task_id": task_id}, + values={ + "user_id": user_id, + "task_id": task_id, + "project_id": project_id, + }, ) # Update task locked duration in the history when `undo` is False @@ -1293,11 +1313,15 @@ async def unlock_task( final_update_query = """ UPDATE tasks SET task_status = :new_status, locked_by = NULL - WHERE id = :task_id + WHERE id = :task_id AND project_id = :project_id """ await db.execute( query=final_update_query, - values={"new_status": new_state.value, "task_id": task_id}, + values={ + "new_status": new_state.value, + "task_id": task_id, + "project_id": project_id, + }, ) @staticmethod diff --git a/backend/services/validator_service.py b/backend/services/validator_service.py index ee72f86368..6d337d5809 100644 --- a/backend/services/validator_service.py +++ b/backend/services/validator_service.py @@ -414,7 +414,6 @@ async def invalidate_all_tasks(project_id: int, user_id: int, db: Database): "validated_status": TaskStatus.VALIDATED.value, }, ) - for task in validated_tasks: await Task.lock_task_for_validating(task["id"], project_id, user_id, db) await Task.unlock_task(