-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setting multiple stockrecords on product, add chunked by defaul…
…t to resources_to_db, don't error on non existent m2m (#50) * Add children to category resource * Add chunked by default in odin, do not try creating m2m instances if the through as no id, children field on category model, partner identifier is code, and partner resource/model * This test no longer raises exception + improve error message. * Allow passing extra context. in case a context mapper is not enough (eg; you need a map for references) * Review feedback & add test for multiple stockrecords * Allow setting chunk size in products_to_db too. --------- Co-authored-by: Joey Jurjens <joey@highbiza.nl>
- Loading branch information
1 parent
2cedd70
commit 8b2bc38
Showing
10 changed files
with
250 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import odin | ||
|
||
from oscar.core.loading import get_class, get_model | ||
|
||
ModelMapping = get_class("oscar_odin.mappings.model_mapper", "ModelMapping") | ||
OscarBaseMapping = get_class("oscar_odin.mappings.common", "OscarBaseMapping") | ||
|
||
# resources | ||
PartnerResource = get_class("oscar_odin.resources.partner", "PartnerResource") | ||
StockRecordResource = get_class("oscar_odin.resources.partner", "StockRecordResource") | ||
|
||
Partner = get_model("partner", "Partner") | ||
StockRecord = get_model("partner", "StockRecord") | ||
|
||
|
||
class PartnerModelToResource(OscarBaseMapping): | ||
from_obj = Partner | ||
to_obj = PartnerResource | ||
|
||
|
||
class PartnerToModel(ModelMapping): | ||
from_obj = PartnerResource | ||
to_obj = Partner | ||
|
||
|
||
class StockRecordModelToResource(OscarBaseMapping): | ||
from_obj = StockRecord | ||
to_obj = StockRecordResource | ||
|
||
@odin.map_field | ||
def partner(self, partner): | ||
return PartnerModelToResource.apply(partner) | ||
|
||
|
||
class StockRecordToModel(ModelMapping): | ||
from_obj = StockRecordResource | ||
to_obj = StockRecord | ||
|
||
@odin.map_field | ||
def partner(self, partner): | ||
return PartnerToModel.apply(partner) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Optional | ||
from decimal import Decimal | ||
|
||
from oscar.core.loading import get_class | ||
|
||
from ..fields import DecimalField | ||
|
||
OscarResource = get_class("oscar_odin.resources.base", "OscarResource") | ||
|
||
|
||
class OscarPartnerResource(OscarResource, abstract=True): | ||
"""Base resource for Oscar partner application.""" | ||
|
||
class Meta: | ||
allow_field_shadowing = True | ||
namespace = "oscar.partner" | ||
|
||
|
||
class PartnerResource(OscarPartnerResource): | ||
id: Optional[int] | ||
name: str | ||
code: str | ||
|
||
|
||
class StockRecordResource(OscarPartnerResource): | ||
id: Optional[int] | ||
partner_sku: str | ||
num_in_stock: Optional[int] | ||
num_allocated: Optional[int] | ||
price: Decimal = DecimalField() | ||
currency: Optional[str] | ||
|
||
# It's optional because we allow easy price setting on the product resource by setting the partner model | ||
# on the product resource, which in turn is used to create a stockrecord with other price related fields. | ||
partner: Optional[PartnerResource] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.conf import settings | ||
|
||
RESOURCES_TO_DB_CHUNK_SIZE = getattr(settings, "RESOURCES_TO_DB_CHUNK_SIZE", 500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.