Skip to content

Commit

Permalink
Merge 24ddb66 into 2c21383
Browse files Browse the repository at this point in the history
  • Loading branch information
tsilva committed May 31, 2016
2 parents 2c21383 + 24ddb66 commit 8b3db9a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/budy/controllers/api/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ def show(self, id):
)
return product

@appier.route("/api/products/search", "GET", json = True)
def search(self):
object = appier.get_object(alias = True, find = True)
products = budy.Product.find(
find_t = "both",
find_n = "search_description",
eager = ("images", "brand"),
map = True,
**object
)
return products

@appier.route("/api/products/<int:id>/related", "GET", json = True)
def related(self, id):
limit = self.field("limit", 10, int)
Expand Down
67 changes: 65 additions & 2 deletions src/budy/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class Product(base.BudyBase):
default = True
)

search_description = appier.field(
index = True
)

product_id = appier.field(
index = True
)
Expand Down Expand Up @@ -382,9 +386,17 @@ def simple_csv_url(cls, absolute = False):

def pre_save(self):
base.BudyBase.pre_save(self)
self._update_search_description()
if not self.measurements: return
self.quantity_hand = sum(measurement.quantity for measurement in self.measurements)
self.price = max(measurement.price for measurement in self.measurements)
self.quantity_hand = sum(measurement.quantity_hand for measurement in\
self.measurements if hasattr(measurement, "quantity_hand"))
self.price = max(measurement.price for measurement in\
self.measurements if hasattr(measurement, "price"))

@appier.operation(name = "Update Search Description")
def update_search_description_s(self):
self._update_search_description()
self.save()

def related(self, limit = 6):
cls = self.__class__
Expand All @@ -410,6 +422,9 @@ def related(self, limit = 6):

def get_measurement(self, value, name = None):
for measurement in self.measurements:
if not measurement: continue
if not hasattr(measurement, "value"): continue
if not hasattr(measurement, "name"): continue
if not measurement.value == value: continue
if not measurement.name == name: continue
return measurement
Expand Down Expand Up @@ -581,3 +596,51 @@ def _get_offset_offset(self, count, limit, kwargs):
kwargs["id"] = {"$lt" : self.id}
offset = cls.count(**kwargs)
return offset

def _update_search_description(self):
tokens = []
self._add_tokens("short_description", tokens, pluralize = True)
self._add_tokens("product_id", tokens, pluralize = False)
self._add_tokens("brand.name", tokens, pluralize = True)
self._add_tokens("season.name", tokens, pluralize = True)
self._add_tokens("characteristics", tokens, pluralize = False)
self._add_tokens("colors.name", tokens, pluralize = True)
self._add_tokens("categories.name", tokens, pluralize = True)
self._add_tokens("collections.name", tokens, pluralize = True)
self._add_tokens("compositions.name", tokens, pluralize = True)
tokens_s = "|".join(tokens)
tokens_s = tokens_s.lower()
tokens_s = self._simplify(tokens_s)
self.search_description = tokens_s

def _add_tokens(self, attribute, tokens, pluralize = False):
attributes = attribute.split(".")
attribute = attributes[0]
attributes = attributes[1:] if len(attributes) > 1 else []
if not hasattr(self, attribute): return

value = getattr(self, attribute)
if not value: return

type_s = str(type(value))
values = value if type_s in ("<type 'list'>", "<type 'tuple'>", "<class 'appier.typesf._References'>") else [value]
for value in values:
for _attribute in attributes: value = getattr(value, _attribute)
tokens.append(value)
if pluralize: tokens.append(value)

def _simplify(self, value):
value = value.replace("á", "a")
value = value.replace("é", "e")
value = value.replace("í", "i")
value = value.replace("ó", "o")
value = value.replace("ú", "u")
value = value.replace("ã", "a")
value = value.replace("õ", "o")
value = value.replace("â", "a")
value = value.replace("ô", "o")
value = value.replace("ç", "c")
return value

def _pluralize(self, value):
return value + "s"

0 comments on commit 8b3db9a

Please sign in to comment.