diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ce7374a..47130e02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ ## Pre-release +### Implemented enhancements +- XOT-1275 - Add MADB link field to country guide + +### Fixed bugs +No ticket - Fix error on opportunity page + +## [2020.01.31](https://github.com/uktrade/directory-cms/releases/tag/2020.01.31) +[Full Changelog](https://github.com/uktrade/directory-cms/compare/2020.01.29...2020.01.31) + ### Implemented enhancements - XOT-1285 madb content markdown field diff --git a/export_readiness/migrations/0073_auto_20200203_1333.py b/export_readiness/migrations/0073_auto_20200203_1333.py new file mode 100644 index 00000000..1ef8bd0c --- /dev/null +++ b/export_readiness/migrations/0073_auto_20200203_1333.py @@ -0,0 +1,52 @@ +# Generated by Django 2.2.6 on 2020-02-03 13:33 + +import logging + + +import core.model_fields +from django.db import migrations, models + +import pycountry + + +logger = logging.getLogger(__name__) + + +def set_duties_and_custom_procedures_cta_link(apps, schema_editor): + url = 'https://www.check-duties-customs-exporting-goods.service.gov.uk/searchproduct?d=' + CountryGuidePage = apps.get_model('export_readiness', 'CountryGuidePage') + for guide in CountryGuidePage.objects.all(): + try: + matches = pycountry.countries.search_fuzzy(guide.country.name) + except LookupError: + logger.warn(f'no country match for {guide.country.name}') + except AttributeError: + logger.warn(f'skipping {guide.title}') + else: + guide.duties_and_custom_procedures_cta_link = f'{url}{matches[0].alpha_2}' + guide.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('export_readiness', '0072_auto_20191212_1403'), + ] + + operations = [ + migrations.RemoveField( + model_name='countryguidepage', + name='help_market_guide_cta_link', + ), + migrations.AddField( + model_name='countryguidepage', + name='duties_and_custom_procedures_cta_link', + field=models.URLField(blank=True, null=True, verbose_name='Check duties and customs procedures for exporting goods'), + ), + migrations.AlterField( + model_name='homepage', + name='madb_content', + field=core.model_fields.MarkdownField(blank=True, null=True, verbose_name='Content'), + ), + migrations.RunPython(set_duties_and_custom_procedures_cta_link, reverse_code=migrations.RunPython.noop) + ] diff --git a/export_readiness/models.py b/export_readiness/models.py index 13ac2da9..202a387e 100644 --- a/export_readiness/models.py +++ b/export_readiness/models.py @@ -1020,8 +1020,9 @@ class Meta: ) # need help - help_market_guide_cta_link = models.CharField( - max_length=255, blank=True, verbose_name='GOV.UK country guide URL') + duties_and_custom_procedures_cta_link = models.URLField( + blank=True, null=True, verbose_name='Check duties and customs procedures for exporting goods' + ) # related pages related_page_one = models.ForeignKey( diff --git a/export_readiness/panels.py b/export_readiness/panels.py index 15bc0267..aa9dc0e6 100644 --- a/export_readiness/panels.py +++ b/export_readiness/panels.py @@ -889,7 +889,7 @@ class CountryGuidePagePanels: heading='Need help', classname='collapsible', children=[ - FieldPanel('help_market_guide_cta_link') + FieldPanel('duties_and_custom_procedures_cta_link') ] ), MultiFieldPanel( diff --git a/export_readiness/serializers.py b/export_readiness/serializers.py index 860d1454..0fafd55f 100644 --- a/export_readiness/serializers.py +++ b/export_readiness/serializers.py @@ -419,7 +419,7 @@ class CountryGuidePageSerializer(PageWithRelatedPagesSerializer, HeroSerializer) accordions = serializers.SerializerMethodField() fact_sheet = serializers.SerializerMethodField() - help_market_guide_cta_link = serializers.CharField(max_length=255) + duties_and_custom_procedures_cta_link = serializers.CharField(max_length=255) tags = core_fields.TagsListField() region = serializers.CharField(allow_null=True, source='country.region.name') diff --git a/great_international/models/capital_invest.py b/great_international/models/capital_invest.py index b04f4309..e8821253 100644 --- a/great_international/models/capital_invest.py +++ b/great_international/models/capital_invest.py @@ -544,9 +544,7 @@ class CapitalInvestRelatedSubSectors(Orderable, RelatedSubSector): ) -class CapitalInvestOpportunityPage( - panels.CapitalInvestOpportunityPagePanels, BaseInternationalPage -): +class CapitalInvestOpportunityPage(panels.CapitalInvestOpportunityPagePanels, BaseInternationalPage): parent_page_types = [ 'great_international.CapitalInvestOpportunityListingPage' diff --git a/great_international/serializers.py b/great_international/serializers.py index c4c03505..cf941cab 100644 --- a/great_international/serializers.py +++ b/great_international/serializers.py @@ -1,3 +1,5 @@ +import random + from directory_constants import cms from rest_framework import serializers from wagtail.images.api import fields as wagtail_fields @@ -1637,23 +1639,38 @@ def get_sub_sectors(self, instance): related_opportunities = serializers.SerializerMethodField() def get_related_opportunities(self, instance): - random_sector = instance.related_sectors.order_by('?').first() + related_sectors_ids = instance.related_sectors.values_list('related_sector_id', flat=True) + + if not related_sectors_ids: + return [] - if random_sector: + random_sector_id = random.choice(related_sectors_ids) - three_random_opps = CapitalInvestOpportunityPage.objects.filter( - related_sectors__related_sector_id=random_sector.related_sector_id - ).order_by('?').exclude(id=instance.id)[:3] + related_opps_ids = CapitalInvestOpportunityPage.objects.filter( + related_sectors__related_sector_id=random_sector_id + ).exclude(id=instance.id).values_list('pk', flat=True) - serializer = RelatedCapitalInvestOpportunityPageSerializer( - three_random_opps, - many=True, - allow_null=True, - context=self.context - ) - return serializer.data + if not related_opps_ids: + return [] + + elif len(related_opps_ids) > 3: + random_ids = random.sample(list(related_opps_ids), 3) + + else: + random_ids = related_opps_ids - return [] + related_opps = [] + + for id in random_ids: + related_opps.append(CapitalInvestOpportunityPage.objects.get(pk=id)) + + serializer = RelatedCapitalInvestOpportunityPageSerializer( + related_opps, + many=True, + allow_null=True, + context=self.context + ) + return serializer.data class MinimalPageSerializer(BasePageSerializer): diff --git a/requirements.in b/requirements.in index 17d69b98..e9c30bb1 100644 --- a/requirements.in +++ b/requirements.in @@ -33,3 +33,4 @@ w3lib>=1.19.0<2.0.0 django-admin-ip-restrictor==2.1.0 notifications-python-client==5.3.* pillow>=6.2.0 # for security fix. check compatibility on next wagtail upgrade +pycountry==19.8.18 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 1837b9f9..922209dd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -56,6 +56,7 @@ notifications-python-client==5.3.0 oauthlib==3.1.0 # via requests-oauthlib pillow==6.2.1 psycopg2==2.7.3.2 +pycountry==19.8.18 pyjwt==1.7.1 # via notifications-python-client pyrsistent==0.15.6 # via jsonschema python-dateutil==2.6.1 # via botocore diff --git a/requirements_test.txt b/requirements_test.txt index ac1c7cb2..0f29550f 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -71,6 +71,7 @@ pluggy==0.12.0 # via pytest psycopg2==2.7.3.2 py==1.7.0 # via pytest pycodestyle==2.4.0 # via flake8 +pycountry==19.8.18 pyflakes==2.0.0 # via flake8 pyjwt==1.7.1 # via notifications-python-client pyparsing==2.3.0 # via packaging diff --git a/tests/export_readiness/factories.py b/tests/export_readiness/factories.py index 1d4cfdd8..68902149 100644 --- a/tests/export_readiness/factories.py +++ b/tests/export_readiness/factories.py @@ -326,7 +326,7 @@ class Meta: fact_sheet_column_2_teaser = factory.fuzzy.FuzzyText(length=10) fact_sheet_column_2_body = factory.fuzzy.FuzzyText(length=10) - help_market_guide_cta_link = factory.fuzzy.FuzzyText(length=10) + duties_and_custom_procedures_cta_link = 'http://www.example.com' related_page_one = factory.SubFactory(ArticlePageFactory) related_page_two = factory.SubFactory(CampaignPageFactory) diff --git a/tests/great_international/factories.py b/tests/great_international/factories.py index 6318ed58..c717d7ca 100644 --- a/tests/great_international/factories.py +++ b/tests/great_international/factories.py @@ -412,6 +412,14 @@ class Meta: parent = None +class CapitalInvestRelatedSectorsFactory(factory.django.DjangoModelFactory): + class Meta: + model = models.capital_invest.CapitalInvestRelatedSectors + + page = None + related_sector = None + + class CapitalInvestOpportunityListingPageFactory( wagtail_factories.PageFactory ): diff --git a/tests/great_international/test_serializers.py b/tests/great_international/test_serializers.py index 91e227f9..fe9cf38c 100644 --- a/tests/great_international/test_serializers.py +++ b/tests/great_international/test_serializers.py @@ -34,13 +34,13 @@ AboutUkLandingPageFactory, InvestInternationalHomePageFactory, CapitalInvestRegionPageFactory, AboutUkRegionListingPageFactory, InvestRegionPageFactory, - InternationalTradeHomePageFactory) + InternationalTradeHomePageFactory, CapitalInvestRelatedSectorsFactory +) from great_international.models.capital_invest import ( CapitalInvestRelatedRegions, CapitalInvestHomesInEnglandCardFieldsSummary, CapitalInvestRegionCardFieldsSummary, - CapitalInvestRelatedSectors, CapitalInvestRelatedSubSectors ) from great_international.models.great_international import ( @@ -511,8 +511,7 @@ def test_capital_invest_landing_page_has_how_we_help( @pytest.mark.django_db -def test_opportunity_page_can_add_sector_as_related(rf, - international_root_page): +def test_opportunity_page_can_add_sector_as_related(rf, international_root_page): guide_landing_page = InternationalGuideLandingPageFactory( parent=international_root_page, @@ -524,15 +523,13 @@ def test_opportunity_page_can_add_sector_as_related(rf, slug='sector' ) - related_sector = CapitalInvestRelatedSectors( - related_sector=sector - ) - opportunity = CapitalInvestOpportunityPageFactory( parent=international_root_page, slug='opp', - related_sectors=[related_sector] + related_sectors=[] ) + related_sector = CapitalInvestRelatedSectorsFactory(related_sector=sector, page=opportunity) + opportunity.related_sector = related_sector opportunity_serializer = CapitalInvestOpportunityPageSerializer( instance=opportunity, @@ -558,15 +555,13 @@ def test_international_sector_page_gets_opps_with_sector_as_related( slug='sector' ) - related_sector = CapitalInvestRelatedSectors( - related_sector=sector - ) - opportunity = CapitalInvestOpportunityPageFactory( parent=international_root_page, slug='opp', - related_sectors=[related_sector] + related_sectors=[] ) + related_sector = CapitalInvestRelatedSectorsFactory(related_sector=sector, page=opportunity) + opportunity.related_sector = related_sector opportunity_serializer = CapitalInvestOpportunityPageSerializer( instance=opportunity, @@ -588,13 +583,13 @@ def test_international_sector_page_gets_opps_with_sector_as_related( @pytest.mark.django_db def test_opp_page_null_case_related_sector(rf, international_root_page): - related_sector = CapitalInvestRelatedSectors() - opportunity = CapitalInvestOpportunityPageFactory( parent=international_root_page, slug='opp', - related_sectors=[related_sector] + related_sectors=[] ) + related_sector = CapitalInvestRelatedSectorsFactory(page=opportunity) + opportunity.related_sector = related_sector opportunity_serializer = CapitalInvestOpportunityPageSerializer( instance=opportunity, @@ -606,9 +601,46 @@ def test_opp_page_null_case_related_sector(rf, international_root_page): @pytest.mark.django_db -def test_opp_page_null_case_related_sector2( - rf, international_root_page -): +def test_opp_page_related_random_opps(rf, international_root_page): + + guide_landing_page = InternationalGuideLandingPageFactory( + parent=international_root_page, + slug='page-slug', + ) + + sector = InternationalSectorPageFactory( + parent=guide_landing_page, + slug='sector' + ) + + opportunity = CapitalInvestOpportunityPageFactory( + parent=international_root_page, + slug='opp', + related_sectors=[] + ) + + related_sector = CapitalInvestRelatedSectorsFactory(related_sector=sector, page=opportunity) + opportunity.related_sectors = [related_sector] + + for i in range(3): + opp = CapitalInvestOpportunityPageFactory( + parent=international_root_page, + slug=f'opp{i}', + related_sectors=[related_sector] + ) + related_sector = CapitalInvestRelatedSectorsFactory(related_sector=sector, page=opp) + opp.related_sectors = [related_sector] + + serializer = CapitalInvestOpportunityPageSerializer( + instance=opportunity, + context={'request': rf.get('/')} + ) + + assert len(serializer.data['related_opportunities']) == 3 + + +@pytest.mark.django_db +def test_opp_page_null_case_related_sector2(rf, international_root_page): opportunity = CapitalInvestOpportunityPageFactory( parent=international_root_page, @@ -644,15 +676,13 @@ def test_international_sector_opportunity_null_case( slug='sectorB' ) - related_sector = CapitalInvestRelatedSectors( - related_sector=sector_a - ) - opportunity = CapitalInvestOpportunityPageFactory( parent=international_root_page, slug='opp', - related_sectors=[related_sector] + related_sectors=[] ) + related_sector = CapitalInvestRelatedSectorsFactory(related_sector=sector_a, page=opportunity) + opportunity.related_sector = related_sector opportunity_serializer = CapitalInvestOpportunityPageSerializer( instance=opportunity, @@ -685,12 +715,10 @@ def test_international_sector_opportunity_null_case2( slug='sector' ) - related_sector = CapitalInvestRelatedSectors() - opportunity = CapitalInvestOpportunityPageFactory( parent=international_root_page, slug='opp', - related_sectors=[related_sector] + related_sectors=[] ) opportunity_serializer = CapitalInvestOpportunityPageSerializer( @@ -953,30 +981,30 @@ def test_opportunity_page_gets_opportunities_with_same_sector(rf, international_ title='sector_title' ) - CapitalInvestOpportunityPageFactory( + ashton_green = CapitalInvestOpportunityPageFactory( parent=international_root_page, slug='ashton-green', title_en_gb='Ashton Green', - related_sectors=[ - CapitalInvestRelatedSectors(related_sector=sector) - ] + related_sectors=[] ) + related_sector = CapitalInvestRelatedSectorsFactory(related_sector=sector, page=ashton_green) + ashton_green.related_sector = related_sector - birmingham_opportuntiy = CapitalInvestOpportunityPageFactory( + birmingham = CapitalInvestOpportunityPageFactory( parent=international_root_page, slug='birimingham-curzon', title_en_gb='Birmingham Curzon', - related_sectors=[ - CapitalInvestRelatedSectors(related_sector=sector) - ] + related_sectors=[] ) + related_sector = CapitalInvestRelatedSectorsFactory(related_sector=sector, page=birmingham) + ashton_green.related_sector = related_sector - birmingham_serializer = CapitalInvestOpportunityPageSerializer( - instance=birmingham_opportuntiy, + serializer = CapitalInvestOpportunityPageSerializer( + instance=birmingham, context={'request': rf.get('/')} ) - related_opps = birmingham_serializer.data['related_opportunities'] + related_opps = serializer.data['related_opportunities'] assert len(related_opps) == 1 @@ -996,7 +1024,7 @@ def test_opportunity_page_null_case_gets_opportunities_with_same_sector(rf, inte title='sector_title' ) - related_sector = CapitalInvestRelatedSectors(related_sector=sector) + related_sector = CapitalInvestRelatedSectorsFactory(related_sector=sector) opportunity = CapitalInvestOpportunityPageFactory( parent=international_root_page,