Skip to content

Commit

Permalink
fix(backend): ProjectUserDict usage and standardise geojson parsing/u…
Browse files Browse the repository at this point in the history
…sage throughout (#1659)

* fix(backend): improve get_uid function to be flexible (AuthUser, DbUser, dict)

* refactor: standardise geojson usage to featcol via jazzband/geojson

* refactor: update schemas to better divide geojson_pydantic and geojson

* refactor: update function names form postgis_utils

* refactor(backend): usage of ProjectUserDict project obj in project_routes

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* refactor: replace authuser by projectuserdict in task_routes and submission_routes

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* refactor: reduce extra projects call function in submission_crud

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: added back the code from develop to handle multipolygon geoms

* refactor: fix pre-commit errors for ruff

* refactor: rename current_user --> project_user (dict)

* refactor: minor changes for auth types

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: sujan <[email protected]>
  • Loading branch information
3 people authored Jul 17, 2024
1 parent 35e1f60 commit 018605b
Show file tree
Hide file tree
Showing 16 changed files with 399 additions and 552 deletions.
1 change: 1 addition & 0 deletions src/backend/app/auth/auth_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class OrgUserDict(TypedDict):

user: DbUser
org: DbOrganisation
project: Optional[DbProject]


class ProjectUserDict(TypedDict):
Expand Down
23 changes: 13 additions & 10 deletions src/backend/app/auth/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
and always return an AuthUser object in a standard format.
"""

from typing import Optional, Union
from typing import Optional

from fastapi import Depends, HTTPException
from loguru import logger as log
Expand All @@ -38,20 +38,23 @@
from app.projects.project_deps import get_project_by_id


async def get_uid(user_data: AuthUser) -> int:
async def get_uid(user_data: AuthUser | DbUser) -> int:
"""Extract user id from returned OSM user."""
if user_id := user_data.id:
try:
user_id = user_data.id
return user_id
else:

except Exception as e:
log.error(e)
log.error(f"Failed to get user id from auth object: {user_data}")
raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED,
detail="Auth failed. No user id present",
)
) from e


async def check_access(
user: Union[AuthUser, int],
user: AuthUser,
db: Session,
org_id: Optional[int] = None,
project_id: Optional[int] = None,
Expand All @@ -77,7 +80,7 @@ async def check_access(
Returns:
Optional[DbUser]: The user details if access is granted, otherwise None.
"""
user_id = user if isinstance(user, int) else await get_uid(user)
user_id = await get_uid(user)

sql = text(
"""
Expand Down Expand Up @@ -162,7 +165,7 @@ async def super_admin(

async def check_org_admin(
db: Session,
user: Union[AuthUser, int],
user: AuthUser,
org_id: int,
) -> OrgUserDict:
"""Database check to determine if org admin role.
Expand All @@ -180,7 +183,7 @@ async def check_org_admin(
)

if db_user:
return {"user": db_user, "org": db_org}
return {"user": db_user, "org": db_org, "project": None}

raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
Expand All @@ -193,7 +196,7 @@ async def org_admin(
org_id: Optional[int] = None,
db: Session = Depends(get_db),
user_data: AuthUser = Depends(login_required),
) -> dict:
) -> OrgUserDict:
"""Organisation admin with full permission for projects in an organisation.
Returns:
Expand Down
4 changes: 2 additions & 2 deletions src/backend/app/central/central_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from app.db.postgis_utils import (
geojson_to_javarosa_geom,
javarosa_to_geojson_geom,
parse_and_filter_geojson,
parse_geojson_file_to_featcol,
)
from app.models.enums import HTTPStatus, TaskStatus, XLSFormType
from app.projects import project_schemas
Expand Down Expand Up @@ -568,7 +568,7 @@ async def convert_geojson_to_odk_csv(
Returns:
feature_csv (StringIO): CSV of features in XLSForm format for ODK.
"""
parsed_geojson = parse_and_filter_geojson(input_geojson.getvalue(), filter=False)
parsed_geojson = parse_geojson_file_to_featcol(input_geojson.getvalue())

if not parsed_geojson:
raise HTTPException(
Expand Down
Loading

0 comments on commit 018605b

Please sign in to comment.