Skip to content

Commit

Permalink
geojsontojson: Include contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Nov 7, 2022
1 parent 8d1d0ba commit c033723
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 15 additions & 2 deletions libcoveofds/geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,20 @@ 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", {}))
Expand Down Expand Up @@ -274,7 +287,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"]:
Expand Down
Empty file removed tests/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions tests/fixtures/geojson_to_json/phases_1.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/geojson_to_json/phases_1.meta.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down

0 comments on commit c033723

Please sign in to comment.