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 1 commit
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
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
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 = ErrorLog(self)

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:
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)

# 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):
Copy link
Member

Choose a reason for hiding this comment

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

class ErrorLog(list):
    def __init__(self, context):
        self.context = context

    def add_error(self, exception, instance):
        self.append(self.compute_error_identifier(exception, instance)

    def register_error_type(self, cls):
        # some way of registering a error indentifier computation function

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
Loading