diff --git a/oscar_odin/mappings/catalogue.py b/oscar_odin/mappings/catalogue.py index b6611ae..4604781 100644 --- a/oscar_odin/mappings/catalogue.py +++ b/oscar_odin/mappings/catalogue.py @@ -15,7 +15,6 @@ from datetime import datetime -from ..utils import validate_resources from .prefetching.prefetch import prefetch_product_queryset from .context import ProductModelMapperContext @@ -38,6 +37,8 @@ StockRecordModel = get_model("partner", "StockRecord") ProductAttributeValueModel = get_model("catalogue", "ProductAttributeValue") +resources_to_db = get_class("oscar_odin.mappings.resources", "resources_to_db") + # mappings ModelMapping = get_class("oscar_odin.mappings.model_mapper", "ModelMapping") map_queryset, OscarBaseMapping = get_classes( @@ -445,21 +446,6 @@ def product_queryset_to_resources( ) -def products_to_model( - products: List[ProductResource], - product_mapper=ProductToModel, - delete_related=False, -) -> Tuple[List[ProductModel], Dict]: - context = ProductModelMapperContext(ProductModel, delete_related=delete_related) - - result = product_mapper.apply(products, context=context) - - try: - return (list(result), context) - except TypeError: # it is not a list - return ([result], context) - - def products_to_db( products, fields_to_update=ALL_CATALOGUE_FIELDS, @@ -474,20 +460,13 @@ def products_to_db( After that all the products will be bulk saved. At last all related models like images, stockrecords, and related_products can will be saved and set on the product. """ - _, errors = validate_resources(products) - if errors: - return [], errors - instances, context = products_to_model( + return resources_to_db( products, - product_mapper=product_mapper, - delete_related=delete_related, - ) - - products, errors = context.bulk_save( - instances, fields_to_update, identifier_mapping, - clean_instances, + model_mapper=product_mapper, + context_mapper=ProductModelMapperContext, + delete_related=delete_related, + clean_instances=clean_instances, ) - return products, errors diff --git a/oscar_odin/mappings/resources.py b/oscar_odin/mappings/resources.py index 8af0216..737c907 100644 --- a/oscar_odin/mappings/resources.py +++ b/oscar_odin/mappings/resources.py @@ -1,5 +1,7 @@ -from oscar_odin.mappings.context import ModelMapperContext -from oscar_odin.utils import validate_resources +from oscar.core.loading import get_class + +ModelMapperContext = get_class("oscar_odin.mappings.context", "ModelMapperContext") +validate_resources = get_class("oscar_odin.utils", "validate_resources") def resources_to_db( @@ -7,6 +9,7 @@ def resources_to_db( fields_to_update, identifier_mapping, model_mapper, + context_mapper=ModelMapperContext, delete_related=False, clean_instances=True, skip_invalid_resources=False, @@ -23,7 +26,7 @@ def resources_to_db( if not skip_invalid_resources and resource_errors: return [], resource_errors - context = ModelMapperContext( + context = context_mapper( model_mapper.to_obj, delete_related=delete_related, error_identifiers=error_identifiers,