Skip to content

Commit

Permalink
Create families with multiple geographies (#212)
Browse files Browse the repository at this point in the history
* Allow for creating families with multiple geographies

* Fix merge

* Make geographies filed on family support single and multiple values

* Bump patch version

* Revert change to rename geography field on FamilyCreateDto

* Undo
  • Loading branch information
annaCPR authored Sep 11, 2024
1 parent 1d42fd9 commit 7c860be
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/model/family.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Optional
from typing import Optional, Union

from pydantic import BaseModel

Expand Down Expand Up @@ -60,7 +60,7 @@ class FamilyCreateDTO(BaseModel):
import_id: Optional[str] = None
title: str
summary: str
geography: str
geography: Union[str, list[str]]
category: str
metadata: Json
collections: list[str]
Expand Down
4 changes: 2 additions & 2 deletions app/model/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class IngestFamilyDTO(BaseModel):
import_id: str
title: str
summary: str
geography: str
geographies: list[str]
category: str
metadata: Json
collections: list[str]
Expand All @@ -52,7 +52,7 @@ def to_family_create_dto(self, corpus_import_id: str) -> FamilyCreateDTO:
import_id=self.import_id,
title=self.title,
summary=self.summary,
geography=self.geography,
geography=self.geographies,
category=self.category,
metadata=self.metadata,
collections=self.collections,
Expand Down
7 changes: 5 additions & 2 deletions app/repository/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ def update(db: Session, import_id: str, family: FamilyWriteDTO, geo_id: int) ->
return True


def create(db: Session, family: FamilyCreateDTO, geo_id: int, org_id: int) -> str:
def create(
db: Session, family: FamilyCreateDTO, geo_ids: list[int], org_id: int
) -> str:
"""
Creates a new family.
Expand All @@ -379,7 +381,8 @@ def create(db: Session, family: FamilyCreateDTO, geo_id: int, org_id: int) -> st
db.add(new_family)

# TODO: PDCT-1406: Properly implement multi-geography support
db.add(FamilyGeography(family_import_id=import_id, geography_id=geo_id))
for geo_id in geo_ids:
db.add(FamilyGeography(family_import_id=import_id, geography_id=geo_id))

# Add corpus - family link.
db.add(
Expand Down
4 changes: 3 additions & 1 deletion app/repository/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def update(
...

@staticmethod
def create(db: Session, family: FamilyCreateDTO, geo_id: int, org_id: int) -> str:
def create(
db: Session, family: FamilyCreateDTO, geo_ids: list[int], org_id: int
) -> str:
"""Creates a family"""
...

Expand Down
11 changes: 8 additions & 3 deletions app/service/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ def create(
if db is None:
db = db_session.get_db()

# Validate geography
geo_id = geography.get_id(db, family.geography)
# Validate geographies
geo_ids = []
if isinstance(family.geography, str):
geo_ids.append(geography.get_id(db, family.geography))
elif isinstance(family.geography, list):
for geo_id in family.geography:
geo_ids.append(geography.get_id(db, geo_id))

# Validate category
category.validate(family.category)
Expand Down Expand Up @@ -218,7 +223,7 @@ def create(
metadata.validate_metadata(db, family.corpus_import_id, family.metadata)

try:
import_id = family_repo.create(db, family, geo_id, entity_org_id)
import_id = family_repo.create(db, family, geo_ids, entity_org_id)
if len(import_id) == 0:
db.rollback()
return import_id
Expand Down
7 changes: 4 additions & 3 deletions app/service/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ def save_families(
dto = IngestFamilyDTO(
**fam, corpus_import_id=corpus_import_id
).to_family_create_dto(corpus_import_id)
import_id = family_repository.create(
db, dto, geography.get_id(db, dto.geography), org_id
)
geo_ids = []
for geo in dto.geography:
geo_ids.append(geography.get_id(db, geo))
import_id = family_repository.create(db, dto, geo_ids, org_id)
family_import_ids.append(import_id)

return family_import_ids
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "admin_backend"
version = "2.15.0"
version = "2.15.1"
description = ""
authors = ["CPR-dev-team <[email protected]>"]
packages = [{ include = "app" }, { include = "tests" }]
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_tests/ingest/test_bulk_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"import_id": "test.new.family.0",
"title": "Test",
"summary": "Test",
"geography": "South Asia",
"geographies": ["South Asia"],
"category": "UNFCCC",
"metadata": {
"author_type": ["Non-Party"],
Expand All @@ -28,7 +28,7 @@
"import_id": "test.new.family.1",
"title": "Test",
"summary": "Test",
"geography": "South Asia",
"geographies": ["South Asia"],
"category": "UNFCCC",
"metadata": {
"author_type": ["Party"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"import_id": "test.new.family.0",
"title": "Test",
"summary": "Test",
"geography": "South Asia",
"geographies": ["South Asia"],
"category": "UNFCCC",
"metadata": {
"author_type": ["Non-Party"],
Expand Down
6 changes: 5 additions & 1 deletion tests/integration_tests/ingest/test_ingest_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def test_get_template_unfcc(
"import_id": {"title": "Import Id", "type": "string"},
"title": {"title": "Title", "type": "string"},
"summary": {"title": "Summary", "type": "string"},
"geography": {"title": "Geography", "type": "string"},
"geographies": {
"items": {"type": "string"},
"title": "Geographies",
"type": "array",
},
"category": {"title": "Category", "type": "string"},
"metadata": {
"author": {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/routers/ingest/test_bulk_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"import_id": "test.new.family.0",
"title": "Test",
"summary": "Test",
"geography": "Test",
"geographies": ["Test"],
"category": "UNFCCC",
"metadata": {
"color": ["blue"],
Expand All @@ -28,7 +28,7 @@
"import_id": "test.new.family.1",
"title": "Test",
"summary": "Test",
"geography": "Test",
"geographies": ["Test"],
"category": "UNFCCC",
"metadata": {
"color": ["pink"],
Expand Down
6 changes: 5 additions & 1 deletion tests/unit_tests/routers/ingest/test_ingest_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def test_ingest_template_when_ok(
"import_id": {"title": "Import Id", "type": "string"},
"title": {"title": "Title", "type": "string"},
"summary": {"title": "Summary", "type": "string"},
"geography": {"title": "Geography", "type": "string"},
"geographies": {
"items": {"type": "string"},
"title": "Geographies",
"type": "array",
},
"category": {"title": "Category", "type": "string"},
"metadata": {
"test": {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/service/ingest/test_ingest_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_ingest_when_ok(
"import_id": "test.new.family.0",
"title": "Test",
"summary": "Test",
"geography": "Test",
"geographies": ["Test"],
"category": "UNFCCC",
"metadata": {"color": ["blue"], "size": [""]},
"collections": ["test.new.collection.0"],
Expand Down

0 comments on commit 7c860be

Please sign in to comment.