Skip to content

Commit

Permalink
Merge pull request #12 from OCHA-DAP/hno_table
Browse files Browse the repository at this point in the history
HAPI-323 HNO table
  • Loading branch information
mcarans authored Dec 20, 2023
2 parents e0a348b + cf78633 commit e332bed
Show file tree
Hide file tree
Showing 33 changed files with 635 additions and 289 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.0]

### Added

- Humanitarian needs tables and views

## [0.4.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ classifiers = [
"Operating System :: Microsoft :: Windows",
]
requires-python = ">=3.8"
dependencies = ['sqlalchemy']
dependencies = ["sqlalchemy"]
dynamic = ["version"]

[project.readme]
Expand Down
18 changes: 10 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,46 @@
cfgv==3.4.0
# via pre-commit
coverage[toml]==7.3.2
# via pytest-cov
# via
# coverage
# pytest-cov
distlib==0.3.7
# via virtualenv
filelock==3.12.4
filelock==3.13.1
# via virtualenv
greenlet==3.0.0
greenlet==3.0.1
# via sqlalchemy
hdx-python-database==1.2.9
# via hapi-schema (pyproject.toml)
identify==2.5.30
identify==2.5.32
# via pre-commit
iniconfig==2.0.0
# via pytest
nodeenv==1.8.0
# via pre-commit
packaging==23.2
# via pytest
platformdirs==3.11.0
platformdirs==4.0.0
# via virtualenv
pluggy==1.3.0
# via pytest
pre-commit==3.5.0
# via hapi-schema (pyproject.toml)
pytest==7.4.2
pytest==7.4.3
# via
# hapi-schema (pyproject.toml)
# pytest-cov
pytest-cov==4.1.0
# via hapi-schema (pyproject.toml)
pyyaml==6.0.1
# via pre-commit
sqlalchemy==2.0.22
sqlalchemy==2.0.23
# via
# hapi-schema (pyproject.toml)
# hdx-python-database
typing-extensions==4.8.0
# via sqlalchemy
virtualenv==20.24.5
virtualenv==20.24.7
# via pre-commit

