Skip to content

Commit

Permalink
Merge branch 'master' into TP2000-959-measure-list-table-sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenMullally committed Sep 15, 2023
2 parents 5ac2d1f + 791e7b6 commit b469706
Show file tree
Hide file tree
Showing 25 changed files with 1,374 additions and 358 deletions.
25 changes: 24 additions & 1 deletion commodities/import_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from commodities import import_parsers as parsers
from commodities import models
from commodities import serializers
from commodities.models.orm import GoodsNomenclatureDescription
from common.validators import UpdateType
from footnotes.models import Footnote
from importer.handlers import BaseHandler
Expand Down Expand Up @@ -154,14 +155,36 @@ def create_missing_goods_nomenclature_description_period(
TODO : implement correct period handling to correctly resolve this crude workaround
"""
data = dict()

# The code below looks for an existing goods nomenclature description in our db (using the description sid)
# and if it finds one it will use the validity start date from that record to use in the new description.
# If there is no existing description found then it will use todays date as the validity start date.
# Setting to today's date is not perfect, and could cause issues in circumstances where the end date of the
# goods nomenclature is before today, however if that's the case it does not really matter.
# an additional possibility is that there already exists a description with this date, which will cause a rule
# violation, also unlikely but possible.

# Ultimately this is a crude temporary measure that will be superseded by a correct implementation splitting
# out description periods into a new table
data["validity_start"] = date.today()

# goods_nomenclature_description = None if no description exists
goods_nomenclature_description = (
GoodsNomenclatureDescription.objects.filter(sid=self.data["sid"])
.latest_approved()
.first()
)

if (
goods_nomenclature_description
and goods_nomenclature_description.described_goods_nomenclature.valid_between.lower
):
data[
"validity_start"
] = (
goods_nomenclature_description.described_goods_nomenclature.valid_between.lower
)
else:
data["validity_start"] = date.today()

# Update the goods nomenclature description with the start date
goods_nomenclature_description_handler.data.update(data)
Expand Down
74 changes: 74 additions & 0 deletions commodities/tests/test_importer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from datetime import date
from unittest.mock import Mock

import pytest

from commodities import models
from commodities import serializers
from commodities.import_handlers import GoodsNomenclatureDescriptionHandler
from common.tests import factories
from common.validators import UpdateType

Expand All @@ -27,6 +31,76 @@ def test_goods_nomenclature_description_importer(
)


def test_goods_nomenclature_description_update_that_comes_without_a_description_period_but_exists_in_our_db(
imported_fields_match,
date_ranges,
):
goods_nomenclature = factories.GoodsNomenclatureFactory.create(
sid=123,
valid_between=date_ranges.normal,
description=None,
)
goods_nomenclature_description = (
factories.GoodsNomenclatureDescriptionFactory.create(
sid=321,
validity_start=date_ranges.normal.lower,
described_goods_nomenclature=goods_nomenclature,
)
)

goods_nomenclature_description_handler = {
"transaction_id": 1,
"data": {
"sid": 321,
"described_goods_nomenclature": goods_nomenclature,
},
}

nursary = Mock()
handler = GoodsNomenclatureDescriptionHandler(
goods_nomenclature_description_handler,
nursary,
)

handler.create_missing_goods_nomenclature_description_period(handler)

assert (
goods_nomenclature_description_handler["data"]["validity_start"]
== goods_nomenclature_description.validity_start
)


def test_goods_nomenclature_description_update_that_comes_without_a_description_period_but_does_not_exist_in_our_db(
imported_fields_match,
date_ranges,
):
goods_nomenclature = factories.GoodsNomenclatureFactory.create(
sid=123,
valid_between=date_ranges.normal,
description=None,
)

goods_nomenclature_description_handler = {
"transaction_id": 1,
"data": {
"sid": 321,
"described_goods_nomenclature": goods_nomenclature,
},
}

nursary = Mock()
handler = GoodsNomenclatureDescriptionHandler(
goods_nomenclature_description_handler,
nursary,
)

handler.create_missing_goods_nomenclature_description_period(handler)

assert (
goods_nomenclature_description_handler["data"]["validity_start"] == date.today()
)


def test_goods_nomenclature_origin_importer(
update_type,
date_ranges,
Expand Down
10 changes: 9 additions & 1 deletion common/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,13 @@ class HMRCCDSManagerActions(TextChoices):

class CommonUserActions(TextChoices):
SEARCH = "SEARCH", "Search the tariff"
IMPORT = "IMPORT", "Import EU Taric files"
# Change this to be dependent on permissions later


class ImportUserActions(TextChoices):
IMPORT = "IMPORT", "Import EU Taric files"


class HomeForm(forms.Form):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user")
Expand All @@ -272,6 +275,11 @@ def __init__(self, *args, **kwargs):

choices += CommonUserActions.choices

if self.user.has_perm("common.add_trackedmodel") or self.user.has_perm(
"common.change_trackedmodel"
):
choices += ImportUserActions.choices

self.fields["workbasket_action"] = forms.ChoiceField(
label="",
choices=choices,
Expand Down
1 change: 0 additions & 1 deletion common/tariffs_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def parse_response(response):

def get_commodity_data(id):
url = f"{Endpoints.COMMODITIES.value}{id}"
print(url)
return parse_response(requests.get(url))


Expand Down
22 changes: 22 additions & 0 deletions common/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,3 +1404,25 @@ class CrownDependenciesPublishingOperationalStatusFactory(

class Meta:
model = "publishing.CrownDependenciesPublishingOperationalStatus"


class DataUploadFactory(factory.django.DjangoModelFactory):
"""Creates a DataUpload instance."""

class Meta:
model = "workbaskets.DataUpload"

raw_data = factory.Faker("text", max_nb_chars=500)
workbasket = factory.SubFactory(WorkBasketFactory)


class DataRowFactory(factory.django.DjangoModelFactory):
"""Creates a DataRow instance."""

class Meta:
model = "workbaskets.DataRow"

data_upload = factory.SubFactory(DataUploadFactory)
commodity = factory.SubFactory(GoodsNomenclatureFactory)
duty_sentence = factory.Faker("text", max_nb_chars=24)
valid_between = date_ranges("no_end")
Loading

0 comments on commit b469706

Please sign in to comment.