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

Never register mappings please #18

Merged
merged 1 commit into from
Feb 22, 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
6 changes: 5 additions & 1 deletion oscar_odin/mappings/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import odin
from django.db.models import QuerySet
from odin.mapping import ImmediateResult
from odin.mapping import ImmediateResult, MappingBase, MappingMeta


def map_queryset(
Expand All @@ -29,3 +29,7 @@ def map_queryset(
return list(
mapping.apply(queryset.all(), context=context, mapping_result=ImmediateResult)
)


class OscarBaseMapping(MappingBase, metaclass=MappingMeta):
register_mapping = False
2 changes: 2 additions & 0 deletions oscar_odin/mappings/_model_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class ModelMapping(MappingBase, metaclass=ModelMappingMeta):
many_to_many_fields: Sequence[ManyToManyRel] = []
foreign_key_fields: Sequence[ForeignKey] = []

register_mapping = False

def create_object(self, **field_values):
"""Create a new product model."""
related_field_values = self.get_related_field_values(field_values)
Expand Down
7 changes: 4 additions & 3 deletions oscar_odin/mappings/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from oscar.core.loading import get_model

from .. import resources
from ._common import OscarBaseMapping

__all__ = (
"BillingAddressToResource",
Expand All @@ -14,14 +15,14 @@
CountryModel = get_model("address", "Country")


class CountryToResource(odin.Mapping):
class CountryToResource(OscarBaseMapping):
"""Mapping from country model to resource."""

from_obj = CountryModel
to_obj = resources.address.Country


class BillingAddressToResource(odin.Mapping):
class BillingAddressToResource(OscarBaseMapping):
"""Mapping from billing address model to resource."""

from_obj = BillingAddressModel
Expand All @@ -33,7 +34,7 @@ def country(self) -> resources.address.Country:
return CountryToResource.apply(self.source.country)


class ShippingAddressToResource(odin.Mapping):
class ShippingAddressToResource(OscarBaseMapping):
"""Mapping from shipping address model to resource."""

from_obj = ShippingAddressModel
Expand Down
4 changes: 2 additions & 2 deletions oscar_odin/mappings/auth.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Mappings between odin and django auth models."""
import odin
from oscar.core.loading import get_model

from .. import resources
from ._common import OscarBaseMapping

__all__ = ("UserToResource",)

UserModel = get_model("auth", "User")


class UserToResource(odin.Mapping):
class UserToResource(OscarBaseMapping):
"""Mapping from user model to resource."""

from_obj = UserModel
Expand Down
18 changes: 9 additions & 9 deletions oscar_odin/mappings/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from .. import resources
from ..resources.catalogue import Structure
from ._common import map_queryset
from ._common import map_queryset, OscarBaseMapping
from ._model_mapper import ModelMapping

from .context import ProductModelMapperContext
Expand All @@ -40,7 +40,7 @@
ProductAttributeValueModel = get_model("catalogue", "ProductAttributeValue")


class ProductImageToResource(odin.Mapping):
class ProductImageToResource(OscarBaseMapping):
"""Map from an image model to a resource."""

from_obj = ProductImageModel
Expand All @@ -53,7 +53,7 @@ def original(self, value: ImageFieldFile) -> str:
return value.url


class ProductImageToModel(odin.Mapping):
class ProductImageToModel(OscarBaseMapping):
"""Map from an image resource to a model."""

from_obj = resources.catalogue.Image
Expand All @@ -67,7 +67,7 @@ def date_created(self, value: datetime) -> datetime:
return datetime.now()


class CategoryToResource(odin.Mapping):
class CategoryToResource(OscarBaseMapping):
"""Map from a category model to a resource."""

from_obj = CategoryModel
Expand All @@ -86,7 +86,7 @@ def image(self, value: ImageFieldFile) -> Optional[str]:
return value.url


class CategoryToModel(odin.Mapping):
class CategoryToModel(OscarBaseMapping):
"""Map from a category resource to a model."""

from_obj = resources.catalogue.Category
Expand Down Expand Up @@ -123,21 +123,21 @@ def description(self, value):
return None


class ProductClassToResource(odin.Mapping):
class ProductClassToResource(OscarBaseMapping):
"""Map from a product class model to a resource."""

from_obj = ProductClassModel
to_obj = resources.catalogue.ProductClass


class ProductClassToModel(odin.Mapping):
class ProductClassToModel(OscarBaseMapping):
"""Map from a product class resource to a model."""

from_obj = resources.catalogue.ProductClass
to_obj = ProductClassModel


class ProductToResource(odin.Mapping):
class ProductToResource(OscarBaseMapping):
"""Map from a product model to a resource."""

from_obj = ProductModel
Expand Down Expand Up @@ -302,7 +302,7 @@ def product_class(self, value) -> ProductClassModel:
return ProductClassToModel.apply(value)


class ParentToModel(odin.Mapping):
class ParentToModel(OscarBaseMapping):
from_obj = resources.catalogue.ParentProduct
to_obj = ProductModel

Expand Down
20 changes: 10 additions & 10 deletions oscar_odin/mappings/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from oscar.core.loading import get_model

from .. import resources
from ._common import map_queryset
from ._common import map_queryset, OscarBaseMapping
from .address import BillingAddressToResource, ShippingAddressToResource
from .auth import UserToResource

Expand All @@ -26,14 +26,14 @@
SurchargeModel = get_model("order", "Surcharge")


class SurchargeToResource(odin.Mapping):
class SurchargeToResource(OscarBaseMapping):
"""Mapping from a surcharge model to a resource."""

from_obj = SurchargeModel
to_obj = resources.order.Surcharge


class DiscountToResource(odin.Mapping):
class DiscountToResource(OscarBaseMapping):
"""Mapping from an order discount model to a resource."""

from_obj = OrderDiscountModel
Expand All @@ -45,28 +45,28 @@ def category(self, value: str) -> resources.order.DiscountCategory:
return resources.order.DiscountCategory(value)


class ShippingEventToResource(odin.Mapping):
class ShippingEventToResource(OscarBaseMapping):
"""Mapping from a shipping event model to a resource."""

from_obj = ShippingEventModel
to_obj = resources.order.ShippingEvent


class PaymentEventToResource(odin.Mapping):
class PaymentEventToResource(OscarBaseMapping):
"""Mapping from a payment event model to a resource."""

from_obj = PaymentEventModel
to_obj = resources.order.PaymentEvent


class LinePriceToResource(odin.Mapping):
class LinePriceToResource(OscarBaseMapping):
"""Mapping from Line price to resource."""

from_obj = LinePriceModel
to_obj = resources.order.LinePrice


class LineToResource(odin.Mapping):
class LineToResource(OscarBaseMapping):
"""Mapping from Line model to resource."""

from_obj = LineModel
Expand All @@ -83,21 +83,21 @@ def attributes(self) -> Dict[str, Any]:
"""Map attributes."""


class StatusChangeToResource(odin.Mapping):
class StatusChangeToResource(OscarBaseMapping):
"""Mapping from order status change model to resource."""

from_obj = OrderStatusChangeModel
to_obj = resources.order.StatusChange


class NoteToResource(odin.Mapping):
class NoteToResource(OscarBaseMapping):
"""Mapping from order note model to resource."""

from_obj = OrderNoteModel
to_obj = resources.order.Note


class OrderToResource(odin.Mapping):
class OrderToResource(OscarBaseMapping):
"""Mapping from order model to resource."""

from_obj = OrderModel
Expand Down
16 changes: 8 additions & 8 deletions tests/reverse/test_catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ def test_error_handling_on_product_operations(self):
_, errors = products_to_db(product_resource)
self.assertEqual(len(errors), 1)
self.assertEqual(
errors[0].message_dict['display_order'][0],
"“top” value must be an integer."
errors[0].message_dict["display_order"][0],
"“top” value must be an integer.",
)

# Correct Data for creating product
Expand Down Expand Up @@ -666,14 +666,14 @@ def test_error_handling_on_product_operations(self):

self.assertEqual(len(errors), 3)
self.assertEqual(
errors[0].message_dict['is_discountable'][0],
"“53” value must be either True or False."
errors[0].message_dict["is_discountable"][0],
"“53” value must be either True or False.",
)
self.assertEqual(
errors[1].message_dict['display_order'][0],
"“Alphabet” value must be an integer."
errors[1].message_dict["display_order"][0],
"“Alphabet” value must be an integer.",
)
self.assertEqual(
errors[2].message_dict['price'][0],
"“expensive” value must be a decimal number."
errors[2].message_dict["price"][0],
"“expensive” value must be a decimal number.",
)
Loading