Skip to content

Commit 64b2071

Browse files
authored
[FEAT] Make structure and title optional (#76)
* feat ⭐ make structure and title optional * tests ✅ update test * tests ✅ dont clean if there is no structure * refactor 📦 update exception message * feat ⭐ make upc optional for product's related models
1 parent 2131d0e commit 64b2071

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

oscar_odin/mappings/context.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,9 @@ def bulk_update_or_create_many_to_many(self):
408408
# Instead of failing bulk_create here below, we will add an error.
409409
self.errors.add_error(
410410
OscarOdinException(
411-
f"Cannot create m2m relationship {Through.__name__} - related model '{relation.related_model.__name__}' is missing a primary key"
411+
{
412+
Through.__name__: f"Cannot create m2m relationship {Through.__name__} - related model '{relation.related_model.__name__}' is missing a primary key"
413+
}
412414
),
413415
instance,
414416
)

oscar_odin/resources/catalogue.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class CategoryResource(OscarCatalogueResource):
7070
depth: Optional[int]
7171
path: Optional[str]
7272
children: Optional[List["CategoryResource"]] = odin.ListOf.delayed(
73-
lambda: CategoryResource
73+
lambda: CategoryResource, null=True
7474
)
7575

7676

@@ -89,11 +89,11 @@ class ProductAttributeValueResource(OscarCatalogueResource):
8989

9090

9191
class ParentProductResource(OscarCatalogueResource):
92-
upc: str
92+
upc: Optional[str]
9393

9494

9595
class ProductRecommentationResource(OscarCatalogueResource):
96-
upc: str
96+
upc: Optional[str]
9797

9898

9999
class ProductResource(OscarCatalogueResource):
@@ -102,8 +102,10 @@ class ProductResource(OscarCatalogueResource):
102102
id: Optional[int]
103103
code: Optional[str]
104104
upc: Optional[str]
105-
structure: str = StringField(choices=ProductModel.STRUCTURE_CHOICES)
106-
title: str
105+
structure: Optional[str] = StringField(
106+
choices=ProductModel.STRUCTURE_CHOICES, null=True
107+
)
108+
title: Optional[str]
107109
slug: Optional[str]
108110
description: Optional[str] = ""
109111
meta_title: Optional[str]

tests/reverse/test_catalogue.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
PRODUCT_TITLE,
2828
PRODUCT_UPC,
2929
PRODUCT_DESCRIPTION,
30+
PRODUCT_IS_DISCOUNTABLE,
3031
PRODUCTCLASS_REQUIRESSHIPPING,
3132
MODEL_IDENTIFIERS_MAPPING,
3233
)
@@ -240,14 +241,12 @@ def test_resource_default_value(self):
240241
prd = Product.objects.get(upc="1234")
241242
self.assertEqual(prd.is_discountable, False)
242243

243-
product_resource = ProductResource(
244-
upc="1234",
245-
title="bat",
246-
slug="asdf",
247-
structure=Product.STANDALONE,
248-
product_class=product_class,
244+
product_resource = ProductResource(upc="1234", is_discountable=True)
245+
_, errors = products_to_db(
246+
product_resource,
247+
fields_to_update=[PRODUCT_IS_DISCOUNTABLE],
248+
clean_instances=False,
249249
)
250-
_, errors = products_to_db(product_resource)
251250
self.assertEqual(len(errors), 0)
252251
prd.refresh_from_db()
253252
# Default value of is_discountable is considered from the ProductResource
@@ -678,7 +677,7 @@ def test_recommendation_non_existing(self):
678677
_, errors = products_to_db(product_resource)
679678
self.assertEqual(len(errors), 1)
680679
self.assertEqual(
681-
str(errors[0]),
680+
str(errors[0].args[0]["ProductRecommendation"]),
682681
"Cannot create m2m relationship ProductRecommendation - related model 'Product' is missing a primary key",
683682
)
684683

0 commit comments

Comments
 (0)