From 97ced2c16c712d2ad01c78275fa0d3e127ac452b Mon Sep 17 00:00:00 2001 From: James Date: Mon, 7 Nov 2022 13:58:42 +0000 Subject: [PATCH] geojsontojson: Include contracts https://github.com/Open-Telecoms-Data/lib-cove-ofds/issues/25 --- CHANGELOG.md | 1 + libcoveofds/geojson.py | 22 +++++++++++++++++-- tests/__init__.py | 0 .../geojson_to_json/phases_1.expected.json | 12 ++++++++++ .../phases_1.meta.expected.json | 18 +++++++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) delete mode 100644 tests/__init__.py diff --git a/CHANGELOG.md b/CHANGELOG.md index e22424a..53b6456 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Makes Organisations into references & populates organisations array (Nodes, Spans) - Add meta output, with output field coverage - Fix bug that meant get_json() could not be called twice + - Include contracts ### Changed diff --git a/libcoveofds/geojson.py b/libcoveofds/geojson.py index 38f33cc..cb5ad26 100644 --- a/libcoveofds/geojson.py +++ b/libcoveofds/geojson.py @@ -178,7 +178,25 @@ def _process_network(self, geojson_feature_node_or_span: dict) -> None: self._networks[network.get("id")]["spans"] = [] self._networks[network.get("id")]["phases"] = {} self._networks[network.get("id")]["organisations"] = {} - self._networks[network.get("id")]["contracts"] = [] + + # Sort references to phases in contracts + if "contracts" in self._networks[network.get("id")] and isinstance( + self._networks[network.get("id")]["contracts"], list + ): + for contract in self._networks[network.get("id")]["contracts"]: + if "relatedPhases" in contract and isinstance( + contract["relatedPhases"], list + ): + out: list = [] + for phase_reference in contract["relatedPhases"]: + phase_id = self._process_phase( + network.get("id"), phase_reference + ) + if phase_id: + out.append({"id": phase_id}) + else: + out.append(phase_reference) + contract["relatedPhases"] = out def _process_node(self, geojson_feature_node: dict) -> None: node = copy.deepcopy(geojson_feature_node.get("properties", {})) @@ -274,7 +292,7 @@ def get_json(self) -> dict: network = copy.deepcopy(network) # Arrays have minItems: 1 set - so if no content, remove the empty array for key in ["nodes", "spans", "phases", "organisations", "contracts"]: - if not network[key]: + if key in network and not network[key]: del network[key] # Sometimes we store these things in dicts - turn to lists for key in ["phases", "organisations"]: diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/fixtures/geojson_to_json/phases_1.expected.json b/tests/fixtures/geojson_to_json/phases_1.expected.json index 2043eb3..08ac39a 100644 --- a/tests/fixtures/geojson_to_json/phases_1.expected.json +++ b/tests/fixtures/geojson_to_json/phases_1.expected.json @@ -3,6 +3,18 @@ { "id": "a096d627-72e1-4f9b-b129-951b1737bff4", "name": "Ghana Fibre Network", + "contracts": [ + { + "id": "1", + "title": "NextGen Phase 1 Construction Contract", + "description": "Contract for the construction of phase 1 of the NextGen network.", + "relatedPhases": [ + { + "id": "1" + } + ] + } + ], "nodes": [ { "id": "1", diff --git a/tests/fixtures/geojson_to_json/phases_1.meta.expected.json b/tests/fixtures/geojson_to_json/phases_1.meta.expected.json index 4520499..2f20880 100644 --- a/tests/fixtures/geojson_to_json/phases_1.meta.expected.json +++ b/tests/fixtures/geojson_to_json/phases_1.meta.expected.json @@ -9,6 +9,24 @@ "/networks/name": { "count": 1 }, + "/networks/contracts": { + "count": 1 + }, + "/networks/contracts/id": { + "count": 1 + }, + "/networks/contracts/title": { + "count": 1 + }, + "/networks/contracts/description": { + "count": 1 + }, + "/networks/contracts/relatedPhases": { + "count": 1 + }, + "/networks/contracts/relatedPhases/id": { + "count": 1 + }, "/networks/nodes": { "count": 1 },