Skip to content

Commit

Permalink
feat ⭐ utilize resources_to_db in products_to_db
Browse files Browse the repository at this point in the history
  • Loading branch information
samar-hassan committed Nov 1, 2024
1 parent fe7dc0c commit b63a46d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
35 changes: 7 additions & 28 deletions oscar_odin/mappings/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from datetime import datetime

from ..utils import validate_resources
from .prefetching.prefetch import prefetch_product_queryset

from .context import ProductModelMapperContext
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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
9 changes: 6 additions & 3 deletions oscar_odin/mappings/resources.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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(
resources,
fields_to_update,
identifier_mapping,
model_mapper,
context_mapper=ModelMapperContext,
delete_related=False,
clean_instances=True,
skip_invalid_resources=False,
Expand All @@ -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,
Expand Down

0 comments on commit b63a46d

Please sign in to comment.