Skip to content

Commit

Permalink
feat: add community_type for organisations, add unapproved org list e…
Browse files Browse the repository at this point in the history
…ndpoint (#1197)

* create organisation permission updated to login_required

* api to list unapproved organisations

* Feat: Added email and community_type field in organisation

* fix: changed org_id to mandatory to create project

* fix: added organisation_id in payload of create project

* fix: await check crs, added email and community type in test organisation

* refactor: proper enum field community type in test organisation

* build: update community_type migration number & logic

* refactor: remove email field from organisation

* refactor: remove organisation_id from ProjectUpload model

* refactor: remove email from dborg model for conftest

* refactor: remove organisation_id from create_project POST json

* test: fix remove organisation_id from ProjectUpload

* fix: add optional organisation_id to ProjectUpload

* feat: add project to org_user_dict if present

* fix: extract project from org_user_dict on deletion

* test: fix tests to include organisation_id extracted from fixture

---------

Co-authored-by: Niraj Adhikari <nrjadkry@gmail.com>
Co-authored-by: sujanadh <sujanadh07@gmail.com>
Co-authored-by: spwoodcock <sam.woodcock@protonmail.com>
  • Loading branch information
4 people authored Feb 13, 2024
1 parent 5ef42b0 commit 941fdde
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 16 deletions.
7 changes: 6 additions & 1 deletion src/backend/app/auth/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,17 @@ async def org_admin(
detail="org_id must be provided to check organisation admin role",
)

return await check_org_admin(
org_user_dict = await check_org_admin(
db,
user_data,
org_id=org_id,
)

if project:
org_user_dict["project"] = project

return org_user_dict


async def project_admin(
project: DbProject = Depends(get_project_by_id),
Expand Down
8 changes: 8 additions & 0 deletions src/backend/app/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
from app.db.postgis_utils import timestamp
from app.models.enums import (
BackgroundTaskStatus,
CommunityType,
MappingLevel,
MappingPermission,
OrganisationType,
Expand Down Expand Up @@ -168,6 +169,13 @@ class DbOrganisation(Base):
odk_central_user = cast(str, Column(String))
odk_central_password = cast(str, Column(String))

community_type = cast(
CommunityType,
Column(
Enum(CommunityType), default=CommunityType.OSM_COMMUNITY, nullable=False
),
)

managers = relationship(
DbUser,
secondary=organisation_managers,
Expand Down
10 changes: 10 additions & 0 deletions src/backend/app/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,13 @@ class ProjectVisibility(IntEnum, Enum):
PUBLIC = 0
PRIVATE = 1
INVITE_ONLY = 2


class CommunityType(IntEnum, Enum):
"""Enum describing community type."""

OSM_COMMUNITY = 0
COMPANY = 1
NON_PROFIT = 2
UNIVERSITY = 3
OTHER = 4
7 changes: 7 additions & 0 deletions src/backend/app/organisations/organisation_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ async def get_organisations(
return db.query(db_models.DbOrganisation).filter_by(approved=True).all()


async def get_unapproved_organisations(
db: Session,
) -> list[db_models.DbOrganisation]:
"""Get unapproved orgs."""
return db.query(db_models.DbOrganisation).filter_by(approved=False)


async def upload_logo_to_s3(
db_org: db_models.DbOrganisation, logo_file: UploadFile(None)
) -> str:
Expand Down
11 changes: 10 additions & 1 deletion src/backend/app/organisations/organisation_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ async def get_organisations(
return await organisation_crud.get_organisations(db, current_user)


@router.get("/unapproved/", response_model=list[organisation_schemas.OrganisationOut])
async def list_unapproved_organisations(
db: Session = Depends(database.get_db),
current_user: AuthUser = Depends(super_admin),
) -> list[DbOrganisation]:
"""Get a list of all organisations."""
return await organisation_crud.get_unapproved_organisations(db)


@router.get("/{org_id}", response_model=organisation_schemas.OrganisationOut)
async def get_organisation_detail(
organisation: DbOrganisation = Depends(org_exists),
Expand All @@ -64,7 +73,7 @@ async def create_organisation(
org: organisation_schemas.OrganisationIn = Depends(),
logo: UploadFile = File(None),
db: Session = Depends(database.get_db),
current_user: DbUser = Depends(super_admin),
current_user: DbUser = Depends(login_required),
) -> organisation_schemas.OrganisationOut:
"""Create an organisation with the given details."""
return await organisation_crud.create_organisation(db, org, logo)
Expand Down
3 changes: 2 additions & 1 deletion src/backend/app/organisations/organisation_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from pydantic import BaseModel, computed_field

from app.config import HttpUrlStr
from app.models.enums import OrganisationType
from app.models.enums import CommunityType, OrganisationType
from app.projects.project_schemas import ODKCentralIn

# class OrganisationBase(BaseModel):
Expand All @@ -36,6 +36,7 @@ class OrganisationIn(ODKCentralIn):
name: str
description: Optional[str] = None
url: Optional[HttpUrlStr] = None
community_type: Optional[CommunityType] = None

@computed_field
@property
Expand Down
10 changes: 3 additions & 7 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ async def read_project(project_id: int, db: Session = Depends(database.get_db)):

@router.delete("/{project_id}")
async def delete_project(
project: db_models.DbProject = Depends(project_deps.get_project_by_id),
current_user: AuthUser = Depends(org_admin),
db: Session = Depends(database.get_db),
org_user_dict: db_models.DbUser = Depends(org_admin),
):
"""Delete a project from both ODK Central and the local database."""
project = org_user_dict.get("project")

log.info(
f"User {org_user_dict.get('user').username} attempting "
f"deletion of project {project.id}"
Expand Down Expand Up @@ -614,11 +614,7 @@ async def generate_files(
"""
log.debug(f"Generating media files tasks for project: {project_id}")

project = await project_crud.get_project(db, project_id)
if not project:
raise HTTPException(
status_code=428, detail=f"Project with id {project_id} does not exist"
)
project = org_user_dict.get("project")

form_category = project.xform_title
custom_xls_form = None
Expand Down
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 @@ -134,8 +134,8 @@ class ProjectIn(BaseModel):
"""Upload new project."""

project_info: ProjectInfo
organisation_id: Optional[int] = None
xform_title: str
organisation_id: Optional[int] = None
hashtags: Optional[List[str]] = None
task_split_type: Optional[TaskSplitType] = None
task_split_dimension: Optional[int] = None
Expand Down Expand Up @@ -202,7 +202,6 @@ class ProjectUpdate(ProjectIn):
name: Optional[str] = None
short_description: Optional[str] = None
description: Optional[str] = None
organisation_id: Optional[int] = None


class GeojsonFeature(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion src/backend/migrations/001-project-split-type-fields.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ BEGIN
);
END IF;
END $$;
ALTER TYPE public.mappinglevel OWNER TO fmtm;
ALTER TYPE public.tasksplittype OWNER TO fmtm;

-- Update task_split_type
DO $$
Expand Down
29 changes: 29 additions & 0 deletions src/backend/migrations/009-add-community-type.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- ## Migration to:
-- * Add public.communitytype enum.
-- * Add public.organisation.community_type field.

-- Start a transaction
BEGIN;

-- Create communitytype enum if it doesn't exist
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'communitytype') THEN
CREATE TYPE public.communitytype AS ENUM (
'OSM_COMMUNITY',
'COMPANY',
'NON_PROFIT',
'UNIVERSITY',
'OTHER'
);
END IF;
END $$;
ALTER TYPE public.communitytype OWNER TO fmtm;

-- Add the community_type column to organisations table
ALTER TABLE IF EXISTS public.organisations
ADD COLUMN IF NOT EXISTS community_type public.communitytype
DEFAULT 'OSM_COMMUNITY';

-- Commit the transaction
COMMIT;
11 changes: 11 additions & 0 deletions src/backend/migrations/revert/009-add-community-type.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
BEGIN;

-- Remove the community_type column from organisations table
ALTER TABLE public.organisations
DROP COLUMN IF EXISTS community_type;

-- Drop the communitytype enum
DROP TYPE IF EXISTS public.communitytype;

-- Commit the transaction
COMMIT;
5 changes: 3 additions & 2 deletions src/backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from app.db.database import Base, get_db
from app.db.db_models import DbOrganisation
from app.main import get_application
from app.models.enums import UserRole
from app.models.enums import CommunityType, UserRole
from app.projects import project_crud
from app.projects.project_schemas import ODKCentralDecrypted, ProjectInfo, ProjectUpload

Expand Down Expand Up @@ -113,6 +113,7 @@ def organisation(db):
url="https://test.org",
logo="none",
approved=True,
community_type=CommunityType.OSM_COMMUNITY,
)
db.add(db_org)
db.commit()
Expand All @@ -133,7 +134,6 @@ async def project(db, admin_user, organisation):
odk_central_user=os.getenv("ODK_CENTRAL_USER"),
odk_central_password=os.getenv("ODK_CENTRAL_PASSWD"),
hashtags=["hot-fmtm"],
organisation_id=organisation.id,
outline_geojson={
"type": "Feature",
"properties": {},
Expand All @@ -150,6 +150,7 @@ async def project(db, admin_user, organisation):
"type": "Polygon",
},
},
organisation_id=organisation.id,
)

odk_creds_decrypted = ODKCentralDecrypted(
Expand Down
3 changes: 2 additions & 1 deletion src/backend/tests/test_projects_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ async def test_create_project(client, admin_user, organisation):
assert "id" in response_data


async def test_delete_project(client, project):
async def test_delete_project(client, admin_user, project):
"""Test deleting a FMTM project, plus ODK Central project."""
log.warning(project)
response = client.delete(f"/projects/{project.id}")
assert response.status_code == 204

Expand Down

0 comments on commit 941fdde

Please sign in to comment.