Skip to content

Commit

Permalink
Add image mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
viggo-devries committed Dec 8, 2023
1 parent 71011b1 commit 33450c5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lib/
lib64/
parts/
sdist/
images/
var/
wheels/
share/python-wheels/
Expand Down
3 changes: 1 addition & 2 deletions oscar_odin/management/commands/test_illshit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
9 changes: 0 additions & 9 deletions oscar_odin/mappings/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion oscar_odin/mappings/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion oscar_odin/resources/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 35 additions & 7 deletions tests/reverse/test_catalogue.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -82,17 +95,23 @@ 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"),
availability=3,
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")
Expand All @@ -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
Expand Down Expand Up @@ -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}
),
Expand All @@ -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}
)
Expand Down

0 comments on commit 33450c5

Please sign in to comment.