def clean_duty_sentence(self): duty_sentence = self.cleaned_data["duty_sentence"] valid_between = self.initial.get("valid_between") if duty_sentence and valid_between is not None: validate_duties(duty_sentence, valid_between.lower) return duty_sentence
def conditions_clean(self, cleaned_data, measure_start_date): """ We get the reference_price from cleaned_data and the measure_start_date from the form's initial data. If both are present, we call validate_duties with measure_start_date. Then, if reference_price is provided, we use DutySentenceParser with measure_start_date, if present, or the current_date, to check that we are dealing with a simple duty (i.e. only one component). We then update cleaned_data with key-value pairs created from this single, unsaved component. """ price = cleaned_data.get("reference_price") if price and measure_start_date is not None: validate_duties(price, measure_start_date) if price: parser = DutySentenceParser.get(measure_start_date) components = parser.parse(price) if len(components) > 1: raise ValidationError( "A MeasureCondition cannot be created with a compound reference price (e.g. 3.5% + 11 GBP / 100 kg)", ) cleaned_data["duty_amount"] = components[0].duty_amount cleaned_data["monetary_unit"] = components[0].monetary_unit cleaned_data["condition_measurement"] = components[0].component_measurement # The JS autocomplete does not allow for clearing unnecessary certificates # In case of a user changing data, the information is cleared here. condition_code = cleaned_data.get("condition_code") if condition_code and not condition_code.accepts_certificate: cleaned_data["required_certificate"] = None return cleaned_data
def test_with_reference_price_string_measurement( measurement_kwargs, condition_kwargs, expected, duty_sentence_parser, ): """ Tests that different combinations of duty_amount, monetary_unit, and measurement produce the expect reference_price_string and that this string represents a valid duty sentence. The final two scenarios record the fact that this queryset, unlike ``duty_sentence_string``, does not support supplementary units and these expressions should evaluate to an empty string. """ condition_measurement = factories.MeasurementFactory.create(**measurement_kwargs) condition = factories.MeasureConditionFactory.create( condition_measurement=condition_measurement, **condition_kwargs ) qs = MeasureCondition.objects.with_reference_price_string() price_condition = qs.first() assert price_condition.reference_price_string == expected validate_duties( price_condition.reference_price_string, condition.dependent_measure.valid_between.lower, )
def test_duties_validator( duties, error_expected, date_ranges, duty_sentence_parser, ): # duty_sentence_parser populates data needed by the DutySentenceParser # removing it will cause the test to fail. with raises_if(ValidationError, error_expected): validate_duties(duties, date_ranges.normal)
def clean_applicable_duty(self): """ Gets applicable_duty from cleaned data. We expect `measure_start_date` to be passed in. Uses `DutySentenceParser` to check that applicable_duty is a valid duty string. """ applicable_duty = self.cleaned_data["applicable_duty"] if applicable_duty and self.measure_start_date is not None: validate_duties(applicable_duty, self.measure_start_date) return applicable_duty
def clean_applicable_duty(self): """ Gets applicable_duty from cleaned data. We get start date from other data in the measure edit form. Uses `DutySentenceParser` to check that applicable_duty is a valid duty string. """ applicable_duty = self.cleaned_data["applicable_duty"] if applicable_duty and self.get_start_date(self.data) is not None: validate_duties(applicable_duty, self.get_start_date(self.data)) return applicable_duty
def test_with_reference_price_string_no_measurement( create_kwargs, expected, duty_sentence_parser, ): """Tests that different combinations of duty_amount and monetary_unit produce the expect reference_price_string and that this string represents a valid duty sentence.""" condition = factories.MeasureConditionFactory.create(**create_kwargs) qs = MeasureCondition.objects.with_reference_price_string() price_condition = qs.first() assert price_condition.reference_price_string == expected validate_duties( price_condition.reference_price_string, condition.dependent_measure.valid_between.lower, )
def clean(self): cleaned_data = super().clean() duties = cleaned_data.get("duties", "") validate_duties(duties, self.measure_start_date) return cleaned_data