Skip to content

Commit

Permalink
ci: run pre-commit hooks on all, lint/format
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jan 12, 2024
1 parent bc5aa9e commit 529a107
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 50 deletions.
4 changes: 3 additions & 1 deletion src/backend/app/central/central_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
6 changes: 2 additions & 4 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 8 additions & 6 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
return project_users
3 changes: 1 addition & 2 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
35 changes: 20 additions & 15 deletions src/backend/app/submission/submission_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -839,31 +839,37 @@ 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:
with zip_ref.open("submissions.json") as file_in_zip:
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
Expand All @@ -876,4 +882,3 @@ async def get_submissions_by_date(db: Session, project_id: int, days: int, plann
]

return response

23 changes: 11 additions & 12 deletions src/backend/app/submission/submission_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand All @@ -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
return response
16 changes: 10 additions & 6 deletions src/backend/app/tasks/tasks_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -338,4 +342,4 @@ def process_history_entry(history_entry):
for history_entry in task_history:
process_history_entry(history_entry)

return tasks
return tasks
6 changes: 5 additions & 1 deletion src/backend/app/tasks/tasks_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/api/Project.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
7 changes: 5 additions & 2 deletions src/frontend/src/api/ProjectTaskStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 = {
Expand Down

0 comments on commit 529a107

Please sign in to comment.