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

allow option to set product mapper #36

Merged
merged 2 commits into from
Sep 2, 2024
Merged
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
18 changes: 14 additions & 4 deletions oscar_odin/mappings/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ def product_to_resource_with_strategy(
product: Union[ProductModel, Iterable[ProductModel]],
stock_strategy: DefaultStrategy,
include_children: bool = False,
product_mapper: OscarBaseMapping = ProductToResource,
):
"""Map a product model to a resource.

Expand All @@ -342,7 +343,7 @@ def product_to_resource_with_strategy(
:param stock_strategy: The current HTTP request
:param include_children: Include children of parent products.
"""
return ProductToResource.apply(
return product_mapper.apply(
product,
context={
"stock_strategy": stock_strategy,
Expand All @@ -356,6 +357,7 @@ def product_to_resource(
request: Optional[HttpRequest] = None,
user: Optional[AbstractUser] = None,
include_children: bool = False,
product_mapper: OscarBaseMapping = ProductToResource,
**kwargs,
) -> Union[resources.catalogue.Product, Iterable[resources.catalogue.Product]]:
"""Map a product model to a resource.
Expand All @@ -371,17 +373,20 @@ def product_to_resource(
:param include_children: Include children of parent products.
:param kwargs: Additional keyword arguments to pass to the strategy selector.
"""

selector_type = get_class("partner.strategy", "Selector")
stock_strategy = selector_type().strategy(request=request, user=user, **kwargs)

return product_to_resource_with_strategy(product, stock_strategy, include_children)
return product_to_resource_with_strategy(
product, stock_strategy, include_children, product_mapper=product_mapper
)


def product_queryset_to_resources(
queryset: QuerySet,
request: Optional[HttpRequest] = None,
user: Optional[AbstractUser] = None,
include_children: bool = False,
product_mapper=ProductToResource,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do the same here as above product_mapper: Baseblabla = ProductToResource

**kwargs,
) -> Iterable[resources.catalogue.Product]:
"""Map a queryset of product models to a list of resources.
Expand All @@ -401,7 +406,12 @@ def product_queryset_to_resources(
)

return product_to_resource(
query_set, request, user, include_children=include_children, **kwargs
query_set,
request,
user,
include_children,
product_mapper=product_mapper,
Copy link
Contributor

Choose a reason for hiding this comment

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

you can remove this as kwarg now

**kwargs,
)


Expand Down
Loading