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

[Refactor] Utilize resources_to_db in products_to_db #49

Merged
merged 2 commits into from
Nov 1, 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
36 changes: 7 additions & 29 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,12 @@ 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
Loading