From c1f9abdee17aeba3ba8d91e6e2ea22c041af0d17 Mon Sep 17 00:00:00 2001 From: James B Date: Wed, 6 Dec 2023 11:39:35 +0000 Subject: [PATCH] Links to codelists - rewrite to OCDS Standard https://github.com/OpenDataServices/lib-cove-web/issues/74 --- cove_oc4ids/views.py | 8 ++++++++ requirements.txt | 4 +--- requirements_dev.txt | 6 +----- tests/fixtures/codelists.json | 24 ++++++++++++++++++++++++ tests/test_basic.py | 25 +++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 tests/fixtures/codelists.json diff --git a/cove_oc4ids/views.py b/cove_oc4ids/views.py index 2b75059..a078ba5 100644 --- a/cove_oc4ids/views.py +++ b/cove_oc4ids/views.py @@ -1,6 +1,7 @@ import json import logging from decimal import Decimal +import re from cove.views import cove_web_input_error, explore_data_context from django.conf import settings @@ -88,6 +89,13 @@ def explore_oc4ids(request, pk): db_data.rendered = True db_data.save() + for path_string, codelist_info in context["additional_open_codelist_values"].items(): + if codelist_info["codelist_url"].startswith(schema_oc4ids.codelists): + codelist_info["codelist_url"] = ( + "https://standard.open-contracting.org/infrastructure/latest/en/reference/codelists/#" + + re.sub(r"([A-Z])", r"\1", codelist_info["codelist"].split(".")[0]).lower() + ) + template = "cove_oc4ids/explore.html" return render(request, template, context) diff --git a/requirements.txt b/requirements.txt index b179f75..92c2310 100644 --- a/requirements.txt +++ b/requirements.txt @@ -38,8 +38,6 @@ django-bootstrap3==15.0.0 # via # -r requirements.in # libcoveweb -django-environ==0.6.0 - # via libcoveweb et-xmlfile==1.1.0 # via openpyxl flattentool==0.24.0 @@ -70,7 +68,7 @@ libcoveoc4ids[perf]==0.4.2 # via -r requirements.in libcoveocds==0.11.0 # via libcoveoc4ids -libcoveweb==0.26.0 +libcoveweb==0.30.1 # via -r requirements.in lxml==4.9.1 # via flattentool diff --git a/requirements_dev.txt b/requirements_dev.txt index 33ef3c5..b61603b 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -73,10 +73,6 @@ django-bootstrap3==15.0.0 # via # -r requirements.txt # libcoveweb -django-environ==0.6.0 - # via - # -r requirements.txt - # libcoveweb docopt==0.6.2 # via coveralls et-xmlfile==1.1.0 @@ -138,7 +134,7 @@ libcoveocds==0.11.0 # via # -r requirements.txt # libcoveoc4ids -libcoveweb==0.26.0 +libcoveweb==0.30.1 # via -r requirements.txt libsass==0.21.0 # via -r requirements_dev.in diff --git a/tests/fixtures/codelists.json b/tests/fixtures/codelists.json new file mode 100644 index 0000000..3e0bada --- /dev/null +++ b/tests/fixtures/codelists.json @@ -0,0 +1,24 @@ +{ + "version": "0.9", + "uri": "https://standard.open-contracting.org/infrastructure/0.9/en/_static/example.json", + "publishedDate": "2018-12-10T15:53:00Z", + "publisher": { + "name": "Open Data Services Co-operative Limited", + "scheme": "GB-COH", + "uid": "9506232", + "uri": "http://data.companieshouse.gov.uk/doc/company/09506232" + }, + "license": "http://opendatacommons.org/licenses/pddl/1.0/", + "publicationPolicy": "https://standard.open-contracting.org/1.1/en/implementation/publication_policy/", + "projects": [ + { + "id": "proj1", + "status": "Bad news captain", + "sector": [ + "little square buildings", + "pointy roofs" + ], + "type": "shiny and new" + } + ] +} diff --git a/tests/test_basic.py b/tests/test_basic.py index 83cd72c..95bca32 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1,3 +1,4 @@ +import os import pytest from cove.input.models import SuppliedData from django.core.files.base import ContentFile @@ -21,3 +22,27 @@ def test_explore_page(client, json_data): data.current_app = "cove_oc4ids" resp = client.get(data.get_absolute_url()) assert resp.status_code == 200 + + + + +@pytest.mark.django_db +def test_codelist_url_extension_codelists(client): + file_name = os.path.join( + "tests", + "fixtures", + "codelists.json", + ) + with open(os.path.join(file_name)) as fp: + user_data = fp.read() + data = SuppliedData.objects.create() + data.original_file.save("test.json", ContentFile(user_data)) + data.current_app = "cove_oc4ids" + resp = client.get(data.get_absolute_url()) + + assert resp.status_code == 200 + assert len(resp.context["additional_open_codelist_values"]) == 1 + assert ( + resp.context["additional_open_codelist_values"]["projects/sector"]["codelist_url"] + == "https://standard.open-contracting.org/infrastructure/latest/en/reference/codelists/#projectsector" + )