Skip to content

Commit

Permalink
Feature/pdct 1256 return corpus_type on document read dto (#176)
Browse files Browse the repository at this point in the history
* Return corpus_type on document read DTO

* Bump to 2.10.11

* Add missing corpus_type during unpack

* Fix test
  • Loading branch information
katybaulch authored Jul 17, 2024
1 parent 47bf1e3 commit 9a61abd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/model/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class DocumentReadDTO(BaseModel):
# From FamilyDocument
import_id: str
family_import_id: str
corpus_type: str
variant_name: Optional[str]
status: DocumentStatus
type: Optional[str]
Expand Down
20 changes: 16 additions & 4 deletions app/repository/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PhysicalDocumentLanguage,
)
from db_client.models.organisation import Organisation
from db_client.models.organisation.corpus import CorpusType
from db_client.models.organisation.counters import CountedEntity
from pydantic import AnyHttpUrl
from sqlalchemy import Column, and_
Expand All @@ -36,7 +37,9 @@
_LOGGER = logging.getLogger(__name__)

CreateObjects = Tuple[PhysicalDocumentLanguage, FamilyDocument, PhysicalDocument]
ReadObj = Tuple[FamilyDocument, PhysicalDocument, Organisation, Language, Language]
ReadObj = Tuple[
FamilyDocument, PhysicalDocument, CorpusType, Organisation, Language, Language
]


def _get_query(db: Session) -> Query:
Expand All @@ -49,7 +52,14 @@ def _get_query(db: Session) -> Query:
pdl_user = aliased(PhysicalDocumentLanguage)

return (
db.query(FamilyDocument, PhysicalDocument, Organisation, lang_user, lang_model)
db.query(
FamilyDocument,
PhysicalDocument,
CorpusType,
Organisation,
lang_user,
lang_model,
)
.filter(FamilyDocument.family_import_id == Family.import_id)
.join(Family, FamilyDocument.family_import_id == Family.import_id)
.join(
Expand All @@ -59,6 +69,7 @@ def _get_query(db: Session) -> Query:
)
.join(FamilyCorpus, FamilyCorpus.family_import_id == Family.import_id)
.join(Corpus, Corpus.import_id == FamilyCorpus.corpus_import_id)
.join(CorpusType, Corpus.corpus_type_name == CorpusType.name)
.join(Organisation, Corpus.organisation_id == Organisation.id)
.join(
pdl_user,
Expand Down Expand Up @@ -121,10 +132,11 @@ def _document_tuple_from_create_dto(


def _doc_to_dto(doc_query_return: ReadObj) -> DocumentReadDTO:
fdoc, pdoc, org, lang_user, lang_model = doc_query_return
fdoc, pdoc, corpus_type, org, lang_user, lang_model = doc_query_return
return DocumentReadDTO(
import_id=str(fdoc.import_id),
family_import_id=str(fdoc.family_import_id),
corpus_type=str(corpus_type.name),
variant_name=str(fdoc.variant_name) if fdoc.variant_name is not None else None,
status=cast(DocumentStatus, fdoc.document_status),
type=str(fdoc.document_type) if fdoc.document_type is not None else None,
Expand Down Expand Up @@ -485,5 +497,5 @@ def get_org_from_import_id(db: Session, import_id: str) -> Optional[int]:
result = _get_query(db).filter(FamilyDocument.import_id == import_id).one_or_none()
if result is None:
return None
_, _, org, _, _ = result
_, _, _, org, _, _ = result
return org.id
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.10.10"
version = "2.10.11"
description = ""
authors = ["CPR-dev-team <[email protected]>"]
packages = [{ include = "app" }, { include = "tests" }]
Expand Down
4 changes: 4 additions & 0 deletions tests/helpers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ def create_document_read_dto(
title: str = "title",
variant_name: Optional[str] = "Original Language",
metadata: Optional[Json] = None,
corpus_type: Optional[str] = None,
) -> DocumentReadDTO:
if metadata is None:
metadata = {"role": ["MAIN"]}
if corpus_type is None:
corpus_type = "Laws and Policies"
return DocumentReadDTO(
import_id=import_id,
family_import_id=family_import_id,
corpus_type=corpus_type,
variant_name=variant_name,
status=DocumentStatus.CREATED,
type="Law",
Expand Down
3 changes: 3 additions & 0 deletions tests/integration_tests/setup_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
{
"import_id": "D.0.0.1",
"family_import_id": "A.0.0.3",
"corpus_type": "Intl. agreements",
"variant_name": "Original Language",
"status": "Created",
"type": "Law",
Expand All @@ -154,6 +155,7 @@
{
"import_id": "D.0.0.2",
"family_import_id": "A.0.0.3",
"corpus_type": "Intl. agreements",
"variant_name": "Original Language",
"status": "Created",
"type": "Law",
Expand All @@ -172,6 +174,7 @@
{
"import_id": "D.0.0.3",
"family_import_id": "A.0.0.2",
"corpus_type": "Laws and Policies",
"variant_name": "Original Language",
"status": "Created",
"type": "Law",
Expand Down

0 comments on commit 9a61abd

Please sign in to comment.