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 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))