Skip to content

Commit

Permalink
More endpoint changes (#62)
Browse files Browse the repository at this point in the history
* query all orgs

* Add doc_type and language to docs and category to family
  • Loading branch information
diversemix authored Mar 21, 2023
1 parent b46b406 commit 2f1948e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/api/api_v1/schemas/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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]
Expand Down
7 changes: 5 additions & 2 deletions app/core/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
},
)

Expand Down
21 changes: 20 additions & 1 deletion app/db/crud/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand All @@ -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]:
Expand Down Expand Up @@ -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
]
Expand Down
13 changes: 11 additions & 2 deletions tests/routes/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,25 @@ 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"
assert family["last_updated_date"] == "2019-12-25T00:00:00+00:00"

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"

0 comments on commit 2f1948e

Please sign in to comment.