Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: update project dashboard to use deps functions #1127

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/backend/app/organisations/organisation_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
from sqlalchemy.orm import Session

from app.db.database import get_db
from app.db.db_models import DbOrganisation
from app.db.db_models import DbOrganisation, DbProject
from app.models.enums import HTTPStatus
from app.projects import project_deps


async def get_organisation_by_name(
Expand Down Expand Up @@ -120,3 +121,11 @@ async def org_exists(
Requires Depends from a route.
"""
return await check_org_exists(db, org_id)


async def org_from_project(
project: DbProject = Depends(project_deps.get_project_by_id),
db: Session = Depends(get_db),
) -> DbOrganisation:
"""Get an organisation from a project id."""
return await check_org_exists(db, project.organisation_id)
18 changes: 7 additions & 11 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
from app.db import db_models
from app.db.database import get_db
from app.db.postgis_utils import geojson_to_flatgeobuf, geometry_to_geojson, timestamp
from app.organisations import organisation_crud
from app.projects import project_schemas
from app.s3 import add_obj_to_bucket, get_obj_from_bucket
from app.tasks import tasks_crud
Expand Down Expand Up @@ -2361,14 +2360,11 @@ async def get_pagination(page: int, count: int, results_per_page: int, total: in
return pagination


async def get_dashboard_detail(project_id: int, db: Session):
async def get_dashboard_detail(
project: db_models.DbProject, db_organisation: db_models.DbOrganisation, db: Session
):
"""Get project details for project dashboard."""
project = await get_project(db, project_id)
db_organisation = await organisation_crud.get_organisation_by_id(
db, project.organisation_id
)

s3_project_path = f"/{project.organisation_id}/{project_id}"
s3_project_path = f"/{project.organisation_id}/{project.id}"
s3_submission_path = f"/{s3_project_path}/submission.zip"
s3_submission_meta_path = f"/{s3_project_path}/submissions.meta.json"

Expand All @@ -2392,15 +2388,15 @@ async def get_dashboard_detail(project_id: int, db: Session):
contributors = (
db.query(db_models.DbTaskHistory.user_id)
.filter(
db_models.DbTaskHistory.project_id == project_id,
db_models.DbTaskHistory.project_id == project.id,
db_models.DbTaskHistory.user_id.isnot(None),
)
.distinct()
.count()
)

project.total_tasks = await tasks_crud.get_task_count_in_project(db, project_id)
project.organisation, project.organisation_logo = (
project.total_tasks = await tasks_crud.get_task_count_in_project(db, project.id)
project.organisation_name, project.organisation_logo = (
db_organisation.name,
db_organisation.logo,
)
Expand Down
18 changes: 12 additions & 6 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from app.central import central_crud
from app.db import database, db_models
from app.models.enums import TILES_FORMATS, TILES_SOURCE, HTTPStatus
from app.organisations import organisation_deps
from app.projects import project_crud, project_deps, project_schemas
from app.projects.project_crud import check_crs
from app.static import data_path
Expand Down Expand Up @@ -1179,28 +1180,33 @@ async def get_template_file(
"/project_dashboard/{project_id}", response_model=project_schemas.ProjectDashboard
)
async def project_dashboard(
project_id: int,
background_tasks: BackgroundTasks,
db_project: db_models.DbProject = Depends(project_deps.get_project_by_id),
db_organisation: db_models.DbOrganisation = Depends(
organisation_deps.org_from_project
),
db: Session = Depends(database.get_db),
):
"""Get the project dashboard details.

Args:
project_id (int): The ID of the project.
background_tasks (BackgroundTasks): FastAPI bg tasks, provided automatically.
db_project (db_models.DbProject): An instance of the project.
db_organisation (db_models.DbOrganisation): An instance of the organisation.
db (Session): The database session.

Returns:
ProjectDashboard: The project dashboard details.
"""
data = await project_crud.get_dashboard_detail(project_id, db)
data = await project_crud.get_dashboard_detail(db_project, db_organisation, db)

background_task_id = await project_crud.insert_background_task_into_database(
db, "sync_submission", project_id
db, "sync_submission", db_project.id
)

background_tasks.add_task(
submission_crud.update_submission_in_s3, db, project_id, background_task_id
submission_crud.update_submission_in_s3, db, db_project.id, background_task_id
)

return data


Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class ProjectDashboard(BaseModel):
"""Project details dashboard."""

project_name_prefix: str
organisation: str
organisation_name: str
spwoodcock marked this conversation as resolved.
Show resolved Hide resolved
total_tasks: int
created: datetime
organisation_logo: Optional[str] = None
Expand Down