diff --git a/.gitignore b/.gitignore index 8d4aff3..af2bdb4 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ venv/ coverage.xml *.pg_restore + +src/* \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index feff2fe..0ff20b1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,10 +11,7 @@ "program": "main.py", "console": "integratedTerminal", "justMyCode": true, - "env":{ - // "HAPI_DB_NAME": "hapi", - "HAPI_USE_VAT": "false" - } + "env":{"HAPI_USE_VAT": "true"} } ], diff --git a/hdx_hapi/db/dao/humanitarian_needs_view_dao.py b/hdx_hapi/db/dao/humanitarian_needs_view_dao.py index 4dbbbcb..586d761 100644 --- a/hdx_hapi/db/dao/humanitarian_needs_view_dao.py +++ b/hdx_hapi/db/dao/humanitarian_needs_view_dao.py @@ -10,7 +10,7 @@ apply_reference_period_filter, ) from hdx_hapi.endpoints.util.util import PaginationParams, ReferencePeriodParameters -from hapi_schema.utils.enums import DisabledMarker, Gender, PopulationGroup, PopulationStatus +from hapi_schema.utils.enums import PopulationStatus async def humanitarian_needs_view_list( @@ -18,11 +18,8 @@ async def humanitarian_needs_view_list( ref_period_parameters: ReferencePeriodParameters, db: AsyncSession, admin2_ref: Optional[int] = None, - gender: Optional[Gender] = None, - age_range: Optional[str] = None, - disabled_marker: Optional[DisabledMarker] = None, + category: Optional[str] = None, sector_code: Optional[str] = None, - population_group: Optional[PopulationGroup] = None, population_status: Optional[PopulationStatus] = None, population_min: Optional[int] = None, population_max: Optional[int] = None, @@ -42,16 +39,10 @@ async def humanitarian_needs_view_list( ): query = select(HumanitarianNeedsView) - if gender: - query = query.where(HumanitarianNeedsView.gender == gender) - if age_range: - query = query.where(HumanitarianNeedsView.age_range == age_range) - if disabled_marker: - query = query.where(HumanitarianNeedsView.disabled_marker == disabled_marker) + if category: + query = query.where(HumanitarianNeedsView.category.icontains(category)) if sector_code: query = query.where(HumanitarianNeedsView.sector_code.icontains(sector_code)) - if population_group: - query = query.where(HumanitarianNeedsView.population_group == population_group) if population_status: query = query.where(HumanitarianNeedsView.population_status == population_status) @@ -85,12 +76,9 @@ async def humanitarian_needs_view_list( query = apply_pagination(query, pagination_parameters) query = query.order_by( HumanitarianNeedsView.admin2_ref, - HumanitarianNeedsView.gender, - HumanitarianNeedsView.age_range, + HumanitarianNeedsView.category, HumanitarianNeedsView.sector_code, - HumanitarianNeedsView.population_group, HumanitarianNeedsView.population_status, - HumanitarianNeedsView.disabled_marker, HumanitarianNeedsView.reference_period_start, ) diff --git a/hdx_hapi/db/models/views/all_views.py b/hdx_hapi/db/models/views/all_views.py index 930b97b..a26a4cc 100644 --- a/hdx_hapi/db/models/views/all_views.py +++ b/hdx_hapi/db/models/views/all_views.py @@ -38,7 +38,6 @@ from hapi_schema.utils.enums import ( CommodityCategory, - DisabledMarker, EventType, IPCPhase, IPCType, @@ -283,14 +282,9 @@ class HumanitarianNeedsView(Base): __table__ = humanitarian_needs_view resource_hdx_id: Mapped[str] = column_property(humanitarian_needs_view.c.resource_hdx_id) admin2_ref: Mapped[int] = column_property(humanitarian_needs_view.c.admin2_ref) - gender: Mapped[Gender] = column_property(humanitarian_needs_view.c.gender) - age_range: Mapped[str] = column_property(humanitarian_needs_view.c.age_range) - min_age: Mapped[int] = column_property(humanitarian_needs_view.c.min_age) - max_age: Mapped[int] = column_property(humanitarian_needs_view.c.max_age) sector_code: Mapped[str] = column_property(humanitarian_needs_view.c.sector_code) - population_group: Mapped[PopulationGroup] = column_property(humanitarian_needs_view.c.population_group) + category: Mapped[str] = column_property(humanitarian_needs_view.c.category) population_status: Mapped[PopulationStatus] = column_property(humanitarian_needs_view.c.population_status) - disabled_marker: Mapped[DisabledMarker] = column_property(humanitarian_needs_view.c.disabled_marker) population: Mapped[int] = column_property(humanitarian_needs_view.c.population) reference_period_start: Mapped[datetime.datetime] = column_property( humanitarian_needs_view.c.reference_period_start diff --git a/hdx_hapi/endpoints/get_affected_people.py b/hdx_hapi/endpoints/get_affected_people.py index b9b6145..e178cd5 100644 --- a/hdx_hapi/endpoints/get_affected_people.py +++ b/hdx_hapi/endpoints/get_affected_people.py @@ -6,7 +6,6 @@ from hdx_hapi.config.config import get_config from hdx_hapi.config.doc_snippets import ( - DOC_DISABLED_MARKER, DOC_GENDER, DOC_AGE_RANGE, DOC_POPULATION_GROUP, @@ -36,7 +35,7 @@ from hdx_hapi.services.humanitarian_needs_logic import get_humanitarian_needs_srv from hdx_hapi.services.refugees_logic import get_refugees_srv from hdx_hapi.services.sql_alchemy_session import get_db -from hapi_schema.utils.enums import DisabledMarker, Gender, PopulationGroup, PopulationStatus +from hapi_schema.utils.enums import Gender, PopulationGroup, PopulationStatus from hdx_hapi.endpoints.util.util import ( CommonEndpointParams, OutputFormat, @@ -145,13 +144,8 @@ async def get_humanitarian_needs( # ref_period_parameters: Annotated[ReferencePeriodParameters, Depends(reference_period_parameters)], common_parameters: Annotated[CommonEndpointParams, Depends(common_endpoint_parameters)], db: AsyncSession = Depends(get_db), - gender: Annotated[Optional[Gender], Query(max_length=3, description=f'{DOC_GENDER}')] = None, - age_range: Annotated[Optional[str], Query(max_length=32, description=f'{DOC_AGE_RANGE}')] = None, - disabled_marker: Annotated[Optional[DisabledMarker], Query(description=f'{DOC_DISABLED_MARKER}')] = None, + category: Annotated[Optional[str], Query(max_length=128, description='')] = None, sector_code: Annotated[Optional[str], Query(max_length=32, description=f'{DOC_SECTOR_CODE}')] = None, - population_group: Annotated[ - Optional[PopulationGroup], Query(max_length=32, description=f'{DOC_POPULATION_GROUP}') - ] = None, population_status: Annotated[ Optional[PopulationStatus], Query(max_length=32, description=f'{DOC_POPULATION_STATUS}') ] = None, @@ -192,11 +186,8 @@ async def get_humanitarian_needs( ref_period_parameters=ref_period_parameters, db=db, admin2_ref=admin2_ref, - gender=gender, - age_range=age_range, - disabled_marker=disabled_marker, + category=category, sector_code=sector_code, - population_group=population_group, population_status=population_status, population_min=population_min, population_max=population_max, diff --git a/hdx_hapi/endpoints/models/humanitarian_needs.py b/hdx_hapi/endpoints/models/humanitarian_needs.py index f88d37c..eab1cad 100644 --- a/hdx_hapi/endpoints/models/humanitarian_needs.py +++ b/hdx_hapi/endpoints/models/humanitarian_needs.py @@ -3,40 +3,18 @@ from typing import Optional from hdx_hapi.config.doc_snippets import ( - DOC_DISABLED_MARKER, - DOC_GENDER, - DOC_POPULATION_GROUP, DOC_POPULATION_STATUS, DOC_HDX_RESOURCE_ID, - DOC_AGE_RANGE, DOC_REFERENCE_PERIOD_START, DOC_REFERENCE_PERIOD_END, truncate_query_description, ) from hdx_hapi.endpoints.models.base import HapiBaseModel, HapiModelWithAdmins -from hapi_schema.utils.enums import Gender, PopulationGroup, PopulationStatus, DisabledMarker +from hapi_schema.utils.enums import PopulationStatus class HumanitarianNeedsResponse(HapiBaseModel, HapiModelWithAdmins): resource_hdx_id: str = Field(max_length=36, description=truncate_query_description(DOC_HDX_RESOURCE_ID)) - gender: Gender = Field(description=truncate_query_description(DOC_GENDER)) - age_range: str = Field(max_length=32, description=truncate_query_description(DOC_AGE_RANGE)) - min_age: Optional[int] = Field( - ge=0, - description=( - 'The minimum age from `age_range`, set to `null` if `age_range` is "all" and ' - 'there is no age disaggregation' - ), - ) - max_age: Optional[int] = Field( - ge=0, - description=( - 'The maximum age from `age_range`, set to `null` if `age_range` is "all" and ' - 'there is no age disaggregation, or if there is no upper limit to the ' - 'age range' - ), - ) - disabled_marker: DisabledMarker = Field(description=truncate_query_description(DOC_DISABLED_MARKER)) sector_code: str = Field( max_length=32, description=( @@ -45,7 +23,10 @@ class HumanitarianNeedsResponse(HapiBaseModel, HapiModelWithAdmins): 'dataset, or created for HDX HAPI' ), ) - population_group: PopulationGroup = Field(description=truncate_query_description(DOC_POPULATION_GROUP)) + category: str = Field( + max_length=128, + description='A category combining gender, age range, disability marker and population group information', + ) population_status: PopulationStatus = Field(description=truncate_query_description(DOC_POPULATION_STATUS)) population: int = Field(ge=0, description='The number of people') reference_period_start: datetime.datetime = Field(description=DOC_REFERENCE_PERIOD_START) diff --git a/hdx_hapi/services/humanitarian_needs_logic.py b/hdx_hapi/services/humanitarian_needs_logic.py index 5d28794..c90cd6f 100644 --- a/hdx_hapi/services/humanitarian_needs_logic.py +++ b/hdx_hapi/services/humanitarian_needs_logic.py @@ -6,7 +6,7 @@ from hdx_hapi.db.dao.humanitarian_needs_view_dao import humanitarian_needs_view_list from hdx_hapi.services.admin_level_logic import compute_unspecified_values from hdx_hapi.endpoints.util.util import AdminLevel, PaginationParams, ReferencePeriodParameters -from hapi_schema.utils.enums import DisabledMarker, Gender, PopulationGroup, PopulationStatus +from hapi_schema.utils.enums import PopulationStatus async def get_humanitarian_needs_srv( @@ -14,11 +14,8 @@ async def get_humanitarian_needs_srv( ref_period_parameters: ReferencePeriodParameters, db: AsyncSession, admin2_ref: Optional[int] = None, - gender: Optional[Gender] = None, - age_range: Optional[str] = None, - disabled_marker: Optional[DisabledMarker] = None, + category: Optional[str] = None, sector_code: Optional[str] = None, - population_group: Optional[PopulationGroup] = None, population_status: Optional[PopulationStatus] = None, population_min: Optional[int] = None, population_max: Optional[int] = None, @@ -44,11 +41,8 @@ async def get_humanitarian_needs_srv( ref_period_parameters=ref_period_parameters, db=db, admin2_ref=admin2_ref, - gender=gender, - age_range=age_range, - disabled_marker=disabled_marker, + category=category, sector_code=sector_code, - population_group=population_group, population_status=population_status, population_min=population_min, population_max=population_max, diff --git a/tests/sample_data/humanitarian_needs.sql b/tests/sample_data/humanitarian_needs.sql index 9c2e3cc..e533776 100644 --- a/tests/sample_data/humanitarian_needs.sql +++ b/tests/sample_data/humanitarian_needs.sql @@ -1,16 +1,16 @@ -- dummy data INSERT INTO public.humanitarian_needs -(resource_hdx_id, admin2_ref, gender, age_range, min_age, max_age, sector_code, population_group, population_status, disabled_marker, population, reference_period_start, reference_period_end) -VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab',2, 'all', 'ALL',0,99,'EDU','REF','AFF','y',500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00'); +(resource_hdx_id, admin2_ref, category, sector_code, population_status, population, reference_period_start, reference_period_end) +VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab',2, 'All - Disabled - all - AFF','EDU','AFF',500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00'); INSERT INTO public.humanitarian_needs -(resource_hdx_id, admin2_ref, gender, age_range, min_age, max_age, sector_code, population_group, population_status, disabled_marker, population, reference_period_start, reference_period_end) -VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 4, 'f', '0-17',0,17,'SHL','IDP','INN','n',1500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00'); +(resource_hdx_id, admin2_ref, category, sector_code, population_status, population, reference_period_start, reference_period_end) +VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab',4,'Female - child - INN','SHL','INN',1500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00'); INSERT INTO public.humanitarian_needs -(resource_hdx_id, admin2_ref, gender, age_range, min_age, max_age, sector_code, population_group, population_status, disabled_marker, population, reference_period_start, reference_period_end) -VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 6, 'm', '65+',65,100,'WSH','all','all','all',100000,'2023-01-01 00:00:00', '2025-01-01 00:00:00'); +(resource_hdx_id, admin2_ref, category, sector_code, population_status, population, reference_period_start, reference_period_end) +VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 6,'Male - elderly - all','WSH','all',100000,'2023-01-01 00:00:00', '2025-01-01 00:00:00'); INSERT INTO public.humanitarian_needs -(resource_hdx_id, admin2_ref, gender, age_range, min_age, max_age, sector_code, population_group, population_status, disabled_marker, population, reference_period_start, reference_period_end) -VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 4, 'f', '18-64',18,64,'SHL','IDP','INN','n',1500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00'); +(resource_hdx_id, admin2_ref, category, sector_code, population_status, population, reference_period_start, reference_period_end) +VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 4, 'Female - adult - INN','SHL','INN',1500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00'); INSERT INTO public.humanitarian_needs -(resource_hdx_id, admin2_ref, gender, age_range, min_age, max_age, sector_code, population_group, population_status, disabled_marker, population, reference_period_start, reference_period_end) -VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 6, 'm', '18-64',18,64,'WSH','all','all','all',100000,'2023-01-01 00:00:00', '2025-01-01 00:00:00'); +(resource_hdx_id, admin2_ref, category, sector_code, population_status, population, reference_period_start, reference_period_end) +VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 6,'Male - adult - all','WSH','all',100000,'2023-01-01 00:00:00', '2025-01-01 00:00:00'); diff --git a/tests/test_endpoints/endpoint_data.py b/tests/test_endpoints/endpoint_data.py index e469bc6..2cf694b 100644 --- a/tests/test_endpoints/endpoint_data.py +++ b/tests/test_endpoints/endpoint_data.py @@ -2,7 +2,6 @@ from hapi_schema.utils.enums import ( CommodityCategory, - DisabledMarker, EventType, Gender, PopulationGroup, @@ -456,11 +455,8 @@ '/api/v1/affected-people/humanitarian-needs': { 'query_parameters': { 'admin2_ref': 2, - 'gender': Gender.ALL.value, - 'age_range': 'ALL', - 'disabled_marker': DisabledMarker.YES.value, 'sector_code': 'EDU', - 'population_group': PopulationGroup.REFUGEES.value, + 'category': 'All - Disabled - all - AFF', 'population_status': PopulationStatus.AFFECTED.value, # 'reference_period_start_min': '2020-01-01T00:00:00', # 'reference_period_start_max': '2026-01-01T00:00:00', @@ -479,13 +475,8 @@ 'expected_fields': [ 'resource_hdx_id', 'admin2_ref', - 'gender', - 'age_range', - 'min_age', - 'max_age', - 'disabled_marker', + 'category', 'sector_code', - 'population_group', 'population_status', 'population', 'reference_period_start', diff --git a/tests/test_endpoints/test_humanitarian_needs_endpoint.py b/tests/test_endpoints/test_humanitarian_needs_endpoint.py index 239b7e9..8c1f2ee 100644 --- a/tests/test_endpoints/test_humanitarian_needs_endpoint.py +++ b/tests/test_endpoints/test_humanitarian_needs_endpoint.py @@ -3,6 +3,7 @@ from httpx import AsyncClient from hdx_hapi.endpoints.models.humanitarian_needs import HumanitarianNeedsResponse +from hapi_schema.utils.enums import PopulationStatus from main import app from tests.test_endpoints.endpoint_data import endpoint_data @@ -67,14 +68,9 @@ async def test_get_humanitarian_needs_adm_fields(event_loop, refresh_db): humanitarian_needs_view_adm_specified = HumanitarianNeedsResponse( resource_hdx_id='17acb541-9431-409a-80a8-50eda7e8ebab', - gender='f', - age_range='1-2', - min_age=1, - max_age=2, - disabled_marker='y', + category='', sector_code='EDU', - population_group='REF', - population_status='AFF', + population_status=PopulationStatus.AFFECTED, population=500000, reference_period_start='2023-01-01 00:00:00', reference_period_end='2023-03-31 23:59:59', @@ -111,14 +107,9 @@ async def test_get_humanitarian_needs_adm_fields(event_loop, refresh_db): humanitarian_needs_view_adm_unspecified = HumanitarianNeedsResponse( resource_hdx_id='17acb541-9431-409a-80a8-50eda7e8ebab', - gender='f', - age_range='1-2', - min_age=1, - max_age=2, - disabled_marker='y', sector_code='EDU', - population_group='REF', - population_status='AFF', + category='', + population_status=PopulationStatus.AFFECTED, population=500000, reference_period_start='2023-01-01 00:00:00', reference_period_end='2023-03-31 23:59:59',