-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from OCHA-DAP/hno_table
HAPI-323 HNO table
- Loading branch information
Showing
33 changed files
with
635 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) |
Oops, something went wrong.