Skip to content

Commit

Permalink
refactor 📦 use error log class to add errors
Browse files Browse the repository at this point in the history
  • Loading branch information
samar-hassan committed Sep 3, 2024
1 parent 94d3863 commit bd9122e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
11 changes: 3 additions & 8 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, set_error_identifiers
from ..utils import ErrorLog, in_bulk
from ..exceptions import OscarOdinException
from .constants import MODEL_IDENTIFIERS_MAPPING

Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
26 changes: 13 additions & 13 deletions oscar_odin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [], []
Expand All @@ -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

0 comments on commit bd9122e

Please sign in to comment.