def import_product_list(company_id, filename='/tmp/products/products.csv'): company = Company.load(company_id) default_campaign = company.default_campaign products = [] with open(filename) as f: products = f.readlines() products = [p.rstrip() for p in products[1:]] product_categories = {} for pline in products: log(pline) (product_name, category_id, pic) = pline.split(',') pic = pic.strip() key = '%s%s' % (product_name.strip(), category_id.strip()) cat = ProductCategory.load(category_id.strip(), False) prod = None if key in product_categories: prod = Product.load(product_categories[key][0], False) else: prod = Product() prod.company = company prod.name = product_name.strip() prod.type = 'Parent or Child' prod.save() prod.flush() product_categories[key] = [str(prod.product_id), str(cat.category_id)] ass = Asset() ass.fk_type = 'Product' ass.fk_id = prod.product_id ass.enterprise_id = company.enterprise_id ass.name = os.path.basename(pic) ass.extension = os.path.splitext(pic)[1] ass.save() ass.flush() storage_root = Asset.get_storage_root() if not storage_root: storage_root = '/apps/pvs/storage' fs_real_dir = "{root}/{reldir}".format(root=storage_root, reldir=ass.relative_dir) util.mkdir_p(fs_real_dir) fs_real_path = "{fs_real_dir}/{assid}{ext}".format(fs_real_dir=fs_real_dir, assid=ass.id, ext=ass.extension) shutil.copyfile(pic, fs_real_path) for pc in product_categories: pcat = product_categories[pc] cat = ProductCategory.load(pcat[1], False) cat.add_product(pcat[0]) db.commit()
def _edit_impl(self): product_id = self.request.matchdict.get('product_id') campaigns = Campaign.find_all(self.enterprise_id) companies = util.select_list(Company.find_all(self.enterprise_id), 'company_id', 'name') product_types = Product.get_types() vendors = util.select_list(Vendor.find_all(self.enterprise_id), 'vendor_id', 'name', True) categories = util.select_list(ProductCategory.find_all(self.enterprise_id), 'category_id', 'name', True) if product_id: product = Product.load(product_id) self.forbid_if(not product or product.company.enterprise_id != self.enterprise_id) product_categories = ProductCategory.find_by_product(product) else: product = Product() product_categories = [] self.forbid_if(self.request.ctx.user.is_vendor_user() and product.product_id and not self.request.ctx.user.vendor_id == product.vendor_id) children = product.get_children() other_products = product.find_eligible_children() non_children = [] for prod in other_products: found = False for kid in children: if kid.child_id == prod.product_id: found = True break if found == False: non_children.append(prod) return { 'product' : product, 'campaigns' : campaigns, 'companies' : companies, 'product_types' : product_types, 'vendors' : vendors, 'categories' : categories, 'product_categories' : product_categories, 'children' : children, 'non_children': non_children, 'other_products' : other_products, 'events' : util.select_list(StatusEvent.find_all_applicable(self.enterprise_id, product), 'event_id', 'display_name'), 'is_attribute' : self.request.GET.get('is_attribute') == 'True', 'parent_product' : Product.load(self.request.GET.get('parent_id')) if 'parent_id' in self.request.GET else None }
def test_category(self): ent = Enterprise.find_by_name('Healthy U Store') campaign = ent.companies[0].default_campaign categories = ProductCategory.find_by_campaign(campaign) assert len(categories) > 0 category = categories[0] products = category.get_web_products(campaign) R = self.get('/category/%s/%s/catalog_category' % (category.name, category.category_id)) assert R.status_int == 200 for prod in products: assert str(prod.product_id) in R.body
def _edit_impl(self): category_id = self.request.matchdict.get('category_id') companies = util.select_list(Company.find_all(self.enterprise_id), 'company_id', 'name') if category_id: category = ProductCategory.load(category_id) self.forbid_if(not category or category.company.enterprise_id != self.enterprise_id) else: category = ProductCategory() all_products = Product.find_all(self.enterprise_id) return {'companies' : companies, 'category' : category, 'all_products' : all_products}
def category(self): # /category/{category_id}/{page} page = self.request.matchdict.get("page", "category") category_id = util.to_uuid(self.request.matchdict.get("category_id")) category = ProductCategory.load(category_id) self.redir_if(not category) self.session["back_link"] = "/category/%s" % category_id params = self.params() params["products"] = util.page_list( SmartCatalog.category_product_list(self.request.ctx.campaign, category_id), self.request.GET.get("offset"), self.request.GET.get("limit"), ) params["category"] = category (params["seo_title"], params["seo_keywords"], params["seo_description"]) = SmartSeo.category_seo( category, self.request.ctx.site ) return self.render(page, params)
def save(self): #pylint: disable-msg=R0912,R0915 product_id = self.request.POST.get('product_id') if product_id: prod = Product.load(product_id) else: prod = Product() prod.bind(self.request.POST, True) prod.mod_dt = util.now() prod.save() self.db_flush() new_children = {} for k in self.request.POST.keys(): if k.startswith('campaign_price'): match = re.search(r'^.*\[(.*)\]', k) if match: campaign = Campaign.load(match.group(1)) price = self.request.POST.get(k) discount = self.request.POST.get('campaign_discount[%s]' % campaign.campaign_id) if price: price = util.float_(price) discount = util.float_(util.nvl(discount, 0.0)) prod.set_price(campaign, price, discount) else: prod.remove_price(campaign) if k.startswith('child_incl'): child_product_id = self.request.POST.get(k) child_product_quantity = self.request.POST.get('child_quantity_%s' % child_product_id) new_children[child_product_id] = child_product_quantity # KB: [2013-02-23]: Clear out old children that were just deselected and add the ones that are still selected. for current_child in prod.get_children(): if current_child.child_id not in new_children.keys(): prod.clear_child(current_child.child_id) for new_child_product_id in new_children.keys(): new_child_product_quantity = new_children[new_child_product_id] prod.add_child(new_child_product_id, new_child_product_quantity) prod.save() self.db_flush() redir_params = '' if 'parent_id' in self.request.POST and self.request.POST['parent_id']: parent = Product.load(self.request.POST['parent_id']) if not parent.has_child(prod.product_id): parent.add_child(prod.product_id) parent.save() redir_params = '?is_attribute=True&parent_id=%s' % parent.product_id inventory = str(self.request.POST.get('prod_inventory', '0')) if inventory and str(round(float(inventory), 2)) != str(round(util.nvl(InventoryJournal.total(prod), 0), 2)): InventoryJournal.create_new(prod, 'Inventory Adjust', inventory) self.db_flush() self.flash('Inventory Adjusted to %s' % inventory) prod.clear_attributes() for i in range(30): attr_name = self.request.POST.get('attr_name[%d]' % i) attr_value = self.request.POST.get('attr_value[%d]' % i) if attr_name and attr_value: prod.set_attr(attr_name, attr_value) category_id = self.request.POST.get('category_id') if category_id: category = ProductCategory.load(category_id) self.forbid_if(not category) category.add_product(prod.product_id) self.flash('Successfully saved %s.' % prod.name) return HTTPFound('/crm/product/edit/%s%s' % (prod.product_id, redir_params))
def category_list(campaign): return ProductCategory.find_ordered_list(campaign, 'revenue')
def category_product_list(campaign, category_id, offset=None, limit=None): category = ProductCategory.load(category_id) return util.page_list(category.get_web_products(campaign) if len(category.products) > 0 else [], offset, limit)
def save(self): pcat = ProductCategory.load(self.request.POST.get('category_id')) if not pcat: pcat = ProductCategory() pcat.bind(self.request.POST) pcat.mod_dt = util.now() pcat.save() pcat.flush() pcat.clear_products() pcat.flush() for k in self.request.POST.keys(): if k.startswith('child_incl'): child_product_id = self.request.POST.get(k) pcat.add_product(child_product_id) pcat.flush() pcat.invalidate_caches() self.request.session.flash('Successfully saved %s.' % pcat.name) return HTTPFound('/crm/product/category/edit/%s' % pcat.category_id)
def list(self): return {'categories' : ProductCategory.find_all(self.enterprise_id) }
def _delete_new(self, category_id): ProductCategory.full_delete(category_id) self.commit()