diff --git a/oscar_odin/mappings/context.py b/oscar_odin/mappings/context.py index 0da1c5e..603736d 100644 --- a/oscar_odin/mappings/context.py +++ b/oscar_odin/mappings/context.py @@ -8,7 +8,7 @@ from oscar.core.loading import get_model from oscar.apps.catalogue.product_attributes import QuerysetCache -from ..utils import in_bulk, set_error_identifiers +from ..utils import ErrorLog, in_bulk from ..exceptions import OscarOdinException from .constants import MODEL_IDENTIFIERS_MAPPING @@ -62,7 +62,6 @@ class ModelMapperContext(dict): Model = None errors = None delete_related = False - error_identifiers = None clean_instances = True update_related_models_same_type = True @@ -78,9 +77,8 @@ def __init__( self.fields_to_update = defaultdict(list) self.identifier_mapping = defaultdict(tuple) self.attribute_data = [] - self.errors = [] + self.errors = ErrorLog(identifiers=error_identifiers) self.delete_related = delete_related - self.error_identifiers = error_identifiers self.Model = Model def __bool__(self): @@ -117,10 +115,7 @@ def validate_instances(self, instances, validate_unique=True, fields=None): validate_unique=validate_unique, exclude=exclude ) except ValidationError as e: - # Add details to identify the instance that produced this error - if self.error_identifiers: - set_error_identifiers(e, instance, self.error_identifiers) - self.errors.append(e) + self.errors.add_error(e, instance) else: validated_instances.append(instance) diff --git a/oscar_odin/utils.py b/oscar_odin/utils.py index a3249c3..cfe1d91 100644 --- a/oscar_odin/utils.py +++ b/oscar_odin/utils.py @@ -75,19 +75,21 @@ def querycounter(*labels, print_queries=False): print(" ", q) -def set_error_identifiers(error, record, error_identifiers): - all_identifier_values = [] - for identifier in error_identifiers: - value = "" - if hasattr(record, identifier): - value = getattr(record, identifier) - all_identifier_values.append(str(value)) - error.identifier_values = all_identifier_values - return error +class ErrorLog(list): + def __init__(self, identifiers=None): + self.identifiers = identifiers + + def add_error(self, error, record): + if self.identifiers is not None: + # Add details to identify the instance that produced this error + error.identifier_values = [ + str(getattr(record, identifier, "")) for identifier in self.identifiers + ] + self.append(error) def validate_resources(resources, error_identifiers=None): - errors = [] + errors = ErrorLog(identifiers=error_identifiers) valid_resources = [] if not resources: return [], [] @@ -101,7 +103,5 @@ def validate_resources(resources, error_identifiers=None): resource.full_clean() valid_resources.append(resource) except ValidationError as error: - if error_identifiers is not None: - error = set_error_identifiers(error, resource, error_identifiers) - errors.append(error) + errors.add_error(error, resource) return valid_resources, errors