Skip to content

Commit

Permalink
feat ⭐ add error identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
samar-hassan committed Sep 3, 2024
1 parent 3347e00 commit 94d3863
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 10 additions & 2 deletions oscar_odin/mappings/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from oscar.core.loading import get_model
from oscar.apps.catalogue.product_attributes import QuerysetCache

from ..utils import in_bulk
from ..utils import in_bulk, set_error_identifiers
from ..exceptions import OscarOdinException
from .constants import MODEL_IDENTIFIERS_MAPPING

Expand Down Expand Up @@ -61,11 +61,15 @@ class ModelMapperContext(dict):
instance_keys = None
Model = None
errors = None
delete_related = False
error_identifiers = None
clean_instances = True

update_related_models_same_type = True

def __init__(self, Model, *args, delete_related=False, **kwargs):
def __init__(
self, Model, *args, delete_related=False, error_identifiers=None, **kwargs
):
super().__init__(*args, **kwargs)
self.foreign_key_items = defaultdict(list)
self.many_to_many_items = defaultdict(list)
Expand All @@ -76,6 +80,7 @@ def __init__(self, Model, *args, delete_related=False, **kwargs):
self.attribute_data = []
self.errors = []
self.delete_related = delete_related
self.error_identifiers = error_identifiers
self.Model = Model

def __bool__(self):
Expand Down Expand Up @@ -112,6 +117,9 @@ 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)
else:
validated_instances.append(instance)
Expand Down
15 changes: 14 additions & 1 deletion oscar_odin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,18 @@ def querycounter(*labels, print_queries=False):
print(" ", q)


def validate_resources(resources):
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


def validate_resources(resources, error_identifiers=None):
errors = []
valid_resources = []
if not resources:
Expand All @@ -90,5 +101,7 @@ def validate_resources(resources):
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)
return valid_resources, errors

0 comments on commit 94d3863

Please sign in to comment.