From 33450c577c51bc1ed212c05cbca51bb82c40f13d Mon Sep 17 00:00:00 2001 From: Viggo de Vries Date: Fri, 8 Dec 2023 16:20:56 +0100 Subject: [PATCH] Add image mapping --- .gitignore | 1 + .../management/commands/test_illshit.py | 3 +- oscar_odin/mappings/catalogue.py | 9 ---- oscar_odin/mappings/context.py | 2 +- oscar_odin/resources/catalogue.py | 2 +- tests/reverse/test_catalogue.py | 42 +++++++++++++++---- 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 78dc5ec..895dd31 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ lib/ lib64/ parts/ sdist/ +images/ var/ wheels/ share/python-wheels/ diff --git a/oscar_odin/management/commands/test_illshit.py b/oscar_odin/management/commands/test_illshit.py index eedc047..4d7307c 100644 --- a/oscar_odin/management/commands/test_illshit.py +++ b/oscar_odin/management/commands/test_illshit.py @@ -95,9 +95,8 @@ def handle(self, *args, **options): attributes=attributes ) ) - with querycounter("COMMANDO"): products_to_db(products) - + print("AANTAL PRODUCTEN AANGEMAAKT:", Product.objects.count()) diff --git a/oscar_odin/mappings/catalogue.py b/oscar_odin/mappings/catalogue.py index e7d6360..1ec059b 100644 --- a/oscar_odin/mappings/catalogue.py +++ b/oscar_odin/mappings/catalogue.py @@ -65,15 +65,6 @@ class ProductImageToModel(odin.Mapping): from_obj = resources.catalogue.Image to_obj = ProductImageModel - @odin.map_field - def original(self, value: str) -> str: - """Convert value into a pure URL.""" - # TODO convert into a form that can be accepted by a model - if value: - return value - - return "lege-image" - @odin.map_field def date_created(self, value: datetime) -> datetime: if value: diff --git a/oscar_odin/mappings/context.py b/oscar_odin/mappings/context.py index 055f98f..355f637 100644 --- a/oscar_odin/mappings/context.py +++ b/oscar_odin/mappings/context.py @@ -100,7 +100,7 @@ def add_instances_to_o2m_relation(self, relation, instances): def add_instance_to_fk_items(self, field, instance): if not instance.pk: self.foreign_key_items[field] += [instance] - + def add_fields_to_update(self, fields_to_update): self.fields_to_update = fields_to_update diff --git a/oscar_odin/resources/catalogue.py b/oscar_odin/resources/catalogue.py index 71b3560..2a127f1 100644 --- a/oscar_odin/resources/catalogue.py +++ b/oscar_odin/resources/catalogue.py @@ -31,7 +31,7 @@ class Meta: verbose_name_plural = "Product images" id: int - original: Url = odin.Options(validators=[]) + original: Any caption: str = odin.Options(empty=True) display_order: int = odin.Options( default=0, diff --git a/tests/reverse/test_catalogue.py b/tests/reverse/test_catalogue.py index 149bc16..ed36d59 100644 --- a/tests/reverse/test_catalogue.py +++ b/tests/reverse/test_catalogue.py @@ -1,6 +1,11 @@ +import io +import PIL + from decimal import Decimal as D +from django.core.files import File from django.test import TestCase + from oscar.core.loading import get_model from oscar_odin.mappings.catalogue import products_to_db @@ -24,6 +29,14 @@ class SingleProductReverseTest(TestCase): + + @property + def image(self): + img = PIL.Image.new(mode="RGB", size=(200, 200)) + output = io.BytesIO() + img.save(output, "jpeg") + return output + def test_create_product_with_related_fields(self): product_class = ProductClass.objects.create( name="Klaas", slug="klaas", requires_shipping=True, track_stock=True @@ -57,8 +70,8 @@ def test_create_product_with_related_fields(self): partner=partner, product_class=product_class, images=[ - ImageResource(caption="gekke caption", display_order=0), - ImageResource(caption="gekke caption 2", display_order=1), + ImageResource(caption="gekke caption", display_order=0, original=File(self.image, name="harrie.jpg")), + ImageResource(caption="gekke caption 2", display_order=1, original=File(self.image, name="vats.jpg")), ], categories=[CategoryResource(name="henk", slug="klaas"), CategoryResource(name="Hatsie datsie", slug="batsie")], attributes={"henk": "Klaas", "harrie": 1} @@ -82,6 +95,8 @@ def test_create_product_with_related_fields(self): self.assertEquals(prd.attr.henk, "Klaas") self.assertEquals(prd.attr.harrie, 1) + self.assertEquals(prd.images.count(), 2) + product_resource = ProductResource( upc="1234323-2", price=D("21.50"), @@ -89,10 +104,14 @@ def test_create_product_with_related_fields(self): currency="EUR", partner=partner, product_class=product_class, + images=[ + ImageResource(caption="gekke caption", display_order=0, original=File(self.image, name="harriebatsie.jpg")), + ImageResource(caption="gekke caption 2", display_order=1, original=File(self.image, name="vatsie.jpg")), + ], categories=[CategoryResource(name="henk", slug="klaas"), CategoryResource(name="Hatsie datsie", slug="batsie")], ) - fields_to_update = ["upc", "price", "num_in_stock", "num_allocated"] + fields_to_update = ["upc", "price", "num_in_stock", "num_allocated", "original"] products_to_db(product_resource, fields_to_update=fields_to_update) prd = Product.objects.get(upc="1234323-2") @@ -101,9 +120,18 @@ def test_create_product_with_related_fields(self): self.assertEquals(stockrecord.price, D("21.50")) self.assertEquals(stockrecord.num_in_stock, 3) self.assertEquals(prd.categories.count(), 2) + + self.assertEquals(prd.images.count(), 4) class MultipleProductReverseTest(TestCase): + @property + def image(self): + img = PIL.Image.new(mode="RGB", size=(200, 200)) + output = io.BytesIO() + img.save(output, "jpeg") + return output + def test_create_simple_product(self): product_class = ProductClass.objects.create( name="Klaas", slug="klaas", requires_shipping=True, track_stock=True @@ -166,8 +194,8 @@ def test_create_product_with_related_fields(self): currency="EUR", product_class=product_class, images=[ - ImageResource(caption="gekke caption", display_order=0), - ImageResource(caption="gekke caption 2", display_order=1), + ImageResource(caption="gekke caption", display_order=0, original=File(self.image, name="klaas.jpg")), + ImageResource(caption="gekke caption 2", display_order=1, original=File(self.image, name="harrie.jpg")), ], attributes={"henk": "Poep", "harrie": 22} ), @@ -184,8 +212,8 @@ def test_create_product_with_related_fields(self): partner=Partner.objects.create(name="klaas"), product_class=product_class, images=[ - ImageResource(caption="gekke caption", display_order=0), - ImageResource(caption="gekke caption 2", display_order=1), + ImageResource(caption="gekke caption", display_order=0, original=File(self.image, name="klaas.jpg")), + ImageResource(caption="gekke caption 2", display_order=1, original=File(self.image, name="harrie.jpg")), ], attributes={"henk": "Klaas", "harrie": 1} )