# The following packages are considered to be unsafe in a requirements file:
Expand Down
4 changes: 2 additions & 2 deletions src/hapi_schema/db_gender.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Gender table and view."""

from sqlalchemy import CHAR, String, select
from sqlalchemy import String, select
from sqlalchemy.orm import Mapped, mapped_column

from hapi_schema.utils.base import Base
Expand All @@ -10,7 +10,7 @@
class DBGender(Base):
__tablename__ = "gender"

code: Mapped[str] = mapped_column(CHAR(1), primary_key=True)
code: Mapped[str] = mapped_column(String(1), primary_key=True)
description: Mapped[str] = mapped_column(String(256), nullable=False)


Expand Down
132 changes: 132 additions & 0 deletions src/hapi_schema/db_humanitarian_needs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
"""HumanitarianNeeds table and view."""
from datetime import datetime

from sqlalchemy import (
Boolean,
DateTime,
ForeignKey,
Integer,
Text,
select,
text,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship

from hapi_schema.db_admin1 import DBAdmin1
from hapi_schema.db_admin2 import DBAdmin2
from hapi_schema.db_dataset import DBDataset
from hapi_schema.db_location import DBLocation
from hapi_schema.db_resource import DBResource
from hapi_schema.db_sector import DBSector
from hapi_schema.utils.base import Base
from hapi_schema.utils.view_params import ViewParams


class DBHumanitarianNeeds(Base):
__tablename__ = "humanitarian_needs"

id: Mapped[int] = mapped_column(Integer, primary_key=True)
resource_ref: Mapped[int] = mapped_column(
ForeignKey("resource.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
)
admin2_ref: Mapped[int] = mapped_column(
ForeignKey("admin2.id", onupdate="CASCADE"), nullable=False
)
gender_code: Mapped[str] = mapped_column(
ForeignKey("gender.code", onupdate="CASCADE"), nullable=True
)
age_range_code: Mapped[str] = mapped_column(
ForeignKey("age_range.code", onupdate="CASCADE"), nullable=True
)
disabled_marker: Mapped[bool] = mapped_column(
Boolean, nullable=True, server_default=text("NULL")
)
sector_code: Mapped[str] = mapped_column(
ForeignKey("sector.code", onupdate="CASCADE"), nullable=True
)
population_group_code: Mapped[str] = mapped_column(
ForeignKey("population_group.code", onupdate="CASCADE"), nullable=True
)
population_status_code: Mapped[str] = mapped_column(
ForeignKey("population_status.code", onupdate="CASCADE"),
nullable=True,
)
population: Mapped[int] = mapped_column(
Integer, nullable=False, index=True
)
reference_period_start: Mapped[datetime] = mapped_column(
DateTime, nullable=False, index=True
)
reference_period_end: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL")
)
source_data: Mapped[str] = mapped_column(Text, nullable=True)

resource = relationship("DBResource")
admin2 = relationship("DBAdmin2")
gender = relationship("DBGender")
age_range = relationship("DBAgeRange")
sector = relationship("DBSector")
population_group = relationship("DBPopulationGroup")
population_status = relationship("DBPopulationStatus")


view_params_humanitarian_needs = ViewParams(
name="humanitarian_needs_view",
metadata=Base.metadata,
selectable=select(
*DBHumanitarianNeeds.__table__.columns,
DBDataset.hdx_id.label("dataset_hdx_id"),
DBDataset.hdx_stub.label("dataset_hdx_stub"),
DBDataset.title.label("dataset_title"),
DBDataset.hdx_provider_stub.label("dataset_hdx_provider_stub"),
DBDataset.hdx_provider_name.label("dataset_hdx_provider_name"),
DBResource.hdx_id.label("resource_hdx_id"),
DBResource.name.label("resource_name"),
DBResource.update_date.label("resource_update_date"),
DBLocation.code.label("location_code"),
DBLocation.name.label("location_name"),
DBAdmin1.code.label("admin1_code"),
DBAdmin1.name.label("admin1_name"),
DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
DBAdmin2.code.label("admin2_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
DBSector.name.label("sector_name"),
).select_from(
# Join pop to admin2 to admin1 to loc
DBHumanitarianNeeds.__table__.join(
DBAdmin2.__table__,
DBHumanitarianNeeds.admin2_ref == DBAdmin2.id,
isouter=True,
)
.join(
DBAdmin1.__table__,
DBAdmin2.admin1_ref == DBAdmin1.id,
isouter=True,
)
.join(
DBLocation.__table__,
DBAdmin1.location_ref == DBLocation.id,
isouter=True,
)
# Join needs to resource to dataset
.join(
DBResource.__table__,
DBHumanitarianNeeds.resource_ref == DBResource.id,
isouter=True,
)
.join(
DBDataset.__table__,
DBResource.dataset_ref == DBDataset.id,
isouter=True,
)
# Join needs to sector
.join(
DBSector.__table__,
DBHumanitarianNeeds.sector_code == DBSector.code,
isouter=True,
)
),
)
12 changes: 6 additions & 6 deletions src/hapi_schema/db_operational_presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class DBOperationalPresence(Base):
ForeignKey("resource.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
)
org_ref = mapped_column(
admin2_ref: Mapped[int] = mapped_column(
ForeignKey("admin2.id", onupdate="CASCADE"), nullable=False
)
org_ref: Mapped[str] = mapped_column(
ForeignKey("org.id", onupdate="CASCADE"), nullable=False
)
sector_code = mapped_column(
sector_code: Mapped[str] = mapped_column(
ForeignKey("sector.code", onupdate="CASCADE"), nullable=False
)
admin2_ref: Mapped[int] = mapped_column(
ForeignKey("admin2.id", onupdate="CASCADE"), nullable=False
)
reference_period_start: Mapped[datetime] = mapped_column(
DateTime, nullable=False, index=True
)
Expand All @@ -49,9 +49,9 @@ class DBOperationalPresence(Base):
source_data: Mapped[str] = mapped_column(Text, nullable=True)

resource = relationship("DBResource")
admin2 = relationship("DBAdmin2")
org = relationship("DBOrg")
sector = relationship("DBSector")
admin2 = relationship("DBAdmin2")


view_params_operational_presence = ViewParams(
Expand Down
19 changes: 2 additions & 17 deletions src/hapi_schema/db_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

from hapi_schema.db_admin1 import DBAdmin1
from hapi_schema.db_admin2 import DBAdmin2
from hapi_schema.db_age_range import DBAgeRange
from hapi_schema.db_dataset import DBDataset
from hapi_schema.db_gender import DBGender
from hapi_schema.db_location import DBLocation
from hapi_schema.db_resource import DBResource
from hapi_schema.utils.base import Base
Expand Down Expand Up @@ -52,8 +50,8 @@ class DBPopulation(Base):

resource = relationship("DBResource")
admin2 = relationship("DBAdmin2")
age_range = relationship("DBAgeRange")
gender = relationship("DBGender")
age_range = relationship("DBAgeRange")


view_params_population = ViewParams(
Expand All @@ -69,15 +67,14 @@ class DBPopulation(Base):
DBResource.hdx_id.label("resource_hdx_id"),
DBResource.name.label("resource_name"),
DBResource.update_date.label("resource_update_date"),
DBGender.description.label("gender_description"),
DBLocation.code.label("location_code"),
DBLocation.name.label("location_name"),
DBAdmin1.code.label("admin1_code"),
DBAdmin1.name.label("admin1_name"),
DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
DBAdmin2.code.label("admin2_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.is_unspecified.label("admin2_is_unspecified")
DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
).select_from(
# Join pop to admin2 to admin1 to loc
DBPopulation.__table__.join(
Expand Down Expand Up @@ -106,17 +103,5 @@ class DBPopulation(Base):
DBResource.dataset_ref == DBDataset.id,
isouter=True,
)
# Join pop to gender
.join(
DBGender.__table__,
DBPopulation.gender_code == DBGender.code,
isouter=True,
)
# Join pop to age range
.join(
DBAgeRange.__table__,
DBPopulation.age_range_code == DBAgeRange.code,
isouter=True,
)
),
)
23 changes: 23 additions & 0 deletions src/hapi_schema/db_population_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Population group table and view."""

