Пример #1
0
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()
Пример #2
0
    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))