Skip to content

Commit

Permalink
better import for budy and omni
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed May 16, 2016
1 parent 82822ad commit 1f7ccdc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/budy/bots/omni_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def sync_sub_product(self, merchandise):
if not measurement:
product = api.get_product(product["object_id"])
self.sync_product(product)
measurement = budy.Measurement.from_omni(sub_product)
measurement = budy.Measurement.from_omni(merchandise, sub_product)

if not measurement: return

Expand Down
8 changes: 8 additions & 0 deletions src/budy/models/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@ def validate(cls):
@classmethod
def list_names(cls):
return ["id", "name"]

@classmethod
def ensure_s(cls, name):
category = cls.get(name = name, raise_e = False)
if category: return category
category = cls(name = name)
category.save()
return category
8 changes: 8 additions & 0 deletions src/budy/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@ def validate(cls):
@classmethod
def list_names(cls):
return ["id", "name"]

@classmethod
def ensure_s(cls, name):
collection = cls.get(name = name, raise_e = False)
if collection: return collection
collection = cls(name = name)
collection.save()
return collection
8 changes: 8 additions & 0 deletions src/budy/models/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@ def validate(cls):
@classmethod
def list_names(cls):
return ["id", "name"]

@classmethod
def ensure_s(cls, name):
color = cls.get(name = name, raise_e = False)
if color: return color
color = cls(name = name)
color.save()
return color
12 changes: 12 additions & 0 deletions src/budy/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,17 @@ def index_names(cls):

@classmethod
def from_omni(cls, merchandise, gender = "Both", currency = "EUR"):
from . import color
from . import category
from . import collection
company_product_code = merchandise["company_product_code"]
metadata = merchandise["metadata"] or dict()
_color = metadata.get("material")
_category = metadata.get("category")
_collection = metadata.get("collection")
if _color: _color = color.Color.ensure_s(_color)
if _category: _category = category.Category.ensure_s(_category)
if _collection: _collection = collection.Collection.ensure_s(_collection)
_product = cls.get(product_id = company_product_code, raise_e = False)
if not _product: _product = cls()
_product.product_id = company_product_code
Expand All @@ -228,6 +237,9 @@ def from_omni(cls, merchandise, gender = "Both", currency = "EUR"):
_product.price = merchandise["retail_price"]
_product.currency = currency
_product.characteristics = metadata.get("characteristics", [])
_product.colors = [_color]
_product.categories = [_category]
_product.collections = [_collection]
return _product

@classmethod
Expand Down

0 comments on commit 1f7ccdc

Please sign in to comment.