Skip to content

Commit

Permalink
[FEAT] Added error identifiers (#38)
Browse files Browse the repository at this point in the history
* feat ⭐ add error identifiers

* refactor 📦 use error log class to add errors
  • Loading branch information
samar-hassan authored Sep 3, 2024
1 parent 3347e00 commit 6119c85
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
11 changes: 7 additions & 4 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 ErrorLog, in_bulk
from ..exceptions import OscarOdinException
from .constants import MODEL_IDENTIFIERS_MAPPING

Expand Down Expand Up @@ -61,11 +61,14 @@ class ModelMapperContext(dict):
instance_keys = None
Model = None
errors = None
delete_related = False
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 @@ -74,7 +77,7 @@ def __init__(self, Model, *args, delete_related=False, **kwargs):
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.Model = Model

Expand Down Expand Up @@ -112,7 +115,7 @@ def validate_instances(self, instances, validate_unique=True, fields=None):
validate_unique=validate_unique, exclude=exclude
)
except ValidationError as e:
self.errors.append(e)
self.errors.add_error(e, instance)
else:
validated_instances.append(instance)

Expand Down
19 changes: 16 additions & 3 deletions oscar_odin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,21 @@ def querycounter(*labels, print_queries=False):
print(" ", q)


def validate_resources(resources):
errors = []
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 = ErrorLog(identifiers=error_identifiers)
valid_resources = []
if not resources:
return [], []
Expand All @@ -90,5 +103,5 @@ def validate_resources(resources):
resource.full_clean()
valid_resources.append(resource)
except ValidationError as error:
errors.append(error)
errors.add_error(error, resource)
return valid_resources, errors

0 comments on commit 6119c85

Please sign in to comment.