diff --git a/app/api/api_v1/schemas/document.py b/app/api/api_v1/schemas/document.py index 6b3945d1..056c47fe 100644 --- a/app/api/api_v1/schemas/document.py +++ b/app/api/api_v1/schemas/document.py @@ -64,6 +64,8 @@ class FamilyDocumentResponse(BaseModel): cdn_object: Optional[str] source_url: Optional[str] content_type: Optional[str] + language: str + document_type: str class FamilyContext(BaseModel): @@ -72,6 +74,7 @@ class FamilyContext(BaseModel): title: str import_id: str geography: str + category: str slugs: list[str] published_date: Optional[datetime] last_updated_date: Optional[datetime] diff --git a/app/core/lookups.py b/app/core/lookups.py index 671ac442..60f8c7ba 100644 --- a/app/core/lookups.py +++ b/app/core/lookups.py @@ -5,6 +5,7 @@ from app.core.organisation import get_organisation_taxonomy_by_name from app.core.util import tree_table_to_json, table_to_json +from app.db.models.app.users import Organisation from app.db.models.law_policy import Geography from app.db.models.deprecated import ( Category, @@ -41,11 +42,13 @@ def get_metadata(db: Session): def get_config(db: Session) -> ApplicationConfig: - ORG_NAME = "CCLW" + org_names = db.query(Organisation.name).all() + return ApplicationConfig( geographies=tree_table_to_json(table=Geography, db=db), taxonomies={ - ORG_NAME: get_organisation_taxonomy_by_name(db=db, org_name=ORG_NAME) + org_name[0]: get_organisation_taxonomy_by_name(db=db, org_name=org_name[0]) + for org_name in org_names }, ) diff --git a/app/db/crud/document.py b/app/db/crud/document.py index 3218614b..d2c36432 100644 --- a/app/db/crud/document.py +++ b/app/db/crud/document.py @@ -15,7 +15,11 @@ FamilyEventsResponse, ) from app.db.models.app.users import Organisation -from app.db.models.document.physical_document import PhysicalDocument +from app.db.models.document.physical_document import ( + PhysicalDocument, + PhysicalDocumentLanguage, + Language, +) from app.db.models.law_policy.collection import Collection, CollectionFamily from app.db.models.law_policy.family import ( Family, @@ -71,6 +75,7 @@ def get_family_document_and_context( import_id=import_id, geography=cast(str, geography.value), slugs=slugs, + category=family.family_category, published_date=family.published_date, last_updated_date=family.last_updated_date, ) @@ -83,11 +88,23 @@ def get_family_document_and_context( cdn_object=to_cdn_url(physical_document.cdn_object), source_url=physical_document.source_url, content_type=physical_document.content_type, + language=_get_language_for_phys_doc(db, physical_document.id), + document_type=document.document_type, ) return FamilyDocumentWithContextResponse(family=family, document=document) +def _get_language_for_phys_doc(db: Session, physical_document_id: str) -> str: + language = ( + db.query(Language) + .filter(PhysicalDocumentLanguage.document_id == physical_document_id) + .filter(Language.id == PhysicalDocumentLanguage.language_id) + ).one_or_none() + + return cast(str, language.code) if language else "" + + def get_family_and_documents( db: Session, import_id: str ) -> Optional[FamilyAndDocumentsResponse]: @@ -213,6 +230,8 @@ def _get_documents_for_family_import_id( cdn_object=to_cdn_url(pd.cdn_object), source_url=pd.source_url, content_type=pd.content_type, + language=_get_language_for_phys_doc(db, pd.id), + document_type=d.document_type, ) for d, pd in db_documents ] diff --git a/tests/routes/test_documents.py b/tests/routes/test_documents.py index 89f6c662..5dcbbb3d 100644 --- a/tests/routes/test_documents.py +++ b/tests/routes/test_documents.py @@ -259,9 +259,11 @@ def test_documents_doc_slug_preexisting_objects( family = json_response["family"] assert family + assert len(family.keys()) == 7 assert family["title"] == "Fam2" assert family["import_id"] == "CCLW.family.2002.0" assert family["geography"] == "GEO" + assert family["category"] == "Executive" assert len(family["slugs"]) == 1 assert family["slugs"][0] == "FamSlug2" assert family["published_date"] == "2019-12-25T00:00:00+00:00" @@ -269,6 +271,13 @@ def test_documents_doc_slug_preexisting_objects( doc = json_response["document"] assert doc - assert doc["title"] == "Title2" - assert doc["slugs"] == ["DocSlug2"] + assert len(doc) == 10 assert doc["import_id"] == "CCLW.executive.2.2" + assert doc["variant"] == "MAIN" + assert doc["slugs"] == ["DocSlug2"] + assert doc["title"] == "Title2" + assert doc["md5_sum"] is None + assert doc["cdn_object"] == "https://cdn.climatepolicyradar.org/" + assert doc["source_url"] == "http://another_somewhere" + assert doc["language"] == "" + assert doc["document_type"] == "Order"