From 529a1077ddbfdcc7645a20d358efc7901132a0ef Mon Sep 17 00:00:00 2001 From: spwoodcock Date: Fri, 12 Jan 2024 17:05:54 +0000 Subject: [PATCH] ci: run pre-commit hooks on all, lint/format --- src/backend/app/central/central_routes.py | 4 ++- src/backend/app/projects/project_crud.py | 6 ++-- src/backend/app/projects/project_routes.py | 14 ++++---- src/backend/app/projects/project_schemas.py | 3 +- src/backend/app/submission/submission_crud.py | 35 +++++++++++-------- .../app/submission/submission_routes.py | 23 ++++++------ src/backend/app/tasks/tasks_crud.py | 16 +++++---- src/backend/app/tasks/tasks_routes.py | 6 +++- src/frontend/src/api/Project.js | 2 +- src/frontend/src/api/ProjectTaskStatus.js | 7 ++-- 10 files changed, 66 insertions(+), 50 deletions(-) diff --git a/src/backend/app/central/central_routes.py b/src/backend/app/central/central_routes.py index 6e42b0c806..60933cf45f 100644 --- a/src/backend/app/central/central_routes.py +++ b/src/backend/app/central/central_routes.py @@ -261,7 +261,9 @@ async def get_submission( for entry in range(1, len(data)): submissions.append(json.loads(data[entry])) else: - data = central_crud.download_submissions(first.odkid, xmlFormId, None, True, odk_credentials) + data = central_crud.download_submissions( + first.odkid, xmlFormId, None, True, odk_credentials + ) submissions.append(json.loads(data[0])) if len(data) >= 2: for entry in range(1, len(data)): diff --git a/src/backend/app/projects/project_crud.py b/src/backend/app/projects/project_crud.py index b1cdbbb971..3f2a864978 100644 --- a/src/backend/app/projects/project_crud.py +++ b/src/backend/app/projects/project_crud.py @@ -2336,15 +2336,13 @@ def is_valid_coordinate(coord): else input_geojson.get("geometry", {}).get("type", "") ) if geometry_type == "MultiPolygon": - first_coordinate = ( - coordinates[0][0] if coordinates and coordinates[0] else None - ) + first_coordinate = coordinates[0][0] if coordinates and coordinates[0] else None elif geometry_type == "Point": first_coordinate = coordinates if coordinates else None elif geometry_type == "LineString": first_coordinate = coordinates[0] if coordinates else None - + else: first_coordinate = coordinates[0][0] if coordinates else None diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index c5a3dede87..dcb0517407 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -1172,14 +1172,15 @@ async def get_template_file( ) -@router.get("/project_dashboard/{project_id}", response_model=project_schemas.ProjectDashboard) +@router.get( + "/project_dashboard/{project_id}", response_model=project_schemas.ProjectDashboard +) async def project_dashboard( - project_id: int, + project_id: int, background_tasks: BackgroundTasks, - db: Session = Depends(database.get_db) + db: Session = Depends(database.get_db), ): - """ - Get the project dashboard details. + """Get the project dashboard details. Args: project_id (int): The ID of the project. @@ -1198,6 +1199,7 @@ async def project_dashboard( ) return data + @router.get("/contributors/{project_id}") async def get_contributors(project_id: int, db: Session = Depends(database.get_db)): """Get contributors of a project. @@ -1209,4 +1211,4 @@ async def get_contributors(project_id: int, db: Session = Depends(database.get_d list[project_schemas.ProjectUser]: List of project users. """ project_users = await project_crud.get_project_users(db, project_id) - return project_users \ No newline at end of file + return project_users diff --git a/src/backend/app/projects/project_schemas.py b/src/backend/app/projects/project_schemas.py index 66655f273b..9f81b34d4f 100644 --- a/src/backend/app/projects/project_schemas.py +++ b/src/backend/app/projects/project_schemas.py @@ -29,7 +29,6 @@ from app.models.enums import ProjectPriority, ProjectStatus, TaskSplitType from app.tasks import tasks_schemas from app.users.user_schemas import User -from pydantic.functional_validators import field_validator class ODKCentral(BaseModel): @@ -147,7 +146,7 @@ class ProjectOut(ProjectBase): class ReadProject(ProjectBase): project_uuid: uuid.UUID = uuid.uuid4() - location_str: Optional[str] =None + location_str: Optional[str] = None class BackgroundTaskStatus(BaseModel): diff --git a/src/backend/app/submission/submission_crud.py b/src/backend/app/submission/submission_crud.py index d9b9bfabde..46ec181fb0 100644 --- a/src/backend/app/submission/submission_crud.py +++ b/src/backend/app/submission/submission_crud.py @@ -23,11 +23,10 @@ import threading import uuid from asyncio import gather -from datetime import datetime -from io import BytesIO -from pathlib import Path from collections import Counter from datetime import datetime, timedelta +from io import BytesIO +from pathlib import Path import sozipfile.sozipfile as zipfile from asgiref.sync import async_to_sync @@ -821,9 +820,10 @@ async def get_submission_count_of_a_project(db: Session, project_id: int): return len(files) -async def get_submissions_by_date(db: Session, project_id: int, days: int, planned_task: int): - """ - Get submissions by date. +async def get_submissions_by_date( + db: Session, project_id: int, days: int, planned_task: int +): + """Get submissions by date. Fetches the submissions for a given project within a specified number of days. @@ -839,14 +839,13 @@ async def get_submissions_by_date(db: Session, project_id: int, days: int, plann # Fetch submissions for project with ID 1 within the last 7 days submissions = await get_submissions_by_date(db, 1, 7) """ - project = await project_crud.get_project(db, project_id) s3_project_path = f"/{project.organisation_id}/{project_id}" s3_submission_path = f"/{s3_project_path}/submission.zip" try: file = get_obj_from_bucket(settings.S3_BUCKET_NAME, s3_submission_path) - except ValueError as e: + except ValueError: return [] with zipfile.ZipFile(file, "r") as zip_ref: @@ -854,16 +853,23 @@ async def get_submissions_by_date(db: Session, project_id: int, days: int, plann content = file_in_zip.read() content = json.loads(content) - end_dates = [datetime.fromisoformat(entry["end"].split('+')[0]) for entry in content if entry.get("end")] - - dates = [date.strftime('%m/%d') for date in end_dates if datetime.now() - date <= timedelta(days=days)] + end_dates = [ + datetime.fromisoformat(entry["end"].split("+")[0]) + for entry in content + if entry.get("end") + ] + + dates = [ + date.strftime("%m/%d") + for date in end_dates + if datetime.now() - date <= timedelta(days=days) + ] submission_counts = Counter(sorted(dates)) response = [ - {"date": key, "count": value} - for key, value in submission_counts.items() - ] + {"date": key, "count": value} for key, value in submission_counts.items() + ] if planned_task: count_dict = {} cummulative_count = 0 @@ -876,4 +882,3 @@ async def get_submissions_by_date(db: Session, project_id: int, days: int, plann ] return response - diff --git a/src/backend/app/submission/submission_routes.py b/src/backend/app/submission/submission_routes.py index d19a6c0437..2046b62eb3 100644 --- a/src/backend/app/submission/submission_routes.py +++ b/src/backend/app/submission/submission_routes.py @@ -26,13 +26,12 @@ from osm_fieldwork.osmfile import OsmFile from sqlalchemy.orm import Session +from app.central import central_crud from app.config import settings from app.db import database from app.projects import project_crud, project_schemas -from app.central import central_crud -from app.tasks import tasks_crud - from app.submission import submission_crud +from app.tasks import tasks_crud router = APIRouter( prefix="/submission", @@ -317,13 +316,13 @@ async def get_submission_page( planned_task: Optional[int] = None, db: Session = Depends(database.get_db), ): - """ - This api returns the submission page of a project. + """This api returns the submission page of a project. It takes one parameter: project_id. project_id: The ID of the project. This endpoint returns the submission page of this project. """ - - data = await submission_crud.get_submissions_by_date(db, project_id, days, planned_task) + data = await submission_crud.get_submissions_by_date( + db, project_id, days, planned_task + ) # Update submission cache in the background background_task_id = await project_crud.insert_background_task_into_database( @@ -338,9 +337,10 @@ async def get_submission_page( @router.get("/submission_form_fields/{project_id}") -async def get_submission_form_fields(project_id: int, db: Session = Depends(database.get_db)): - """ - Retrieves the submission form for a specific project. +async def get_submission_form_fields( + project_id: int, db: Session = Depends(database.get_db) +): + """Retrieves the submission form for a specific project. Args: project_id (int): The ID of the project. @@ -349,9 +349,8 @@ async def get_submission_form_fields(project_id: int, db: Session = Depends(data Returns: Any: The response from the submission form API. """ - project = await project_crud.get_project(db, project_id) task_list = await tasks_crud.get_task_id_list(db, project_id) odk_form = central_crud.get_odk_form(project) response = odk_form.form_fields(project.odkid, str(task_list[0])) - return response \ No newline at end of file + return response diff --git a/src/backend/app/tasks/tasks_crud.py b/src/backend/app/tasks/tasks_crud.py index e8d9d1cd13..2d12c231ca 100644 --- a/src/backend/app/tasks/tasks_crud.py +++ b/src/backend/app/tasks/tasks_crud.py @@ -18,7 +18,7 @@ import base64 from typing import List -from fastapi import HTTPException, Depends +from fastapi import Depends, HTTPException from geoalchemy2.shape import from_shape from geojson import dump from loguru import logger as log @@ -27,15 +27,15 @@ from sqlalchemy.orm import Session from sqlalchemy.sql import text -from app.tasks import tasks_schemas from app.central import central_crud -from app.db import db_models, database +from app.db import database, db_models from app.models.enums import ( TaskStatus, get_action_for_status_change, verify_valid_status_update, ) from app.projects import project_crud +from app.tasks import tasks_schemas from app.users import user_crud @@ -321,13 +321,17 @@ async def edit_task_boundary(db: Session, task_id: int, boundary: str): return True -async def update_task_history(tasks: List[tasks_schemas.TaskBase], db: Session = Depends(database.get_db)): +async def update_task_history( + tasks: List[tasks_schemas.TaskBase], db: Session = Depends(database.get_db) +): def process_history_entry(history_entry): status = history_entry.action_text.split() history_entry.status = status[5] if history_entry.user_id: - user = db.query(db_models.DbUser).filter_by(id=history_entry.user_id).first() + user = ( + db.query(db_models.DbUser).filter_by(id=history_entry.user_id).first() + ) if user: history_entry.username = user.username history_entry.profile_img = user.profile_img @@ -338,4 +342,4 @@ def process_history_entry(history_entry): for history_entry in task_history: process_history_entry(history_entry) - return tasks \ No newline at end of file + return tasks diff --git a/src/backend/app/tasks/tasks_routes.py b/src/backend/app/tasks/tasks_routes.py index 44f79cdaae..6df7163a47 100644 --- a/src/backend/app/tasks/tasks_routes.py +++ b/src/backend/app/tasks/tasks_routes.py @@ -28,6 +28,7 @@ from app.models.enums import TaskStatus from app.projects import project_crud, project_schemas from app.users import user_schemas + from . import tasks_crud, tasks_schemas router = APIRouter( @@ -37,6 +38,7 @@ responses={404: {"description": "Not found"}}, ) + @router.get("/task-list", response_model=List[tasks_schemas.ReadTask]) async def read_task_list( project_id: int, @@ -109,7 +111,9 @@ async def read_tasks(task_id: int, db: Session = Depends(database.get_db)): return task -@router.post("/{task_id}/new_status/{new_status}", response_model=tasks_schemas.ReadTask) +@router.post( + "/{task_id}/new_status/{new_status}", response_model=tasks_schemas.ReadTask +) async def update_task_status( user: user_schemas.User, task_id: int, diff --git a/src/frontend/src/api/Project.js b/src/frontend/src/api/Project.js index 225ec0fc74..7f85f0e333 100755 --- a/src/frontend/src/api/Project.js +++ b/src/frontend/src/api/Project.js @@ -1,7 +1,7 @@ import { ProjectActions } from '../store/slices/ProjectSlice'; import CoreModules from '../shared/CoreModules'; import environment from '../environment'; -import {task_priority_str} from '../types/enums' +import { task_priority_str } from '../types/enums'; export const ProjectById = (existingProjectList, projectId) => { return async (dispatch) => { diff --git a/src/frontend/src/api/ProjectTaskStatus.js b/src/frontend/src/api/ProjectTaskStatus.js index f0e36e27e1..6e88a54b06 100755 --- a/src/frontend/src/api/ProjectTaskStatus.js +++ b/src/frontend/src/api/ProjectTaskStatus.js @@ -3,7 +3,7 @@ import { easeIn, easeOut } from 'ol/easing'; import { HomeActions } from '../store/slices/HomeSlice'; import CoreModules from '../shared/CoreModules'; import { CommonActions } from '../store/slices/CommonSlice'; -import {task_priority_str} from '../types/enums' +import { task_priority_str } from '../types/enums'; const UpdateTaskStatus = (url, style, existingData, currentProjectId, feature, map, view, taskId, body) => { return async (dispatch) => { @@ -16,7 +16,10 @@ const UpdateTaskStatus = (url, style, existingData, currentProjectId, feature, m const findIndexForUpdation = existingData[index].taskBoundries.findIndex((obj) => obj.id == response.data.id); let project_tasks = [...existingData[index].taskBoundries]; - project_tasks[findIndexForUpdation] = { ...response.data, task_status: task_priority_str[response.data.task_status] }; + project_tasks[findIndexForUpdation] = { + ...response.data, + task_status: task_priority_str[response.data.task_status], + }; let updatedProject = [...existingData]; const finalProjectOBJ = {