def create(): try: title = request.form.get("title") description = request.form.get("description") file = request.files.get("product_img") if file and title and description: s3.upload_fileobj(file, "krishnaybucket", file.filename, ExtraArgs={ "ACL": "public-read", "ContentType": file.content_type }) user = User.get_or_none(User.id == current_user.id) Product.create( image_path= f"http://krishnaybucket.s3.amazonaws.com/{file.filename}", user=user, description=description, title=title) return redirect(url_for("home")) else: flash("Please complete every field", "warning") return redirect(url_for("product.new")) except Exception as e: flash("Something went wrong.Please try again.", "warning") print("Something Happened: ", e) return redirect(url_for("product.new"))
def load_products(): products = get_products_csv() bar = Bar('Load products', max=len(products)) for product in products: Product.create( link_e=product.get('link-e'), link_g=product.get('link-g'), group = product.get('group'), ) bar.next() bar.finish()
def create_product(): try: Product.create(name=name_entry.get().upper(), reference=reference_entry.get().upper(), brand=brand_entry.get().upper(), code=code_entry.get(), price=float(price_entry.get()), stock=int(stock_entry.get())) detail_product_window.destroy() except Exception as err: messagebox.showerror("Error", err, parent=detail_product_window)
def _update_products(self): counter = 0 updated_prices = 0 update_names = 0 created_products = 0 for key, value in self.df.iterrows(): if counter == 0: counter += 1 continue code = value[2] if str(code) != 'nan': brand = value[0] refence = value[1] price = format_float(value[5]) name = value[3].replace("Ð", "Ñ").replace("║", "|") try: product = Product.get(code=code) except Exception: Product.create(brand=brand, reference=refence, code=code, name=name, price=price) counter += 1 created_products += 1 continue if product.name != name: product.name = name product.save() update_names += 1 if es_casi_igual(product.price, price): pass else: # Actualizar precio. product.price = price product.save() updated_prices += 1 counter += 1 if updated_prices or update_names or created_products: messagebox.showinfo( "Precios Actualizados!", f"{updated_prices} precios actualizados, {update_names} nombre modificados, {created_products} productos nuevos." )
def product_create(request, values): shop = Shop.get(slug=values.get('slug')) if shop is None: raise NotFound() if request.method == 'GET': categories = Category.all() return render_template('product-form.html', { 'categories': categories, 'shop': shop }) name = request.form.get('name') price = request.form.get('price') description = request.form.get('description') categories = request.form.getlist('category') image = request.files.get('image') if image and allowed_image(image.filename): image_name = secure_filename(image.filename) image.save(os.path.join(MEDIA_ROOT, image_name)) else: image_name = '' product = Product.create(name=name, price=price, image=image_name, shop_id=shop.id, description=description) for category in categories: product.add_category(category) return redirect(f'/shops/{shop.slug}/products/{product.id}')
def add_product_to_catalog(user_id, product_name): product = Product.create( product_name=product_name, description="", price_per_unit=100.0, number_in_stock=1, ) UserProduct.create(user_id=user_id, product_id=product) return product
def create(name, category, price): user = user_session.current_user() category = category_controller.get(category) product = Product.create(name=name, user=user, price=price, category=category) product.save() return f'Product {product.name} created!'
def create(product): id = product.get('id') if product.get('id') else str(uuid.uuid4()) if not validate_uuid(id, 4): return uuid_notvalidate(obj, "id") while True: existing_item = Product.query.filter(Product.id == id).one_or_none() if existing_item is None: product['id'] = id break else: id = str(uuid.uuid4()) item = Product.create(**product) return jsonify(item.to_dict()), 201
def web_scrape(): for u in URLS: if u['type'] == 'winwin': url = u['url'] headers = {'User-Agent': USER_AGENT} response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, "html.parser") category = soup.find('div', attrs={'class': 'category-title'}) category_name = category.text.replace('/', '-').replace('\u0161', 's').replace('\u010d', 'c').replace('\u017e', 'z').strip() cat = Category.get_category_by_name(category_name) if cat: pass else: cat = Category.create(name=category_name) for li in soup.findAll('li', attrs={'class': 'item'}): title = li.find('span', attrs={'itemprop': 'name'}) comments = li.find('div', attrs={'class': 'ratings'}).find('span', attrs={'class': 'text-info'}) if comments: count = int(comments.text) else: count = 0 special_price = li.find('p', attrs={'class': 'special-price'}) if special_price: price = special_price.find('span', attrs={'class': 'price'}) else: price = li.find('span', attrs={'class': 'price'}) url = li.find('a', href=True, attrs={'class': 'product-image'}) model = title.text.replace('/', '-').replace(' ', '-').replace(',', '-').replace('*', '-').replace('.', '-').replace('"', '-').replace('\xa0', '-').replace('\u0161', 's').replace('\u010d', 'c').replace('\u017e', 'z') product = Product.get_product_by_title(model) if product: pass else: Product.create(title=model, comments=count, price=int(price.text[:-4].replace('.', "")), url=url.get('href'), category=cat)
def insert_products(): Product.create(store_id=1, name='Iniki', description='Tennis Iniki', price=100, stock=5) Product.create(store_id=1, name='Buzenits', description='Tennis Buzenits', price=100, stock=5) Product.create(store_id=1, name='Continental', description='Tennis Continental', price=100, stock=5) Product.create(store_id=2, name='Air', description='Tennis Air', price=100, stock=5) Product.create(store_id=2, name='Jordan', description='Tennis Jordan', price=100, stock=5) Product.create(store_id=2, name='Venom', description='Tennis Venom', price=100, stock=5)
def insert_products(): Product.create(store_id=1, name='Pan', description='Pan integral', price=5.5, stock=50) Product.create(store_id=1, name='Leche', description='Baja en gradas', price=15.5, stock=100) Product.create(store_id=1, name='Jamon', description='de pavo', price=30.9, stock=80) Product.create(store_id=1, name='mayonesa', description='mayonesa', price=30.9, stock=80) Product.create(store_id=2, name='Soda', description='Dieta', price=10.0, stock=50) Product.create(store_id=2, name='Fritura', description='Frituras de papa', price=20.5, stock=100) Product.create(store_id=2, name='Salsa', description='chile habanero', price=29, stock=80) Product.create(store_id=2, name='Mostaza', description='Mostaza', price=30.9, stock=80)
import csv from models import Product def format_float(float_number): if int(float_number) == 0: return float("{:10.4f}".format(float_number).lstrip(" ")) elif int(float_number) == 1: return float("{:10.3f}".format(float_number).lstrip(" ")) return float("{:10.2f}".format(float_number).lstrip(" ")) with open("products.csv", encoding="utf8", mode="r") as file: reader = csv.reader(file) for row in reader: if (row[1] == 'Unnamed: 0') or (row[1] == 'MARCA'): continue Product.create( brand = row[1], reference = row[2], code = row[3], name = row[4].replace("Ð", "Ñ").replace("║", "|"), price = format_float(float(row[5])) )
def insert_products(): Product.create(store_id=1, name='Pan', description='Pan Integral', price=5.5, stock=2) Product.create(store_id=1, name='Leche', description='Baja en grasas', price=15.5, stock=10) Product.create(store_id=1, name='Jamon', description='de pavo', price=30.90, stock=6) Product.create(store_id=2, name='Soda', description='Dieta', price=1.20, stock=2) Product.create(store_id=2, name='Fritura', description='Frituras de papa', price=7.90, stock=20) Product.create(store_id=2, name='Salda', description='Chile habanero', price=29.30, stock=4)
def insert_data(table_name): if table_name == 'users': user = User.create(username='******', age=30, password='******', email='*****@*****.**') user = User.create(username='******', age=35, password='******', email='*****@*****.**') user = User.create(username='******', age=60, password='******', email='*****@*****.**') user = User.create(username='******', age=68, password='******', email='*****@*****.**') user = User.create(username='******', age=23, password='******', email='*****@*****.**') user.save() elif table_name == 'stores': store = Store.create(user_id=1, name='La esquina', address='Fuentes del Molino #10') store = Store.create(user_id=1, name='Don Simon', address='Fuentes del Molino #12') store = Store.create(user_id=1, name='Carmelita', address='Fuentes del Molino #13') store = Store.create(user_id=4, name='Wallis', address='Fuentes del Molino #14') store = Store.create(user_id=5, name='Don perse', address='Fuentes del Molino #16') store.save() elif table_name == 'products': product = Product.create(store_id=1, name='Pan', description='Pan Integral', price=5.5, stock=10) product = Product.create(store_id=1, name='Leche', description='Baja en grasas', price=15.5, stock=24) product = Product.create(store_id=1, name='Jamon', description='Pavo', price=45.5, stock=10) product = Product.create(store_id=2, name='Soda', description='Dieta', price=10.5, stock=10) product = Product.create(store_id=2, name='Fritura', description='Churros', price=9.5, stock=10) product = Product.create(store_id=2, name='Salsa', description='Habanero', price=11.5, stock=10) product.save() elif table_name == 'categories': category = Category.create(name='Liquidos', description='liquidos') category = Category.create(name='Embutidos', description='embutidos') category = Category.create(name='Snacks', description='snacks') category = Category.create(name='Aderezos', description='aderezos') category = Category.create(name='Carnes', description='carnes') category.save() elif table_name == 'product_categories': ProductCategory.create(category_id=1, product_id=2) ProductCategory.create(category_id=1, product_id=4) ProductCategory.create(category_id=2, product_id=3) ProductCategory.create(category_id=3, product_id=5) ProductCategory.create(category_id=4, product_id=6) ProductCategory.create(category_id=5, product_id=3)
def insert_products(): Product.create(store_id=1, name='Supercan de 20 kilos', description='Alimento para perros', price='20', stock='20') Product.create(store_id=1, name='Sextuple', description='Vacuna para perros', price='5', stock='10') Product.create(store_id=1, name='Hueso de goma', description='Juguete para perros', price='2', stock='15') Product.create(store_id=1, name='Paté Happydog', description='Enlatado para perros', price='3', stock='8') Product.create(store_id=2, name='Supercat de 20 kilos', description='Alimento para gatos', price='22', stock='20') Product.create(store_id=2, name='Triple felina', description='Vacuna para gatos', price='5', stock='12') Product.create(store_id=2, name='Ratón de goma', description='Juguete para gatos', price='2', stock='8') Product.create(store_id=2, name='Paté Ricocat', description='Enlatado para gatos', price='3', stock='25')
def extract_product_detail(url, doc, res=''): ''' desc: 通过详情页获取一系列信息,入库 ''' if not doc.xpath('//div[@class="breadcrumb"]'): logger.info('extract_product_detail --- url %s %s' % (url, u'全球购不处理!!!')) return if doc.xpath('//div[@class="breadcrumb"]//a/text()')[0] == u"首页": logger.info('extract_product_detail --- url %s %s' % (url, u'闪购页面暂时不处理!!!')) return _this_dao = Product.select().where(Product.purl == url).first() if _this_dao: logger.info('extract_product_detail --- product %s exist' % (url)) return # pid pid = re.search('http://item.jd.com/(?P<id>\d*).html', url).groupdict()['id'] # product brand brand = doc.xpath('//*[@id="parameter-brand"]/li/a[1]')[0].text_content() # same detail page not contains brand img ,so set null _brand_img = doc.xpath('//*[@id="extInfo"]/div[1]/a/img/@src') if _brand_img: brand_img = _brand_img[0] brand_img = perfect_href(brand_img) else: brand_img = '' # product img imgs = doc.xpath('//div[@class="spec-items"]/ul/li/img/@src') fix_img = lambda x: re.sub('/n5/', '/imgzone/', "http:" + x) imgs = map(fix_img, imgs) img_first = imgs.pop(0) # pname pname = doc.xpath('//div[@id="product-intro"]//div[@id="itemInfo"]//h1' )[0].text_content() # 价格 _price_url = "http://p.3.cn/prices/get?skuid=J_{pid}" price = None _price_res = fetch(_price_url.format(pid=pid)) if _price_res.status_code == 200: price = json.loads(_price_res.text)[0]['p'] else: raise ("Not Parse Price") # 面包屑 == category _cat_body = parse_category(url, doc, res) if not ProductAndCategory.select().where( ProductAndCategory.pid == pid).first(): _cat_body.update({'pid': int(pid)}) ProductAndCategory.create(**_cat_body) data = { 'pid': pid, 'purl': url, 'pname': pname.encode('utf-8'), 'brand': brand.encode('utf-8'), 'brand_img': brand_img, 'product_img': img_first, 'price': price, 'extra': json.dumps({'img': imgs}) } try: Product.create(**data) logger.info('product success save--- url: %s' % (url)) except IntegrityError: logger.info('product faild repeat --- url: %s' % (url)) except Exception, e: ex = traceback.format_exc() logger.error('product faild exception --- url: %s\n %s' % (url, ex))
def insert_item(item_name, item_price): Product.create(name=item_name, price=item_price)
def insert(self, item): product = Product.create(item) product.put()
return False def format_id(product): """ Turns the product id into an integer """ return int(product['id']) def format_name(product): """ Ran into some weird encoding problem, tried to fix it by ensuring correct encoding. But really, ascii in a webservice? """ return product['name'].encode('utf-8') try: resp = urllib2.urlopen('http://www.unisport.dk/api/sample/') json_data = json.loads(resp.read()) for product in json_data['products']: Product.create(name=format_name(product), price=format_price(product), for_kids=kids_true(product), product_id=format_id(product)) except urllib2.URLError, e: logging.warn(e)
def insert_bulk(self, items): to_save = [] for item in items: product = Product.create(item) to_save.append(product) ndb.put_multi(to_save)