Skip to content

Commit

Permalink
Merge branch 'main' into datetime_type_annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
turnerm authored Sep 3, 2024
2 parents a67fc00 + 8059fb5 commit f9d21b7
Show file tree
Hide file tree
Showing 23 changed files with 570 additions and 17 deletions.
6 changes: 5 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## 0.8.15

### Changes
### Added

- `coverage\_view`, which shows the locations available for each API endpoint

### Changed

- Applied feedback from this
[SQLAlchemy discussion](sqlalchemy/sqlalchemy#11748)
Expand Down
3 changes: 2 additions & 1 deletion contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ the file without committing by executing:

To add a new table to the schema:

1. Add a `db_[table_name].py` file to `src/hapi_schema` which implements the table as a class derived from `hapi_schema.utils.base.Base`
1. Add a `db_[table_name].py` file to `src/hapi_schema` which implements the table as a class derived from `hapi_schema.utils.base.Base`,
together with the view and coverage parameters.
2. Add a file `data_[table_name].py` to `tests/sample_data` which contains three rows of data for the new table as a list of dictionaries.
3. Add the table to the `tests/conftest.py` with an import like:
`from sample_data.data_[table_name] import data_[table_name]`
Expand Down
43 changes: 41 additions & 2 deletions src/hapi_schema/db_conflict_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
select,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql.expression import literal

from hapi_schema.db_admin1 import DBAdmin1, DBLocation
from hapi_schema.db_admin2 import DBAdmin2
from hapi_schema.db_resource import DBResource
from hapi_schema.utils.base import Base
from hapi_schema.utils.constraints import (
population_constraint,
Expand Down Expand Up @@ -50,8 +52,8 @@ class DBConflictEvent(Base):
nullable=False, index=True
)

resource = relationship("DBResource")
admin2 = relationship("DBAdmin2")
resource = relationship(DBResource)
admin2 = relationship(DBAdmin2)


# view
Expand Down Expand Up @@ -91,3 +93,40 @@ class DBConflictEvent(Base):
)
),
)

# Results format: category, subcategory, location_name, location_code, admin1_name, admin1_code, admin2_name, admin2_code, hapi_updated_date
availability_stmt_conflict_event = (
select(
literal("coordination-context").label("category"),
literal("conflict-event").label("subcategory"),
DBLocation.name.label("location_name"),
DBLocation.code.label("location_code"),
DBAdmin1.name.label("admin1_name"),
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
DBResource.hapi_updated_date,
)
.select_from(
DBConflictEvent.__table__.join(
DBAdmin2.__table__,
DBConflictEvent.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(
DBResource.__table__,
DBConflictEvent.resource_hdx_id == DBResource.hdx_id,
)
)
.distinct()
)
45 changes: 45 additions & 0 deletions src/hapi_schema/db_food_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
select,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql.expression import literal

from hapi_schema.db_admin2 import DBAdmin1, DBAdmin2, DBLocation
from hapi_schema.db_currency import DBCurrency
Expand Down Expand Up @@ -126,3 +127,47 @@ class DBFoodPrice(Base):
)
),
)


# Results format: category, subcategory, location_name, location_code, admin1_name, admin1_code, admin2_name, admin2_code, hapi_updated_date
availability_stmt_food_price = (
select(
literal("food").label("category"),
literal("food-price").label("subcategory"),
DBLocation.name.label("location_name"),
DBLocation.code.label("location_code"),
DBAdmin1.name.label("admin1_name"),
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
DBResource.hapi_updated_date,
)
.select_from(
# the admin2 comes from wfp_market
DBFoodPrice.__table__.join(
DBWFPMarket.__table__,
DBFoodPrice.market_code == DBWFPMarket.code,
isouter=True,
)
.join(
DBAdmin2.__table__,
DBWFPMarket.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(
DBResource.__table__,
DBFoodPrice.resource_hdx_id == DBResource.hdx_id,
)
)
.distinct()
)
39 changes: 39 additions & 0 deletions src/hapi_schema/db_food_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
select,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql.expression import literal

from hapi_schema.db_admin1 import DBAdmin1
from hapi_schema.db_admin2 import DBAdmin2
Expand Down Expand Up @@ -95,3 +96,41 @@ class DBFoodSecurity(Base):
)
),
)


