diff --git a/ebl/dossiers/domain/dossier_record.py b/ebl/dossiers/domain/dossier_record.py index f93943937..fe4d4ad65 100644 --- a/ebl/dossiers/domain/dossier_record.py +++ b/ebl/dossiers/domain/dossier_record.py @@ -3,7 +3,7 @@ from ebl.common.domain.provenance import Provenance from ebl.fragmentarium.domain.fragment import Script -from ebl.bibliography.domain.reference import ReferenceType +from ebl.bibliography.domain.reference import Reference @attr.s(frozen=True, auto_attribs=True) @@ -16,4 +16,4 @@ class DossierRecord: related_kings: Sequence[float] = [] provenance: Optional[Provenance] = None script: Optional[Script] = None - references: Sequence[ReferenceType] = [] + references: Sequence[Reference] = () diff --git a/ebl/dossiers/infrastructure/mongo_dossiers_repository.py b/ebl/dossiers/infrastructure/mongo_dossiers_repository.py index 724d50b2b..c11bf58ee 100644 --- a/ebl/dossiers/infrastructure/mongo_dossiers_repository.py +++ b/ebl/dossiers/infrastructure/mongo_dossiers_repository.py @@ -8,10 +8,9 @@ from ebl.dossiers.application.dossiers_repository import DossiersRepository from ebl.common.domain.provenance import Provenance from ebl.fragmentarium.application.fragment_fields_schemas import ScriptSchema -from ebl.schemas import NameEnumField -from ebl.bibliography.domain.reference import ReferenceType +from ebl.bibliography.application.reference_schema import ReferenceSchema -COLLECTION = "dossier" +COLLECTION = "dossiers" provenance_field = fields.Function( lambda object_: getattr(object_.provenance, "long_name", None), @@ -40,10 +39,13 @@ class Meta: ) provenance = provenance_field script = fields.Nested(ScriptSchema, allow_none=True, load_default=None) - references = fields.List(NameEnumField(ReferenceType), load_default=list) + references = fields.Nested( + ReferenceSchema, allow_none=True, many=True, load_default=() + ) @post_load def make_record(self, data, **kwargs): + data["references"] = tuple(data["references"]) return DossierRecord(**data) @@ -53,7 +55,8 @@ def __init__(self, database: Database): def query_by_ids(self, ids: Sequence[str]) -> Sequence[DossierRecord]: cursor = self._collection.find_many({"_id": {"$in": ids}}) - return DossierRecordSchema(many=True).load(cursor) + records = DossierRecordSchema(many=True).load(cursor) + return records def create(self, dossier_record: DossierRecord) -> str: return self._collection.insert_one(DossierRecordSchema().dump(dossier_record)) diff --git a/ebl/dossiers/web/dossier_records.py b/ebl/dossiers/web/dossier_records.py index 6bf3fe0c5..3ace0e5cb 100644 --- a/ebl/dossiers/web/dossier_records.py +++ b/ebl/dossiers/web/dossier_records.py @@ -1,4 +1,5 @@ from falcon import Request, Response +from urllib.parse import parse_qs from ebl.errors import NotFoundError from marshmallow import EXCLUDE @@ -14,11 +15,15 @@ def __init__(self, _dossiersRepository: DossiersRepository): def on_get(self, req: Request, resp: Response) -> None: try: - dossiers = self._dossiersRepository.query_by_ids( - req.params["ids"].split(",") - ) + parsed_params = parse_qs(req.query_string) + ids = parsed_params.get("ids[]", []) + if not ids: + raise ValueError("No valid IDs provided in the request.") + + dossiers = self._dossiersRepository.query_by_ids(ids) except ValueError as error: raise NotFoundError( - f"No dossier records matching {str(req.params)} found." + f"No dossier records matching {req.params} found." ) from error + resp.media = DossierRecordSchema(unknown=EXCLUDE, many=True).dump(dossiers) diff --git a/ebl/tests/dossiers/test_dossier.py b/ebl/tests/dossiers/test_dossier.py index f88f0d7bb..11dbc58c5 100644 --- a/ebl/tests/dossiers/test_dossier.py +++ b/ebl/tests/dossiers/test_dossier.py @@ -7,8 +7,9 @@ ) from ebl.tests.factories.dossier import DossierRecordFactory from ebl.fragmentarium.domain.fragment import Script -from ebl.fragmentarium.application.fragment_fields_schemas import ScriptSchema from ebl.common.domain.provenance import Provenance +from ebl.fragmentarium.application.fragment_fields_schemas import ScriptSchema +from ebl.bibliography.application.reference_schema import ReferenceSchema @pytest.fixture @@ -27,7 +28,7 @@ def test_dossier_record_creation( assert isinstance(dossier_record.related_kings, (list, type(None))) assert isinstance(dossier_record.provenance, (Provenance, type(None))) assert isinstance(dossier_record.script, (Script, type(None))) - assert isinstance(dossier_record.references, (list, type(None))) + assert isinstance(dossier_record.references, (tuple, type(None))) def test_dossier_record_defaults() -> None: @@ -41,7 +42,7 @@ def test_dossier_record_defaults() -> None: assert blank_dossier_record.related_kings == [] assert blank_dossier_record.provenance is None assert blank_dossier_record.script is None - assert blank_dossier_record.references == [] + assert blank_dossier_record.references == () def test_dossier_record_to_dict( @@ -58,10 +59,7 @@ def test_dossier_record_to_dict( if dossier_record.provenance else None, "script": ScriptSchema().dump(dossier_record.script), - "references": [ - str(reference).replace("ReferenceType.", "") - for reference in dossier_record.references - ], + "references": ReferenceSchema().dump(dossier_record.references, many=True), } diff --git a/ebl/tests/dossiers/test_dossiers_route.py b/ebl/tests/dossiers/test_dossiers_route.py index c20342f77..18ecde623 100644 --- a/ebl/tests/dossiers/test_dossiers_route.py +++ b/ebl/tests/dossiers/test_dossiers_route.py @@ -39,7 +39,7 @@ def test_fetch_dossier_record_route( dossiers_repository.create(unrelated_dossier_record) get_result = client.simulate_get( "/dossiers", - params={"ids": ",".join([dossier_record.id, another_dossier_record.id])}, + params={"ids[]": [dossier_record.id, another_dossier_record.id]}, ) assert get_result.status == falcon.HTTP_OK diff --git a/ebl/tests/factories/dossier.py b/ebl/tests/factories/dossier.py index c159deb27..a20fa69be 100644 --- a/ebl/tests/factories/dossier.py +++ b/ebl/tests/factories/dossier.py @@ -6,7 +6,7 @@ from ebl.common.domain.provenance import Provenance from ebl.tests.factories.fragment import ScriptFactory from ebl.chronology.chronology import chronology -from ebl.bibliography.domain.reference import ReferenceType +from ebl.tests.factories.bibliography import ReferenceFactory class DossierRecordFactory(factory.Factory): @@ -28,5 +28,5 @@ class Meta: provenance = factory.fuzzy.FuzzyChoice(set(Provenance) - {Provenance.STANDARD_TEXT}) script = factory.SubFactory(ScriptFactory) references = factory.LazyAttribute( - lambda _: list({list(ReferenceType)[i] for i in range(randint(1, 6))}) + lambda _: tuple(ReferenceFactory() for _ in range(randint(1, 5))) )