def upload_picture(self): product_id = self.request.matchdict.get('product_id') product = Product.load(product_id) self.forbid_if(not product or product.company.enterprise_id != self.enterprise_id) ass = Asset.create_new(product, self.enterprise_id, self.request) self.flash('Uploaded new image to product') product.invalidate_caches() return str(ass.id)
def upload_asset(self): """ KB: [2011-03-23]: Take this file and hash its name up to put it in a sensible directory. """ listing_id = self.request.matchdict.get('listing_id') listing_hash = self.request.matchdict.get('hash') lis = Listing.load(listing_id) self.forbid_if(not lis or lis.hash != listing_hash) ass = Asset.create_new(lis, self.enterprise_id, self.request) Status.add(lis.customer, lis, Status.find_event(self.enterprise_id, lis, 'ASSET_UPLOAD'), ass.name) return str(ass.id)
def delete_picture(self): product_id = self.request.matchdict.get('product_id') product = Product.load(product_id) self.forbid_if(not product or product.company.enterprise_id != self.enterprise_id) asset_id = self.request.matchdict.get('asset_id') asset = Asset.load(asset_id) self.forbid_if(asset.fk_type != 'Product' or str(asset.fk_id) != str(product.product_id)) asset.delete() return 'True'
def file_save(self): site = Site.load(self.request.matchdict.get('site_id')) self.forbid_if(site.company.enterprise_id != self.enterprise_id) asset = Asset.create_new(site, self.enterprise_id, self.request) asset.bind(self.request.POST, True) asset.save() asset.flush() self.flash('Saved image %s' % asset.name) return HTTPFound('/cms/content/file/edit/%s/%s' % (site.site_id, asset.id))
def _test_upload_picture(self): product_id = self._create_new() # http://stackoverflow.com/questions/2488978/nose-tests-file-uploads files = [("Filedata", "testimage.jpg", "not really a jpg")] R = self.app.post('/crm/product/upload_picture/%s' % str(product_id), upload_files=files) assert R.status_int == 200 asset_id = R.body ass = Asset.load(asset_id) assert ass is not None assert os.path.exists(ass.filesystem_path) ass.delete() assert not os.path.exists(ass.filesystem_path) self._delete_new(product_id)
def _file_edit_impl(self): site = Site.load(self.request.matchdict.get('site_id')) self.forbid_if(site.company.enterprise_id != self.enterprise_id) asset_id = self.request.matchdict.get('asset_id') if asset_id: asset = Asset.load(asset_id) self.forbid_if(not asset or str(asset.enterprise_id) != str(self.enterprise_id)) else: asset = Asset() return { 'site' : site, 'asset' : asset }
def test_upload_asset(self): listing_id = self._create_new() # http://stackoverflow.com/questions/2488978/nose-tests-file-uploads listing = Listing.load(listing_id) assert listing is not None files = [("Filedata", "testfile.txt", "testfile.txt contents")] R = self.app.post('/crm/listing/upload/%s/%s' % (listing_id, listing.hash), upload_files=files) assert R.status_int == 200 asset_id = R.body ass = Asset.load(asset_id) assert ass.web_path is not None assert str(ass.id) in ass.web_path assert ass.exists assert ass is not None assert ass.get_listing().listing_id == listing.listing_id listings = Listing.find_all_pending_approval(self.site.company.enterprise_id) assert len(listings) > 0 assert listings[0].listing_id == listing.listing_id self._delete_new(listing_id)
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 file_list(self): site = Site.load(self.request.matchdict.get('site_id')) self.forbid_if(site.company.enterprise_id != self.enterprise_id) return { 'contents' : Asset.find_for_object(site) }
def assets(self): return Asset.find_for_object(self)