From bce4990afd04e0c7c1be4449c9760af7bd3a7e93 Mon Sep 17 00:00:00 2001 From: sniedzielski Date: Wed, 3 Jan 2024 15:38:18 +0100 Subject: [PATCH] CM-378: fixing response output for endpoint with validations --- social_protection/services.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/social_protection/services.py b/social_protection/services.py index fbe6d4c..6bd3f9e 100644 --- a/social_protection/services.py +++ b/social_protection/services.py @@ -148,13 +148,16 @@ def _validate_possible_beneficiaries(self, dataframe: DataFrame, benefit_plan: B validated_dataframe = [] def validate_row(row): - field_validation = {'row': row, 'validations': {}} + field_validation = {'row': row.to_dict(), 'validations': {}} for field, field_properties in properties.items(): if "uniqueness" in field_properties: - field_validation['validations'][f'{field}_uniqueness'] = self._handle_uniqueness(row, field, field_properties, benefit_plan) - + field_validation['validations'][f'{field}_uniqueness'] = self._handle_uniqueness( + row, field, field_properties, benefit_plan, dataframe + ) if "validationCalculation" in field_properties: - field_validation['validations'][f'{field}'] = self._handle_validation_calculation(row, field, field_properties) + field_validation['validations'][f'{field}'] = self._handle_validation_calculation( + row, field, field_properties + ) validated_dataframe.append(field_validation) return row @@ -162,39 +165,32 @@ def validate_row(row): dataframe.apply(validate_row, axis='columns') return validated_dataframe - def _handle_uniqueness(self, row, field, field_properties, benefit_plan): + def _handle_uniqueness(self, row, field, field_properties, benefit_plan, dataframe): unique_class_validation = 'DeduplicationValidationStrategy' - calculation_uuid = SocialProtectionConfig.validation_calculation_uuid - calculation = get_calculation_object(calculation_uuid) - result_row = calculation.calculate_if_active_for_object( unique_class_validation, calculation_uuid, field, row[field], - benefit_plan=benefit_plan.id + benefit_plan=benefit_plan.id, + incoming_data=dataframe ) return result_row def _handle_validation_calculation(self, row, field, field_properties): validation_calculation = field_properties.get("validationCalculation", {}).get("name") - if not validation_calculation: raise ValueError("Missing validation name") - calculation_uuid = SocialProtectionConfig.validation_calculation_uuid - calculation = get_calculation_object(calculation_uuid) - result_row = calculation.calculate_if_active_for_object( validation_calculation, calculation_uuid, field, row[field] ) - return result_row def _create_upload_entry(self, filename):