Skip to content

Commit

Permalink
Merge pull request #3047 from uktrade/ET-65-market-standardisation
Browse files Browse the repository at this point in the history
ET-65 update country choices to use directory-api data
  • Loading branch information
lewis-coulson-dit authored May 7, 2024
2 parents eb82d35 + 887d748 commit 201e617
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 23 deletions.
3 changes: 2 additions & 1 deletion contact/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
},
Expand Down
19 changes: 19 additions & 0 deletions core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -496,6 +497,24 @@ def get_country_data(countries, fields):
return response.json()


def get_markets_list():
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()
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(
Expand Down
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 14 additions & 15 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -746,7 +746,6 @@ tomli==2.0.1
# ipdb
# pip-tools
# pylint
# pyproject-hooks
# pytest
tomlkit==0.12.4
# via pylint
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/core/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 201e617

Please sign in to comment.