def _getSubCategories(self, category): """ """ result = [] cm = ICategoryManagement(category) categories = cm.getTopLevelCategories() for category in categories: show_subtree = self._showSubTree(category) if show_subtree == True: sub_categories = self._getSubCategories(category) else: sub_categories = [] klass = "visualIconPadding" if len(category.getBRefs("parent_category")) > 0: klass += " hasCategories" if show_subtree == True: klass += " navTreeCurrentItem" result.append({ "klass" : klass, "url" : category.absolute_url(), "description" : category.Description(), "title" : category.Title(), "amount_of_products" : 1, "subcategories" : sub_categories, "show_subtree" : show_subtree, }) return result
def _showSubTree(self, category): """Decides, whether a subtree of a category will be displayed or not. """ if self.data.expand_all == True: return True context_url = self.context.absolute_url() category_url = category.getURL() if context_url.startswith(category_url) == True: return True elif IProduct.providedBy(self.context) == True: cm = ICategoryManagement(self.context) try: product_category = cm.getTopLevelCategories()[0] except IndexError: return False while ICategory.providedBy(product_category) == True: if product_category.UID() == category.UID: return True product_category = product_category.aq_inner.aq_parent return False
def getTopLevelCategories(self): """ """ shop = IShopManagement(self.context).getShop() cm = ICategoryManagement(shop) return cm.getTopLevelCategories()
def _showSubTree(self, category): """Decides, whether a subtree of a category will be displayed or not. """ if self.data.expand_all == True: return True context_url = self.context.absolute_url() category_url = category.getURL() if context_url.startswith(category_url) == True: return True elif IProduct.providedBy(self.context) == True: cm = ICategoryManagement(self.context) try: product_category = cm.getTopLevelCategories()[0] except IndexError: return False while ICategory.providedBy(product_category) == True: if product_category.UID() == category.UID: return True product_category = product_category.aq_inner.aq_parent return False
def getSelectedProducts(self): """ """ selected_uids = self.request.get("selected_uids") catalog = getToolByName(self.context, "portal_catalog") brains = catalog.searchResults( UID = selected_uids ) result = [] for brain in brains: product = brain.getObject() cm = ICategoryManagement(product) categories = cm.getTopLevelCategories() categories = ", ".join([c.Title() for c in categories]) result.append({ "uid" : product.UID(), "id" : product.getId(), "title" : product.Title(), "price" : product.getPrice(), "categories" : categories, }) return result
def getTopLevelCategories(self): """ """ mall = IMallManagement(self.context).getMall() cm = ICategoryManagement(mall) return cm.getTopLevelCategories()
def _getSubCategories(self, category): """ """ result = [] cm = ICategoryManagement(category) categories = cm.getTopLevelCategories() for category in categories: show_subtree = self._showSubTree(category) if show_subtree == True: sub_categories = self._getSubCategories(category) else: sub_categories = [] klass = "visualIconPadding" if len(category.getBRefs("parent_category")) > 0: klass += " hasCategories" if show_subtree == True: klass += " navTreeCurrentItem" result.append({ "klass": klass, "url": category.absolute_url(), "description": category.Description(), "title": category.Title(), "amount_of_products": 1, "subcategories": sub_categories, "show_subtree": show_subtree, }) return result
def getTopLevelCategories(self): """ """ mall = IMallManagement(self.context).getMall() cm = ICategoryManagement(mall) return cm.getTopLevelCategories()
def getTopLevelCategories(self): """ """ shop = IShopManagement(self.context).getShop() cm = ICategoryManagement(shop) return cm.getTopLevelCategories()
def getSelectedProducts(self): """ """ selected_uids = self.request.get("selected_uids") catalog = getToolByName(self.context, "portal_catalog") brains = catalog.searchResults(UID=selected_uids) result = [] for brain in brains: product = brain.getObject() cm = ICategoryManagement(product) categories = cm.getTopLevelCategories() categories = ", ".join([c.Title() for c in categories]) result.append({ "uid": product.UID(), "id": product.getId(), "title": product.Title(), "price": product.getPrice(), "categories": categories, }) return result
def testGetTopLevelCategories(self): """ """ self.shop.category_1.category_11.reindexObject() self.shop.category_1.category_12.reindexObject() self.shop.category_1.category_11.category_111.reindexObject() cm = ICategoryManagement(self.shop) ids = [c.id for c in cm.getTopLevelCategories()] self.assertEqual(ids, ["category_1", "category_2", "category_3"])
def testGetTopLevelCategories(self): """ """ cm = ICategoryManagement(self.portal.myshop.categories.category_1) category_ids = [c.id for c in cm.getTopLevelCategories()] self.failUnless(len(category_ids) == 2) for id in ["category_11", "category_12"]: self.failUnless(id in category_ids) cm = ICategoryManagement(self.category_2) self.failUnless(len(cm.getCategories()) == 0)
def testGetTopLevelCategories(self): """ """ cm = ICategoryManagement(self.portal.myshop.categories.category_1) category_ids = [c.id for c in cm.getTopLevelCategories()] self.failUnless(len(category_ids) == 2) for id in ["category_11", "category_12"]: self.failUnless(id in category_ids) cm = ICategoryManagement(self.category_2) self.failUnless(len(cm.getCategories()) == 0)
def getCategories(self): """ """ f = self.getFormats() products_per_line = f.get("products_per_line") cm = ICategoryManagement(self.context) line = [] lines = [] for i, category in enumerate(cm.getTopLevelCategories()): # Image if len(category.getImage()) != 0: image_url = "%s/image_%s" % \ (category.absolute_url(), f.get("image_size")) else: image_url = None # Text temp = f.get("text") if temp == "description": text = category.getDescription() elif temp == "short_text": text = category.getShortText() elif temp == "text": text = category.getText() else: text = "" # CSS Class if (i + 1) % products_per_line == 0: klass = "last" else: klass = "notlast" line.append({ "title" : category.Title(), "text" : text, "url" : category.absolute_url(), "image_url" : image_url, "klass" : klass, }) if (i+1) % products_per_line == 0: lines.append(line) line = [] if len(line) > 0: lines.append(line) return lines
def isValid(self, product): """Returns True if the given product has at least one of the selected categories of the criterion. """ cm = ICategoryManagement(product) product_categories = ["/".join(c.getPhysicalPath()) for c in cm.getTopLevelCategories()] criteria_categories = self.context.getCategories() for criteria_category in criteria_categories: if criteria_category in product_categories: return True return False
def getCategories(self): """ """ f = self.getFormats() products_per_line = f.get("products_per_line") cm = ICategoryManagement(self.context) line = [] lines = [] for i, category in enumerate(cm.getTopLevelCategories()): # Image if len(category.getImage()) != 0: image_url = "%s/image_%s" % \ (category.absolute_url(), f.get("image_size")) else: image_url = None # Text temp = f.get("text") if temp == "description": text = category.getDescription() elif temp == "short_text": text = category.getShortText() elif temp == "text": text = category.getText() else: text = "" # CSS Class if (i + 1) % products_per_line == 0: klass = "last" else: klass = "notlast" line.append({ "title": category.Title(), "text": text, "url": category.absolute_url(), "image_url": image_url, "klass": klass, }) if (i + 1) % products_per_line == 0: lines.append(line) line = [] if len(line) > 0: lines.append(line) return lines
def isValid(self, product): """Returns True if the given product has at least one of the selected categories of the criterion. """ cm = ICategoryManagement(product) product_categories = [ "/".join(c.getPhysicalPath()) for c in cm.getTopLevelCategories() ] criteria_categories = self.context.getCategories() for criteria_category in criteria_categories: if criteria_category in product_categories: return True return False
def getProductURLs(self): """ """ sorting = self.request.SESSION.get("sorting") try: sorted_on, sort_order = sorting.split("-") except (AttributeError, ValueError): sorted_on = "price" sort_order = "desc" # all categories of the product cm = ICategoryManagement(self.context) categories = cm.getTopLevelCategories() result = [] for category in categories: pm = IProductManagement(category) products = pm.getAllProducts( sorted_on=sorted_on, sort_order = sort_order) # determine position of product within category index = products.index(self.context) # generate previous/next url temp = {} if index==0: temp["previous"] = None temp["first"] = None else: product = products[index-1] temp["previous"] = product.absolute_url() temp["first"] = products[0].absolute_url() try: product = products[index+1] temp["next"] = product.absolute_url() temp["last"] = products[-1].absolute_url() except IndexError: temp["next"] = None temp["last"] = None temp["category_url"] = category.absolute_url() temp["category"] = category.Title() temp["position"] = index + 1 temp["amount"] = len(products) result.append(temp) return result
def testGetTopLevelCategories_1(self): """ """ cm = ICategoryManagement(self.product_1) ids = [c.getId() for c in cm.getTopLevelCategories()] self.assertEqual(ids, ["category_11"]) # adding some more self.shop.categories.invokeFactory("Category", id="category_a") self.shop.categories.invokeFactory("Category", id="category_b") self.shop.categories.category_a.addReference( self.product_1, "categories_products") self.shop.categories.category_b.addReference( self.product_1, "categories_products") ids = [c.getId() for c in cm.getTopLevelCategories()] self.failUnless(len(ids) == 3) for id in ["category_11", "category_a", "category_b"]: self.failUnless(id in ids)
def getProductURLs(self): """ """ sorting = self.request.SESSION.get("sorting") try: sorted_on, sort_order = sorting.split("-") except (AttributeError, ValueError): sorted_on = "price" sort_order = "desc" # all categories of the product cm = ICategoryManagement(self.context) categories = cm.getTopLevelCategories() result = [] for category in categories: pm = IProductManagement(category) products = pm.getAllProducts(sorted_on=sorted_on, sort_order=sort_order) # determine position of product within category index = products.index(self.context) # generate previous/next url temp = {} if index == 0: temp["previous"] = None temp["first"] = None else: product = products[index - 1] temp["previous"] = product.absolute_url() temp["first"] = products[0].absolute_url() try: product = products[index + 1] temp["next"] = product.absolute_url() temp["last"] = products[-1].absolute_url() except IndexError: temp["next"] = None temp["last"] = None temp["category_url"] = category.absolute_url() temp["category"] = category.Title() temp["position"] = index + 1 temp["amount"] = len(products) result.append(temp) return result
def _showSubTree(self, category): """Decides, whether a subtree of a category will be displayed or not. """ if self.data.expand_all == True: return True # Check if the passed category is ancestor of context if ICategory.providedBy(self.context) == True: obj = self.context while obj is not None: if category == obj: return True try: obj = obj.getRefs("parent_category")[0] except IndexError: obj = None if IProduct.providedBy(self.context) == True: cm = ICategoryManagement(self.context) try: product_category = cm.getTopLevelCategories()[0] except IndexError: return False while ICategory.providedBy(product_category) == True: if product_category.UID() == category.UID(): return True product_category = product_category.aq_inner.aq_parent return False if IProductSelector.providedBy(self.context) == True: obj = self.context.aq_inner.aq_parent while obj is not None: if category == obj: return True try: obj = obj.getRefs("parent_category")[0] except IndexError: obj = None return False
def _showSubTree(self, category): """Decides, whether a subtree of a category will be displayed or not. """ if self.data.expand_all == True: return True # Check if the passed category is ancestor of context if ICategory.providedBy(self.context) == True: obj = self.context while obj is not None: if category == obj: return True try: obj = obj.getRefs("parent_category")[0] except IndexError: obj = None if IProduct.providedBy(self.context) == True: cm = ICategoryManagement(self.context) try: product_category = cm.getTopLevelCategories()[0] except IndexError: return False while ICategory.providedBy(product_category) == True: if product_category.UID() == category.UID(): return True product_category = product_category.aq_inner.aq_parent return False if IProductSelector.providedBy(self.context) == True: obj = self.context.aq_inner.aq_parent while obj is not None: if category == obj: return True try: obj = obj.getRefs("parent_category")[0] except IndexError: obj = None return False
def getCategories(self): """ """ current_category_uid = self.request.get("uid") view = getMultiAdapter((self.context, self.request), name='search-view') brains = view.getSearchResults() products = [brain.getObject() for brain in brains] category_amounts = {} category_titles = {} category_levels = {} category_tops = {} for product in products: cm = ICategoryManagement(product) for category in cm.getTopLevelCategories(): # Traverse to top category object = category while ICategory.providedBy(object) == True: uid = object.UID() category_titles[uid] = object.Title() category_levels[uid] = len(object.getPhysicalPath()) if category_amounts.has_key(uid) == False: category_amounts[uid] = 0 category_amounts[uid] += 1 temp = object object = object.aq_inner.aq_parent if ICategory.providedBy(object) == False: category_tops[uid] = temp result = [] for uid, category in category_tops.items(): # Calculate children children = [] for child in category.objectValues("Category"): child_uid = child.UID() if child_uid in category_titles.keys(): # Title title = category_titles[child_uid] short_title = title if len(short_title)>13: short_title = short_title[:13] + "..." # Current if current_category_uid == child_uid: klass = "navTreeCurrentItem" else: klass = "" children.append({ "uid" : child_uid, "title" : title, "short_title" : short_title, "amount" : category_amounts[child_uid], "level" : category_levels[child_uid], "class" : klass, }) # Title title = category_titles[uid] short_title = title if len(short_title)>15: short_title = short_title[:15] + "..." # Current if current_category_uid == uid: klass = "navTreeCurrentItem" else: klass = "" result.append({ "uid" : uid, "title" : title, "short_title" : short_title, "amount" : category_amounts[uid], "level" : category_levels[uid], "children" : children, "class" : klass, }) return result
def testGetTopLevelCategories_2(self): """No categories there """ cm = ICategoryManagement(self.product_3) self.assertEqual(cm.getTopLevelCategories(), [])