# Results format: category, subcategory, location_name, location_code, admin1_name, admin1_code, admin2_name, admin2_code, hapi_updated_date
availability_stmt_food_security = (
select(
literal("food").label("category"),
literal("food-security").label("subcategory"),
DBLocation.name.label("location_name"),
DBLocation.code.label("location_code"),
DBAdmin1.name.label("admin1_name"),
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
DBResource.hapi_updated_date,
)
.select_from(
DBFoodSecurity.__table__.join(
DBAdmin2.__table__,
DBFoodSecurity.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(
DBResource.__table__,
DBFoodSecurity.resource_hdx_id == DBResource.hdx_id,
)
)
.distinct()
)
33 changes: 31 additions & 2 deletions src/hapi_schema/db_funding.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
text,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql import null
from sqlalchemy.sql.expression import literal

from hapi_schema.db_location import DBLocation
from hapi_schema.db_resource import DBResource
from hapi_schema.utils.base import Base
from hapi_schema.utils.constraints import reference_period_constraint
from hapi_schema.utils.view_params import ViewParams
Expand Down Expand Up @@ -72,8 +75,8 @@ class DBFunding(Base):
server_default=text("NULL"),
)

resource = relationship("DBResource")
location = relationship("DBLocation")
resource = relationship(DBResource)
location = relationship(DBLocation)


view_params_funding = ViewParams(
Expand All @@ -93,3 +96,29 @@ class DBFunding(Base):
)
),
)

# Results format: category, subcategory, location_name, location_code, admin1_name, admin1_code, admin2_name, admin2_code, hapi_updated_date
availability_stmt_funding = (
select(
literal("coordination-context").label("category"),
literal("funding").label("subcategory"),
DBLocation.name.label("location_name"),
DBLocation.code.label("location_code"),
null().label("admin1_name"),
null().label("admin1_code"),
null().label("admin2_name"),
null().label("admin2_code"),
DBResource.hapi_updated_date,
)
.select_from(
DBFunding.__table__.join(
DBLocation.__table__,
DBFunding.location_ref == DBLocation.id,
isouter=True,
).join(
DBResource.__table__,
DBFunding.resource_hdx_id == DBResource.hdx_id,
)
)
.distinct()
)
45 changes: 42 additions & 3 deletions src/hapi_schema/db_humanitarian_needs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
select,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql.expression import literal

from hapi_schema.db_admin1 import DBAdmin1
from hapi_schema.db_admin2 import DBAdmin2
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.constraints import (
Expand Down Expand Up @@ -79,9 +81,9 @@ class DBHumanitarianNeeds(Base):
nullable=False, index=True
)

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


view_params_humanitarian_needs = ViewParams(
Expand Down Expand Up @@ -127,3 +129,40 @@ class DBHumanitarianNeeds(Base):
)
),
)

# Results format: category, subcategory, location_name, location_code, admin1_name, admin1_code, admin2_name, admin2_code, hapi_updated_date
availability_stmt_humanitarian_needs = (
select(
literal("affected-people").label("category"),
literal("humanitarian-needs").label("subcategory"),
DBLocation.name.label("location_name"),
DBLocation.code.label("location_code"),
DBAdmin1.name.label("admin1_name"),
DBAdmin1.code.label("admin1_code"),
DBAdmin2.name.label("admin2_name"),
DBAdmin2.code.label("admin2_code"),
DBResource.hapi_updated_date,
)
.select_from(
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(
DBResource.__table__,
DBHumanitarianNeeds.resource_hdx_id == DBResource.hdx_id,
)
)
.distinct()
)
33 changes: 31 additions & 2 deletions src/hapi_schema/db_national_risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
select,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql import null
from sqlalchemy.sql.expression import literal

from hapi_schema.db_location import DBLocation
from hapi_schema.db_resource import DBResource
from hapi_schema.utils.base import Base
from hapi_schema.utils.constraints import (
general_risk_constraint,
Expand Down Expand Up @@ -74,8 +77,8 @@ class DBNationalRisk(Base):
nullable=False, index=True
)

resource = relationship("DBResource")
location = relationship("DBLocation")
resource = relationship(DBResource)
location = relationship(DBLocation)


view_params_national_risk = ViewParams(
Expand All @@ -96,3 +99,29 @@ class DBNationalRisk(Base):
)
),
)

# Results format: category, subcategory, location_name, location_code, admin1_name, admin1_code, admin2_name, admin2_code, hapi_updated_date
availability_stmt_national_risk = (
select(
literal("coordination-context").label("category"),
literal("national-risk").label("subcategory"),
DBLocation.name.label("location_name"),
DBLocation.code.label("location_code"),
null().label("admin1_name"),
null().label("admin1_code"),
null().label("admin2_name"),
null().label("admin2_code"),
DBResource.hapi_updated_date,
)
.select_from(
DBNationalRisk.__table__.join(
DBLocation.__table__,
DBNationalRisk.location_ref == DBLocation.id,
isouter=True,
).join(
DBResource.__table__,
DBNationalRisk.resource_hdx_id == DBResource.hdx_id,
)
)
.distinct()
)
Loading

0 comments on commit f9d21b7

Please sign in to comment.