diff --git a/oscar_odin/mappings/_common.py b/oscar_odin/mappings/_common.py index 96e222a..fb32181 100644 --- a/oscar_odin/mappings/_common.py +++ b/oscar_odin/mappings/_common.py @@ -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( @@ -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 diff --git a/oscar_odin/mappings/_model_mapper.py b/oscar_odin/mappings/_model_mapper.py index d0d4768..af5233b 100644 --- a/oscar_odin/mappings/_model_mapper.py +++ b/oscar_odin/mappings/_model_mapper.py @@ -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) diff --git a/oscar_odin/mappings/address.py b/oscar_odin/mappings/address.py index bee3b9c..e25fae1 100644 --- a/oscar_odin/mappings/address.py +++ b/oscar_odin/mappings/address.py @@ -3,6 +3,7 @@ from oscar.core.loading import get_model from .. import resources +from ._common import OscarBaseMapping __all__ = ( "BillingAddressToResource", @@ -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 @@ -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 diff --git a/oscar_odin/mappings/auth.py b/oscar_odin/mappings/auth.py index d1be723..495f190 100644 --- a/oscar_odin/mappings/auth.py +++ b/oscar_odin/mappings/auth.py @@ -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 diff --git a/oscar_odin/mappings/catalogue.py b/oscar_odin/mappings/catalogue.py index 01e7f75..463c030 100644 --- a/oscar_odin/mappings/catalogue.py +++ b/oscar_odin/mappings/catalogue.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/oscar_odin/mappings/order.py b/oscar_odin/mappings/order.py index 2cdd2be..376c368 100644 --- a/oscar_odin/mappings/order.py +++ b/oscar_odin/mappings/order.py @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/tests/reverse/test_catalogue.py b/tests/reverse/test_catalogue.py index 4b7402a..d96c993 100644 --- a/tests/reverse/test_catalogue.py +++ b/tests/reverse/test_catalogue.py @@ -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 @@ -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.", )