def setCategories(self, categories): """ """ # Set the new values reference_catalog = getToolByName(self, "reference_catalog") # save the old categories old_categories = self.getCategories() # delete the product from old categories for category in old_categories: reference_catalog.deleteReference(category, self, "categories_products") for category in categories: reference_catalog.addReference(category, self, "categories_products") # Reindex to get the new values ... self.reindexObject() # ... here. Now reindex all categories of this product and all parent # categories of them. # Todo: Reindex categories which are kept only once. for category in old_categories: obj = category while ICategory.providedBy(obj): obj.reindexObject() obj = obj.aq_inner.aq_parent for category in self.getCategories(): obj = category while ICategory.providedBy(obj): obj.reindexObject() obj = obj.aq_inner.aq_parent
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 _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 getParentCategory(object, portal, **kwargs): try: if ICategory.providedBy(object): parent_category = object.getParentCategory() if parent_category is not None: return parent_category.UID() else: return None except (ComponentLookupError, TypeError, ValueError): raise AttributeError
def getBackToOverViewUrl(self): """ """ parent = self.context.aq_inner.aq_parent if ICategory.providedBy(parent): parent_url = parent.absolute_url() elif ICategoriesContainer.providedBy(parent): shop = IShopManagement(self.context).getShop() parent_url = shop.absolute_url() else: parent_url = None return parent_url
def amount_of_categories(object, portal, **kwargs): try: # This has to be done without the help of the catalog. Otherwise it # counts before all to counted objects are in the catalog. That is at # least the case for Advanced/Update Catalog. counter = 0 if ICategory.providedBy(object): counter = len(object.objectValues("Category")) return counter except (ComponentLookupError, TypeError, ValueError): raise AttributeError
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 getInfo(self): """ """ batch = self._getBatch() # This optimized for speed, as we need _getBatch here anyway. # So there is no need of an extra method call within the page # template to get informations we have here already. Same is true # for format infos, see below parent = self.context.aq_inner.aq_parent if ICategory.providedBy(parent): parent_url = parent.absolute_url() elif ICategoriesContainer.providedBy(parent): shop = IShopManagement(self.context).getShop() parent_url = shop.absolute_url() else: parent_url = None batch_infos = { "parent_url" : parent_url, "first_url" : self._getFirstUrl(batch), "previous_url" : self._getPreviousUrl(batch), "previous" : batch.previous, "next_url" : self._getNextUrl(batch), "next" : batch.next, "last_url" : self._getLastUrl(batch), "navigation_list" : batch.navlist, "number_of_pages" : batch.numpages, "page_number" : batch.pagenumber, "amount" : batch.sequence_length, } sorting = self.request.SESSION.get("sorting") f = self.getFormatInfo() products_per_line = f["products_per_line"] line = [] products = [] for index, product in enumerate(batch): # Price cm = ICurrencyManagement(self.context) p = IPrices(product) # Effective price price = p.getPriceForCustomer() price = cm.priceToString(price, symbol="symbol", position="before") # Standard price standard_price = p.getPriceForCustomer(effective=False) standard_price = cm.priceToString(standard_price, symbol="symbol", position="before") # Image image = IImageManagement(product).getMainImage() if image is not None: image = "%s/image_%s" % (image.absolute_url(), f.get("image_size")) # Text temp = f.get("text") if temp == "description": text = product.getDescription() elif temp == "short_text": text = product.getShortText() elif temp == "text": text = product.getText() else: text = "" # Title temp = f.get("title") if temp == "title": title = product.Title() elif temp == "short_title": title = product.getShortTitle() try: chars = int(f.get("chars")) except (TypeError, ValueError): chars = 0 if (chars != 0) and (len(title) > chars): title = title[:chars] title += "..." # CSS Class if (index + 1) % products_per_line == 0: klass = "last" else: klass = "notlast" line.append({ "title" : title, "text" : text, "url" : product.absolute_url(), "image" : image, "for_sale" : product.getForSale(), "price" : price, "standard_price" : standard_price, "class" : klass, }) if (index + 1) % products_per_line == 0: products.append(line) line = [] # the rest if len(line) > 0: products.append(line) # Return format infos here, because we need it anyway in this method # This is for speed reasons. See above. return { "products" : products, "batch_info" : batch_infos, "format_info" : f, }
def getInfo(self): """ """ batch = self._getBatch() # This optimized for speed, as we need _getBatch here anyway. # So there is no need of an extra method call within the page # template to get informations we have here already. Same is true # for format infos, see below parent = self.context.aq_inner.aq_parent if ICategory.providedBy(parent): parent_url = parent.absolute_url() elif ICategoriesContainer.providedBy(parent): shop = IShopManagement(self.context).getShop() parent_url = shop.absolute_url() else: parent_url = None batch_infos = { "parent_url": parent_url, "first_url": self._getFirstUrl(batch), "previous_url": self._getPreviousUrl(batch), "previous": batch.previous, "next_url": self._getNextUrl(batch), "next": batch.next, "last_url": self._getLastUrl(batch), "navigation_list": batch.navlist, "number_of_pages": batch.numpages, "page_number": batch.pagenumber, "amount": batch.sequence_length, } sorting = self.request.SESSION.get("sorting") f = self.getFormatInfo() products_per_line = f["products_per_line"] line = [] products = [] for index, product in enumerate(batch): # Price cm = ICurrencyManagement(self.context) p = IPrices(product) # Effective price price = p.getPriceForCustomer() price = cm.priceToString(price, symbol="symbol", position="before") # Standard price standard_price = p.getPriceForCustomer(effective=False) standard_price = cm.priceToString(standard_price, symbol="symbol", position="before") # Image image = IImageManagement(product).getMainImage() if image is not None: image = "%s/image_%s" % (image.absolute_url(), f.get("image_size")) # Text temp = f.get("text") if temp == "description": text = product.getDescription() elif temp == "short_text": text = product.getShortText() elif temp == "text": text = product.getText() else: text = "" # Title temp = f.get("title") if temp == "title": title = product.Title() elif temp == "short_title": title = product.getShortTitle() try: chars = int(f.get("chars")) except (TypeError, ValueError): chars = 0 if (chars != 0) and (len(title) > chars): title = title[:chars] title += "..." # CSS Class if (index + 1) % products_per_line == 0: klass = "last" else: klass = "notlast" line.append({ "title": title, "text": text, "url": product.absolute_url(), "image": image, "for_sale": product.getForSale(), "price": price, "standard_price": standard_price, "class": klass, }) if (index + 1) % products_per_line == 0: products.append(line) line = [] # the rest if len(line) > 0: products.append(line) # Return format infos here, because we need it anyway in this method # This is for speed reasons. See above. return { "products": products, "batch_info": batch_infos, "format_info": f, }