From e920813972263df3d296db3ccce10e0876c9e7d8 Mon Sep 17 00:00:00 2001 From: Tom Endacott Date: Thu, 2 May 2024 08:53:42 +0100 Subject: [PATCH 1/2] ET-65 update country choices to use directory-api data --- contact/forms.py | 3 ++- core/helpers.py | 16 ++++++++++++ requirements.in | 2 +- requirements.txt | 12 ++++----- requirements_test.txt | 29 ++++++++++----------- tests/unit/core/test_helpers.py | 45 +++++++++++++++++++++++++++++++++ 6 files changed, 84 insertions(+), 23 deletions(-) diff --git a/contact/forms.py b/contact/forms.py index f2e76a6803..e219b4add5 100644 --- a/contact/forms.py +++ b/contact/forms.py @@ -19,6 +19,7 @@ import regex from contact import constants, mixins as contact_mixins, widgets as contact_widgets from contact.helpers import get_free_trade_agreements, retrieve_regional_office +from core import helpers from core.forms import TERMS_LABEL, ConsentFieldMixin from core.validators import is_valid_uk_postcode from directory_constants import choices @@ -317,7 +318,7 @@ class DomesticExportSupportStep5Form(forms.Form): markets = forms.MultipleChoiceField( label='Select all markets that apply', widget=contact_widgets.GreatCheckboxes, - choices=COUNTRY_CHOICES + [('notspecificcountry', 'My query is not related to a specific country')], + choices=helpers.get_markets_list() + [('notspecificcountry', 'My query is not related to a specific country')], error_messages={ 'required': 'Enter a market', }, diff --git a/core/helpers.py b/core/helpers.py index 0b76b44165..a48e17071f 100644 --- a/core/helpers.py +++ b/core/helpers.py @@ -8,6 +8,7 @@ from difflib import SequenceMatcher from io import StringIO from logging import getLogger +from operator import itemgetter import boto3 import great_components.helpers @@ -496,6 +497,21 @@ def get_country_data(countries, fields): return response.json() +def get_markets_list(): + response = api_client.dataservices.get_markets_data() + if not response.ok: + return choices.COUNTRY_CHOICES + json_data = response.json() + if len(json_data) == 0: + return choices.COUNTRY_CHOICES + market_list = [] + for market in json_data: + if market['enabled']: + market_list.append((market['iso2_code'], market['name'])) + market_list.sort(key=itemgetter(1)) + return market_list + + def build_social_link(template, request, title): text_to_encode = 'Export Readiness - ' + title + ' ' return template.format( diff --git a/requirements.in b/requirements.in index 6e7742f267..a740f6dda0 100644 --- a/requirements.in +++ b/requirements.in @@ -35,7 +35,7 @@ sentry-sdk==1.14.* # DIT packages # ------------ great-components==2.6.4 -directory-api-client==26.4.7 +directory-api-client==26.6.1 directory-ch-client==4.0.2 directory-client-core==7.2.13 directory-components==40.2.3 diff --git a/requirements.txt b/requirements.txt index bf66f69050..5dbd8d50a7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -91,7 +91,7 @@ dateparser==0.7.2 # via -r requirements.in defusedxml==0.7.1 # via willow -directory-api-client==26.4.7 +directory-api-client==26.6.1 # via -r requirements.in directory-ch-client==4.0.2 # via -r requirements.in @@ -216,7 +216,7 @@ factory-boy==3.3.0 # via # -r requirements.in # wagtail-factories -faker==24.11.0 +faker==25.0.0 # via factory-boy filetype==1.2.0 # via willow @@ -314,7 +314,7 @@ pycparser==2.22 # via cffi pygments==2.17.2 # via sphinx -pyhanko==0.23.2 +pyhanko==0.24.0 # via xhtml2pdf pyhanko-certvalidator==0.26.3 # via @@ -363,11 +363,11 @@ qrcode==7.4.2 # via pyhanko readtime==1.1.1 # via -r requirements.in -redis==5.0.3 +redis==5.0.4 # via # celery # django-redis -regex==2024.4.16 +regex==2024.4.28 # via dateparser reportlab==4.0.9 # via @@ -427,7 +427,7 @@ tablib==3.5.0 # via -r requirements.in telepath==0.3.1 # via wagtail -tinycss2==1.2.1 +tinycss2==1.3.0 # via # cssselect2 # svglib diff --git a/requirements_test.txt b/requirements_test.txt index 025475bc99..a7250ff056 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -58,7 +58,7 @@ black==24.3.0 # blacken-docs blacken-docs==1.6.0 # via -r requirements_test.in -blinker==1.7.0 +blinker==1.8.1 # via flask boto3==1.24.96 # via @@ -117,7 +117,7 @@ colorama==0.4.6 # via djlint configargparse==1.7 # via locust -coverage[toml]==7.4.4 +coverage[toml]==7.5.0 # via # pytest-codecov # pytest-cov @@ -146,7 +146,7 @@ defusedxml==0.7.1 # via willow dill==0.3.8 # via pylint -directory-api-client==26.4.7 +directory-api-client==26.6.1 # via -r requirements.in directory-ch-client==4.0.2 # via -r requirements.in @@ -292,9 +292,9 @@ factory-boy==3.3.0 # via # -r requirements.in # wagtail-factories -faker==24.11.0 +faker==25.0.0 # via factory-boy -filelock==3.13.4 +filelock==3.14.0 # via virtualenv filetype==1.2.0 # via willow @@ -497,7 +497,7 @@ pillow-heif==0.16.0 # via willow pip-tools==7.4.1 # via -r requirements_test.in -platformdirs==4.2.0 +platformdirs==4.2.1 # via # black # pylint @@ -540,7 +540,7 @@ pygments==2.17.2 # via # ipython # sphinx -pyhanko==0.23.2 +pyhanko==0.24.0 # via xhtml2pdf pyhanko-certvalidator==0.26.3 # via @@ -558,7 +558,7 @@ pypdf==4.2.0 # via xhtml2pdf pypng==0.20220715.0 # via qrcode -pyproject-hooks==1.0.0 +pyproject-hooks==1.1.0 # via # build # pip-tools @@ -568,7 +568,7 @@ pyrsistent==0.20.0 # via jsonschema pysocks==1.7.1 # via urllib3 -pytest==8.1.1 +pytest==8.2.0 # via # -r requirements_test.in # allure-pytest @@ -587,7 +587,7 @@ pytest-django==4.8.0 # via -r requirements_test.in pytest-sugar==1.0.0 # via -r requirements_test.in -pytest-xdist==3.5.0 +pytest-xdist==3.6.1 # via -r requirements_test.in python-bidi==0.4.2 # via xhtml2pdf @@ -632,7 +632,7 @@ qrcode==7.4.2 # via pyhanko readtime==1.1.1 # via -r requirements.in -redis==5.0.3 +redis==5.0.4 # via # celery # django-redis @@ -676,7 +676,7 @@ ruamel-yaml-clib==0.2.8 # via ruamel-yaml s3transfer==0.6.2 # via boto3 -selenium==4.19.0 +selenium==4.20.0 # via -r requirements_test.in sentry-sdk==1.14.0 # via -r requirements.in @@ -731,7 +731,7 @@ telepath==0.3.1 # via wagtail termcolor==2.4.0 # via pytest-sugar -tinycss2==1.2.1 +tinycss2==1.3.0 # via # cssselect2 # svglib @@ -746,7 +746,6 @@ tomli==2.0.1 # ipdb # pip-tools # pylint - # pyproject-hooks # pytest tomlkit==0.12.4 # via pylint @@ -803,7 +802,7 @@ vine==5.1.0 # amqp # celery # kombu -virtualenv==20.25.3 +virtualenv==20.26.1 # via pre-commit w3lib==1.22.0 # via directory-client-core diff --git a/tests/unit/core/test_helpers.py b/tests/unit/core/test_helpers.py index 3e1d901f3f..b34257f5bc 100644 --- a/tests/unit/core/test_helpers.py +++ b/tests/unit/core/test_helpers.py @@ -572,6 +572,51 @@ def test_get_country_data(mock_country_data, client): assert response.get('FR') == country_data['FR'] +@mock.patch.object(api_client.dataservices, 'get_markets_data') +@pytest.mark.django_db +def test_get_markets_list(mock_markets_data, client): + markets_data = [ + { + 'reference_id': 'CTHMTC00001', + 'name': 'Abu Dhabi', + 'type': 'Territory', + 'iso1_code': None, + 'iso2_code': 'AE-AZ', + 'iso3_code': None, + 'overseas_region_overseas_region_name': 'Middle East, Afghanistan and Pakistan', + 'start_date': None, + 'end_date': None, + 'enabled': False, + }, + { + 'reference_id': 'CTHMTC00002', + 'name': 'Afghanistan', + 'type': 'Country', + 'iso1_code': '004', + 'iso2_code': 'AF', + 'iso3_code': 'AFG', + 'overseas_region_overseas_region_name': 'Middle East, Afghanistan and Pakistan', + 'start_date': None, + 'end_date': None, + 'enabled': True, + }, + ] + # response not ok + mock_markets_data.return_value = create_response(status_code=404, json_body=markets_data) + response = helpers.get_markets_list() + assert len(response) > 1 + # no json in response + mock_markets_data.return_value = create_response(status_code=200) + response = helpers.get_markets_list() + assert len(response) > 1 + # response ok and json in response + mock_markets_data.return_value = create_response(status_code=200, json_body=markets_data) + response = helpers.get_markets_list() + assert len(response) == 1 + assert response[0][0] == 'AF' + assert response[0][1] == 'Afghanistan' + + def test_build_twitter_link(rf): actual = helpers.build_twitter_link( request=rf.get( From b0bd0ebf74404a115ba593fc324df353b5104ed0 Mon Sep 17 00:00:00 2001 From: Tom Endacott Date: Fri, 3 May 2024 11:23:51 +0100 Subject: [PATCH 2/2] ET-65 update helper to fix failed connection error --- core/helpers.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/helpers.py b/core/helpers.py index a48e17071f..21c5d991e1 100644 --- a/core/helpers.py +++ b/core/helpers.py @@ -498,7 +498,10 @@ def get_country_data(countries, fields): def get_markets_list(): - response = api_client.dataservices.get_markets_data() + try: + response = api_client.dataservices.get_markets_data() + except Exception: + return choices.COUNTRY_CHOICES if not response.ok: return choices.COUNTRY_CHOICES json_data = response.json()