def process_product_add(): if not logged_in(): return redirect(url_for('login', next='/admin/products/')) # username in session and admin role, continue if request.method != 'POST': # return to products.html page containing add form. Only POST method is allowed error = 'Please use the form to add new products' return render_template('products.html', title="ADMINISTER PRODUCTS", information=error, css="error") # No problem so far, get the request object and the parameters sent from the form. name = request.form['name'] code = request.form['code'] description = request.form['description'] price_per_unit = request.form['price_per_unit'] product_inception_date = request.form['product_inception_date'] # let's write to the database try: product = models.Product(name=name, code=code, description=description, price_per_unit=price_per_unit, product_inception_date=product_inception_date) models.db.session.add(product) models.db.session.commit() except Exception as e: error = 'Could not submit. The error message is {}'.format(e.__cause__) return render_template('products.html', title="ADMINISTER PRODUCTS", information=error, css="error") # no error, continue return redirect(url_for('admin_page.products', information="Add successful", css="success"))
async def create_product(product: ProductCreate, session: Session = Depends(get_session)): new_product = models.Product(name=product.name, price=product.price) session.add(new_product) session.commit() session.refresh(new_product) return new_product
def post_create(): name = None seller = None price = None if request.method == 'POST': name = request.form['name'] seller = request.form['seller'] price = request.form['price'] elif request.method == 'GET': name = request.args.get('name') seller = request.args.get('seller') price = request.args.get('price') item = models.Product() item.name = name item.seller = seller item.price = price models.db.session.add(item) models.db.session.commit() response = jsonify({'message': 'Product added', 'product': item.to_json()}) return response
def add_new_product(shop_id): shop = db.query(models.Shop).filter(models.Shop.id == shop_id).first() p1 = models.Product() p1.name = input('Unesite ime product-a: ') p1.typee = input('Unesite tip product-a: ') p1.price = float(input('Unesite cenu product-a: ')) p1.shop_id = shop_id if p1.typee in ('medicine', 'parking ticket'): p1.serial_number = input('Unesite serial number product-a: ') if shop.typee == "pharmacy" and p1.typee == "medicine": db.add(p1) db.commit() db.refresh(p1) db.close() print('Uspesno ste dodali medicine product') elif shop.typee != "pharmacy" and p1.typee == "medicine": print('Doslo je do greske pri upisivanju u bazi') elif shop.typee == "corner shop" and p1.typee == "cigarettes": db.add(p1) db.commit() db.refresh(p1) db.close() elif shop.typee != "corner shop" and p1.typee == "cigarettes": print('Doslo je do greske pri upisivanju u bazi') else: db.add(p1) db.commit() db.refresh(p1) db.close() add_new_storage(p1.id)
def save_to_db(db_file, store): engine = create_engine('sqlite:///{}'.format(db_file)) Session = sessionmaker(bind=engine) # Initialize DB if needed if not os.path.isfile(db_file): models.Base.metadata.create_all(engine) # Save to database session = Session() # Select/save store db_store = get_or_create(session, models.Store, name=store.name) store_id = db_store.id # Process the gathered data for overview_page in tqdm(store.overview_pages): # Save category hierarchy category_parent_id = None for category in overview_page['product_category']: # Check if exists... res = session.query(models.Category).filter( models.Category.name == category).filter( models.Category.parent_id == category_parent_id).first() # ... else create new category if res: category_parent_id = res.id else: new_category = models.Category(name=category, parent_id=category_parent_id) session.add(new_category) session.commit() category_parent_id = new_category.id # Save products for position, product_url in enumerate(overview_page['product_urls']): if product_url in store.product_pages.keys(): product = store.product_pages[product_url] # Select/save brand db_brand = get_or_create(session, models.Brand, name=product['brand']) brand_id = db_brand.id # Save product new_product = models.Product( name=product['product_name'], type=product['product_type'], price=product['price'], page_position=position + 1, # From 0-based to 1-based page_number=overview_page['page_number'], category_id=category_parent_id, brand_id=brand_id, store_id=store_id) session.add(new_product) session.commit() session.close()
def transform(dictionary): if (dictionary is None): raise Exception('Invalid Product Details Specified!') product = models.Product(dictionary['productId'], dictionary['title'], dictionary['qtyInStock'], dictionary['unitPrice']) return product
def admin_product_new(): p = db.Product() p.title = "Untitled" p.price = 10000 p.image_url = "" p.status = db.Product.DRAFT p.save() return x.redirect("/admin/product/%s" % p.id)
def create_product(product: schemas.ProductCreate, db: Session = Depends(get_db)): """ Create product with specified params See params in schemas.ProductCreate """ product = models.Product(**product.dict()) db.add(product) db.commit() db.refresh(product) return product
def test(): breads = [ 'White', 'Wheat', 'Pumpernickel', 'Rye', 'Homemade', 'Cinnamon Raisin', 'Grain' ] butters = ['Almond', 'Cashew', "Peanut"] # 2 loops , 1 per list, create document for db for b in breads: tmpBread = models.Product(shelf="bread", name=b) tmpBread.save() for b in butters: tmpButter = models.Product(shelf="butter", name=b) tmpButter.save() return "ok"
def upload_file(products): statuses = ['active', 'inactive'] for product in products: status = random.choice(statuses) prod = models.Product(sku=product['sku'], name=product['name'], description=product['description'], status=status) session.merge(prod) session.commit() return 'success'
def create(product: schemas.Product, db: Session, user: schemas.LoginUser) -> schemas.Product: novo_product = models.Product(title=product.title, description=product.description, link=product.link, image=product.image, user_id=user.id, buy=False) db.add(novo_product) db.commit() db.refresh(novo_product) return novo_product
def create_product(): data = request.get_json() if data and data.get('name') and data.get('price'): product = models.Product(public_id=str(uuid.uuid4()), name=data['name'], price=data['price']) db.session.add(product) db.session.commit() return jsonify({'message': 'Product successfully created!', 'product': product.serialize()}), 201 return jsonify({'message': 'All parameter are required!'}), 502
def newProduct(): if request.method == "POST": product = models.Product(brand=request.form['brand'], category=request.form['category'], name=request.form['name'], imgsrc=request.form['imgsrc'], description=request.form['description']) db.session.add(product) db.sesion.commit() db.session.refresh(product) return redirect('/product/' + str(product.id)) return render_template('newProduct.html')
def main(): # Init database models.init() print('') # Fill with Test Data models.test_data() print('') # Search search('sweater') print('') # List John's Products list_user_products(2) print('') # List all products tagged with 'knitted' list_products_per_tag(1) print('') # John now starts selling olive oil! product = models.Product(name='Olive Oil', description='Fresh olive oil from the farm', price=6.50, quantity=10) add_product_to_catalog(2, product) print('') # List John's Products again list_user_products(2) print('') # Now there's only 5 steaks left in the webshop update_stock(5, 5) print('') # Let's make a transaction purchase_product(1, 2, 2) print('') # And remove a product remove_product(6) print('') # Search for something old fashioned search('grandma') print('')
def db_start(): create_engine('sqlite:///tmp/testapi.db', convert_unicode=True) db.create_all() db.session.commit() product_1 = models.Product() product_1.name = 'Computer' product_1.description = 'Very big comupter' product_1.category = 'Personal computer' db.session.add(product_1) db.session.commit() review_1 = models.Review() review_1.author = 'Johny' review_1.text = 'Very nice computer' review_1.rating = 5 review_1.product_id = 1 db.session.add(review_1) db.session.commit()
def products(): if request.method == "GET": branch = models.Branch.query.filter_by( id=request.args.get("branch_id")).first() if branch: return make_response( jsonify( list( map(lambda product: product.serialize(), branch.products))), 200) else: return "Branch not found" elif request.method == "POST": # validate form product = models.Product(name="One", quantity=500, price=12.99, description="yes") db.session.add(product) db.session.commit() return make_response(jsonify(product.serialize()), 200)
def process_product(self, item): item = self.preprocessor.preprocess_product(item) if item['id'] in self.item_set: raise DropItem("Duplicate product") self.item_set.add(item['id']) db_item = item.copy() status = db_item['status'] if status == 'Available': db_item['status'] = models.ItemStatusEnum.AVAILABLE elif status == "Out Of Stock": db_item['status'] = models.ItemStatusEnum.OUTOFSTOCK elif status == "Discontinued": db_item['status'] = models.ItemStatusEnum.DISCONTINUED elif status == "Pre Order": db_item['status'] = models.ItemStatusEnum.PREORDER elif status == "Up Coming": db_item['status'] = models.ItemStatusEnum.UPCOMING elif status == "Call for Price": db_item['status'] = models.ItemStatusEnum.CALLFORPRICE else: # TODO: modify logger's dropped method to be more user friendly. raise DropItem("Item {} has unknown status {}".format( db_item['id'], status)) db_item['brand'] = self.get_set_brand(item['brand']) db_item['platform'] = self.platform db_item['specifications'] = [ models.Specification(key=spec[0], value=spec[1]) for spec in item['specifications'].items() ] Pipeline.db.add(models.Product(**db_item)) return item
def buildProductBatch(cls, rows): """Build product documents and their related datastore entities, in batch, given a list of params dicts. Should be used for new products, as does not handle updates of existing product entities. This method does not require that the doc ids be tied to the product ids, and obtains the doc ids from the results of the document add.""" docs = [] dbps = [] for row in rows: try: params = cls._normalizeParams(row) doc = cls._createDocument(**params) docs.append(doc) # create product entity, sans doc_id dbp = models.Product(id=params['pid'], price=params['price'], category=params['category']) dbps.append(dbp) except errors.OperationFailedError: logging.error('error creating document from data: %s', row) try: add_results = cls.add(docs) except search.Error: logging.exception('Add failed') return if len(add_results) != len(dbps): # this case should not be reached; if there was an issue, # search.Error should have been thrown, above. raise errors.OperationFailedError( 'Error: wrong number of results returned from indexing operation' ) # now set the entities with the doc ids, the list of which are returned in # the same order as the list of docs given to the indexers for i, dbp in enumerate(dbps): dbp.doc_id = add_results[i].id # persist the entities ndb.put_multi(dbps)
for i in categories: models.Category(title=i, description=f'{i} directory description',).save() for j in subcategories[categories.index(i)]: subcut = models.Category(title=j, description=f'{j} directory description',).save() models.Category.objects(title=i).get().add_subcategory(subcut) # for i in product: for j, m in zip(subcategories[product.index(i)], i): for y in m: product_price = randint(1000, 10000) discount_price = product_price/2 if m.index(y) % 2 == 0: models.Product(title=y, description=f'{y} directory description', price=product_price, new_price=product_price, category=models.Category.objects(title=j).get()).save() else: models.Product(title=y, description=f'{y} directory description', price=product_price, new_price=discount_price, is_discount=True, category=models.Category.objects(title=j).get()).save() for product in models.Product.objects: p_img = open('1.jpg', 'rb') product.p_img.put(p_img, content_type='image/jpeg') product.save() news = ['Осенняя распродажа', 'Черная пятница', 'Сезонные скидки']
images=json.dumps(images), proper_price=proper_price, sales_price=sales_price, source_url=url, source='base-komai0526', ) categories = get_categories() for category in categories: print(category.name) resp = requests.get(category.source_url) soup = BeautifulSoup(resp.content, 'html.parser') product_links = soup.select('li.itemListBox > a') for link in product_links: url = link.get('href') product_id = url.split('/')[-1] product = session.query(models.Product).filter( models.Product.product_id == product_id).first() if not product: product_params = get_product(url) product = models.Product(category_id=category.id, **product_params) product = get_or_create(session, models.Product, product, 'product_id') # with open('{}.csv'.format(category_name), 'w+') as f: # writer = csv.DictWriter(f, header) # writer.writeheader() # for row in products: # writer.writerow(row)
def process_product_add(): if not logged_in(): return redirect(url_for('login', next='/admin/products/')) # username in session and admin role, continue if request.method != 'POST': # return to products.html page containing add form. Only POST method is allowed error = 'Please use the form to add new products' return render_template('products.html', title="ADMINISTER PRODUCTS", information=error, css="error") # No problem so far, get the request object and the parameters sent from the form. name = request.form['name'] code = request.form['code'] description = request.form['description'] price_per_unit = request.form['price_per_unit'] product_inception_date = request.form['product_inception_date'] # let's write to the database try: product = models.Product(name=name, code=code, description=description, price_per_unit=price_per_unit,product_inception_date=product_inception_date) models.db.session.add(product) models.db.session.commit() except Exception as e: error = 'Could not submit. The error message is {}'.format(e.__cause__) return render_template('products.html', title="ADMINISTER PRODUCTS", information=error, css="error") # no error, continue return redirect(url_for('admin_page.products', information="Add successful", css="success") @admin_page.route("/admin/products/edit/<int:id>/", methods=['POST','GET']) def product_edit(id): # check database for the product to edit product = models.Product.query.filter_by(id=id).first() # send to the edit form return render_template('product-edit.html', product=product) @admin_page.route("/admin/products/process-product-edit/<int:id>/", methods=['POST', 'GET']) def process_product_edit(id): if not logged_in(): return redirect(url_for('login', next='/admin/products/')) # username in session and admin in role, continue if request.method != 'POST': # redirect to signup form. Only POST method is allowed error = 'Please use the form to edit products' return render_template('products.html', title="ADMINISTER PRODUCTS", information=error, css="error") # No problem so far, get the request object and the parameters sent. name = request.form['name'] code = request.form['code'] description = request.form['description'] price_per_unit = request.form['price_per_unit'] product_inception_date = request.form['product_inception_date'] # let's update the database try: # Get the existing data from database as object product = models.Product.query.filter_by(id=id).first() # Update the fields product.name = name product.code = code product.description = description product.price_per_unit = price_per_unit product.product_inception_date = product_inception_date # commit models.db.session.commit() except Exception as e: error = 'Could not update product. The error message is {}'.format(e.__cause__) return redirect(url_for('admin_page.products', information="Update not successful", css="error")) return redirect(url_for('admin_page.products', information="Update successful", css="success")) @admin_page.route("/admin/products/delete/<int:id>/", methods=['POST', 'GET']) def product_delete(id): if not logged_in(): return redirect(url_for('login', next='/admin/products/')) # username in session and admin role, continue # No problem so far # let's update the database try: # Get the existing data from database as object product = models.Product.query.filter_by(id=id).first() # Delete the record models.db.session.delete(product) # commit models.db.session.commit() except Exception as e: error = 'Could not delete product. The error message is {}'.format(e.__cause__) return redirect(url_for('admin_page.products', information="Delete not successful", css="error")) return redirect(url_for('admin_page.products', information="Delete successful", css="success"))
customer = models.Customer(public_id=str(uuid.uuid4()), name='Rik Morty', address='Ukraine, Kyiv, Soloma str., 234', phone='+380989694355') customer1 = models.Customer(public_id=str(uuid.uuid4()), name='Erik Kirk', address='USA, NY, Blabla str., 123d', phone='+190989694355') customer2 = models.Customer(public_id=str(uuid.uuid4()), name='Megan Fox', address='England, Barber str., 24a', phone='+280989694355') db.session.add(customer) db.session.add(customer1) db.session.add(customer2) db.session.commit() product = models.Product(public_id=str(uuid.uuid4()), name='Telephone', price=123) product1 = models.Product(public_id=str(uuid.uuid4()), name='TV', price=466) product2 = models.Product(public_id=str(uuid.uuid4()), name='MacBook', price=1200) db.session.add(product) db.session.add(product1) db.session.add(product2) db.session.commit()
def index(): errors = [] if request.method == 'POST': f = request.files['file'] filename = secure_filename(f.filename) f.save(filename) with open(filename, 'r') as fp: content = fp.readlines() content = [line.strip().split('\t') for line in content] for line in content: # map columns to names customer_id = int(line[0]) first_name = line[1] last_name = line[2] address = line[3] state = line[4] zip_code = line[5] status = line[6] product_id = int(line[7]) product_name = line[8] price = line[9] date = datetime.strptime(line[10], '%Y-%m-%dT%H:%MZ') #check if customer in database customer = db.session.query(models.Customer).filter( models.Customer.id == customer_id).first() if not customer: c = models.Customer(customer_id, first_name, last_name, address, state, zip_code) db.session.add(c) else: if customer.address != address or customer.state != state or customer.zip != zip_code: customer.address = address customer.state = state customer.zip = zip_code db.session.commit() # check if product in db product = db.session.query(models.Product).filter( models.Product.id == product_id).first() if not product: p = models.Product(product_id, product_name) db.session.add(p) # process order data if status == 'canceled': o = db.session.query(models.Order).filter( models.Order.customer_id == customer_id, models.Order.product_id == product_id).first() if o: # if the order is already in the db if o.status == 'new' and o.date < date: # if the status of the order is new and was placed before o.status = status db.session.commit() else: errors.append( 'Order of product {p_name} does not exist for customer {c_name}' .format(p_name=product_name, c_name=first_name + ' ' + last_name)) else: # the order did not exist in the db, we will add this to the list of errors new_order = models.Order(customer_id, product_id, price, status, date) db.session.add(new_order) db.session.commit() return render_template('index.html', errors=errors)
def process_entry(entry, website_pk): """Process entry data. params: - entry: parsed dataset line - website_pk: website Mongo <ObjectId> reference The function will process the entry data based on the "page type: product_detail or product_listing". A boolean value will be returned to mark the process ended successful or failed. The process can also raise exception for unrecoverable failures. """ if not entry['extract_ok']: return False extracted_data = entry['extracted_data'] if entry['page_type'] == 'product_detail': item = extracted_data['item'] brand = item['brand_name'] if brand: try: brand = models.Brand(brand=brand).ensure() except: raise props = { "brand": brand, "crawled_at": parse_datetime(entry['crawled_at']), "discount_percentage": item['discount_percentage'], "name": item['article_name'], "on_sale": item['on_sale'], "price": item['sale_price'], "product_type": item['article_type'], "properties": item['extra_props'], "sku": item['sku'], "url": entry['page_url'], "website": website_pk, # path=None, # listings=[], } # print(props) ## Clean None values props = utils.removeNoneValuesFromDict(props) # print(props) p = models.Product(**props) try: # p.save() p.ensure() except models.DuplicateKeyError as error: logger.debug("Item already exists: %s - %s - %s [%s]" % ( props.get("sku"), props.get("name"), props.get("url"), props.get("crawled_at"), )) return False except Exception as e: writeErrorFile('detail-%s' % (website_pk), entry['body']) raise e elif entry['page_type'] == 'product_listing': status = True number_of_items = extracted_data['number_of_items'] # number_of_items = len(extracted_data['items']) props = { "page_number": entry['page_number'], "page_listing_size": number_of_items, "category": entry['product_category'], "sorted_by": entry['ordering'], "url": entry['page_url'], "crawled_at": parse_datetime(entry['crawled_at']), "website": website_pk, } props = utils.removeNoneValuesFromDict(props) pl = models.ProductListingPage(**props) try: pl.ensure() pl_pk = pl.pk except models.DuplicateKeyError as error: pl = models.ProductListingPage.objects.get( dict([(k, v) for k, v in props.items() if k in ('url', 'crawled_at')])) pl_pk = pl.pk except: raise # ------------------------------------------------------------------------- # Assign Items # ------------------------------------------------------------------------- total_items = 0 not_found_products = 0 listing_added_total = 0 insufficent_data = 0 for i, item in enumerate(extracted_data['items']): # ------------------------------------------------------------------------- # Find Item first # ------------------------------------------------------------------------- detail_page_url = item.get('detail_page_url') if not detail_page_url: continue total_items = total_items + 1 # ------------------------------------------------------------------------- # Find matching Product based on detail_page_url # ------------------------------------------------------------------------- try: product = models.Product.objects.get({'path': detail_page_url}) except models.Product.DoesNotExist: logger.debug("No Product match found for %s" % (detail_page_url)) not_found_products = not_found_products + 1 continue try: li_props = { "position": i + 1, "price": item['sale_price'], "on_sale": item['on_sale'], "discount_percentage": item['discount_percentage'], "listing_props": item['listing_props'], "listing": pl_pk, } # ------------------------------------------------------------------------- # Create Listing Item # ------------------------------------------------------------------------- li = models.ProductListingItem(**li_props) except Exception as e: writeErrorFile('listing-%s' % (pl_pk), entry['body']) logger.error(e) insufficent_data = insufficent_data + 1 continue if any([True for l in product.listings if l.listing._id == pl_pk]): # print("Listing already added to product") listing_added_total = listing_added_total + 1 continue # ------------------------------------------------------------------------- # Add New Listing ot Product listings # ------------------------------------------------------------------------- product.listings.append(li) try: product.save() listing_added_total = listing_added_total + 1 except Exception as e: logger.error(e) writeErrorFile('listing-%s-%s' % (pl_pk, i), entry['body']) # ------------------------------------------------------------------------- # Debug stats # ------------------------------------------------------------------------- logger.debug("""%s: stats (ok:%s/missing:%s/nodata:%s/total:%s)""" % ( utils.get_url_path(entry['page_url']), listing_added_total, not_found_products, insufficent_data, total_items, )) return True else: logger.error("Unknown page_type") return False return True
def setData(self): womens = [ "Myrtis Kahn ", "Margery Hou ", "Georgianne Teneyck ", "Zora Dunsmore ", "Misha Calcagni ", "Coral Watson ", "Destiny Millet ", "Willodean Severt ", "Nanci Nicol ", "Maris Credle ", "Brittney Hawes ", "Maryrose Butterworth ", "Catherin Dobson ", "Marylin Torpey ", "Myrtle Tracy ", "Velda Coy ", "Lolita Maglio ", "Stephaine Ciulla ", "Regina Quarterman ", "Dorine Bucy " ] mans = [ "Lowell Wightman ", "Avery Harriger ", "Alonso Marcello ", "Johnson Rego ", "Wilfred Mackay ", "Trent Dimick ", "Edgar Holtkamp ", "Nicholas Chaudhry ", "Bernard Bourbeau ", "Gabriel Sperling ", "Rolf Cirillo ", "Ruben Turberville ", "Ethan Dresser ", "Spencer Bourque ", "Marcelo Draves ", "Galen Maybee ", "Malcom Mckee ", "Felix Moreles ", "Reuben Villalpando ", "Archie Liao " ] sellers = [] products = [] customers = [] for p in womens: sellers.append( self.sellers.insert_one( models.Seller(p.split(" ")[0], p.split(" ")[1])).inserted_id) customers.append( self.customers.insert_one( models.Customer(p.split(" ")[0], p.split(" ")[1])).inserted_id) for p in mans: sellers.append( self.sellers.insert_one( models.Seller(p.split(" ")[0], p.split(" ")[1])).inserted_id) customers.append( self.customers.insert_one( models.Customer(p.split(" ")[0], p.split(" ")[1])).inserted_id) for i in range(0, 100): products.append( self.products.insert_one( models.Product("Product " + str(randint(0, 1000)), randint(0, 10000))).inserted_id) for i in range(0, 75000): pr = self.products.find_one( {'_id': ObjectId(products[randint(0, len(products) - 1)])}) count = randint(0, 10) sell = self.sellers.find_one( {"_id": sellers[randint(0, len(sellers) - 1)]}) cust = self.customers.find_one( {"_id": customers[randint(0, len(customers) - 1)]}) self.sales.insert_one( models.Sale(sell, cust, pr, count, count * pr["price"]))