from sqlalchemy import String, select
from sqlalchemy.orm import Mapped, mapped_column

from hapi_schema.utils.base import Base
from hapi_schema.utils.view_params import ViewParams


class DBPopulationGroup(Base):
__tablename__ = "population_group"

code: Mapped[str] = mapped_column(String(32), primary_key=True)
description: Mapped[str] = mapped_column(
String(512), nullable=False, index=True
)


view_params_population_group = ViewParams(
name="population_group_view",
metadata=Base.metadata,
selectable=select(*DBPopulationGroup.__table__.columns),
)
23 changes: 23 additions & 0 deletions src/hapi_schema/db_population_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Population status table and view."""

from sqlalchemy import String, select
from sqlalchemy.orm import Mapped, mapped_column

from hapi_schema.utils.base import Base
from hapi_schema.utils.view_params import ViewParams


class DBPopulationStatus(Base):
__tablename__ = "population_status"

code: Mapped[str] = mapped_column(String(32), primary_key=True)
description: Mapped[str] = mapped_column(
String(512), nullable=False, index=True
)


view_params_population_status = ViewParams(
name="population_status_view",
metadata=Base.metadata,
selectable=select(*DBPopulationStatus.__table__.columns),
)
Loading

0 comments on commit e332bed

Please sign in to comment.