Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Added error identifiers #38

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.errors.add_error(e, instance)

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
Loading