def populate(): Role.insert_roles() user = Account(password='******', email='*****@*****.**', confirmed=True) user2 = Account(password='******', email='*****@*****.**', confirmed=True) new_category = Category(name='Kategoria1') new_category2 = Category(name='Kategoria2') new_product = Product(name='Produkt1', description='Opis1', weight=1.99, price=12.99, category=new_category, seller=user) new_product2 = Product(name='Produkt2', description='Opis2', weight=20, price=100, category=new_category2, seller=user2) db.session.add_all([user, user2, new_product, new_product2, new_category, new_category2]) db.session.commit()
def extract_meta(self, channel, method, properties, body): product = json.loads(body) print(body) amazon_url = 'https://www.amazon.com/Portable-Waterproof-Wireless-Bluetooth-Speakerphone/dp/B07458JJPB?th=1' meta = Scraper(amazon_url, AdvancedExtractor()).get_details([AmazonRatings]) # for each in details: # setattr(product, each.name, each.value) Product.save_meta(product['id'], meta) channel.basic_ack(delivery_tag=method.delivery_tag)
def setUpClass(cls): super(ProductModelTestCase, cls).setUpClass() cls.new_category = Category(name='Kategoria1') cls.new_category2 = Category(name='Kategoria2') cls.new_product = Product(name='Produkt1', description='Opis1', weight=1.99, price=12.99, category=cls.new_category, seller=cls.acc) cls.new_product2 = Product(name='Produkt2', description='Opis2', weight=20, price=100, category=cls.new_category2, seller=cls.acc)
def callback(ch, method, properties, body): print(" [x] Received from main") data = json.loads(body) print(data) if properties.content_type == 'product_created': product = Product(id=data['id'], title=data['title'], image=data['image']) db.session.add(product) db.session.commit() elif properties.content_type == 'product_updated': product = Product.query.get(data['id']) print(product) product.title = data['title'] product.image = data['image'] db.session.commit() elif properties.content_type == 'product_deleted': product = Product.query.get(data) db.session.delete(product) db.session.commit()
def get_products(product_ids: list) -> dict: r""" Get all the products of perticular secondary category """ # All the products of perticular secondary category products = ProductDB.objects(product_id__in=product_ids).all() # List of marshmallows of ProductDB objects data = [] for product in products: product_data = get_product_json(product, product_resource_fields) data.append(product_data) return data
def get_products(product_ids: list) -> dict: r""" Get all the sproducts of perticular secondary category """ # All the sproducts of perticular secondary category products = ProductDB.objects(product_id__in=product_ids).all() # List of marshmallows of ProductDB objects product_marshal = [ get_product_json(product, product_resource_fields) for product in products ] return product_marshal
def load_url(self, channel, method, properties, body): body = json.loads(body) url = body.get('url', '') print(url) if url: details = Scraper(url, BasicExtractor()).get_details([NameAttribute]) product = Product() for attribute in details: setattr(product, attribute.name, attribute.value) print(product.name) channel.basic_ack(delivery_tag=method.delivery_tag) data = product.__dict__ Scheduler.add('extract_meta', data)
def get(self): args = parser.parse_args() response = {} log_info(args) try: category_ids = args['category_id'] min_max_price = args['price'] if not (isinstance(category_ids, list) and isinstance(min_max_price, list) and len(category_ids) > 0 and len(min_max_price) == 2): return {'status': 'fail', 'msg': 'Bad Request'}, 400 category_ids = list(map(int, category_ids)) min_max_price = list(map(int, min_max_price)) min_price, max_price = min(min_max_price), max(min_max_price) # SELECT * FROM SecondaryCategoryDB category_docs = (SecondaryCategoryDB.objects( category_id__in=category_ids).all()) product_docs = (ProductDB.objects( category__in=category_docs, discounted_price__gt=min_price, discounted_price__lt=max_price).all()) if len(product_docs) == 0: data = [] else: data = [ get_product_json(doc, product_resource_fields) for doc in product_docs ] response['status'] = 'success' response['msg'] = {'data': data} return response, 200 except Exception as e: response['status'] = 'fail' response['msg'] = str(e) return response, 500
def get(self, id=None): if id is None: return {'status': 'success', 'msg': {'data': None}}, 204 args = parser.parse_args() response = {} try: # SELECT * FROM ProductDB WHERE product_id=id product = ProductDB.objects(product_id=id).first() if product is None: response['status'] = 'fail' response['msg'] = f'product_id {id} not available in db' return response, 404 # Marshmallow-ing product object product_marshal = get_product_json(product, product_resource_fields) # Get corresponding category from SecondaryCategoryDB # SELECT * FROM SecondaryCategoryDB WHERE category_id=parent_category_id parent_category_id = product.category.category_id category_doc = (SecondaryCategoryDB.objects( category_id=parent_category_id).first()) if category_doc is not None: # Marshmallow-ing category object category_data = get_secondary_category_json( category_doc, secondary_category_resource_fields) product_marshal['category_data'] = category_data response['status'] = 'success' response['msg'] = {'data': product_marshal} return response, 200 except Exception as e: # log_info(e) response['status'] = 'fail' response['msg'] = str(e) return response, 500
def csv_to_db(file_path): """Write csv to db""" with open(file_path, mode='r', newline="", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: if os.path.basename(file_path) == "products.csv": product = Product( title=row['Title'], asin=row['Asin'] ) db.session.add(product) db.session.commit() elif os.path.basename(file_path) == "reviews.csv": review = Review( title=row['Title'], review=row['Review'], product_asin=row['Asin'] ) db.session.add(review) db.session.commit()
def import_data(csv_filename): with open(csv_filename, 'r') as csvfile: datareader = csv.DictReader(csvfile) bulk = [] for row in datareader: row['id'] = int(row['']) del row[''] try: row['timestamp'] = datetime.strptime(row['timestamp'], '%Y-%m-%d %H:%M:%S.%f') except ValueError: row['timestamp'] = datetime.strptime(row['timestamp'], '%Y-%m-%d %H:%M:%S') bulk.append(Product(**row)) if len(bulk) >= BULK_SIZE: commit_data(db.session, bulk) bulk = [] if bulk: commit_data(db.session, bulk)
def get_products(product_ids: list) -> dict: r""" Get all the sproducts of perticular secondary category """ # All the sproducts of perticular secondary category products = ProductDB.objects(product_id__in=product_ids).all() parent_category_ids = [ product.category.category_id for product in products ] # Get corresponding all categories from SecondaryCategoryDB # SELECT * FROM SecondaryCategoryDB WHERE category_id in parent_category_ids category_docs = (SecondaryCategoryDB.objects( category_id__in=parent_category_ids).all()) # List of product marshmallows products_marshal = [] for product in products: # Marshmallow-ing product object product_marshal = get_product_json(product, product_resource_fields) for category_doc in category_docs: if product.category.category_id == category_doc['category_id']: # Marshmallow-ing category object and then # break the inner for-loop category_data = get_secondary_category_json( category_doc, secondary_category_resource_fields) product_marshal['category_data'] = category_data break products_marshal.append(product_marshal) return products_marshal
def testNameAndDescription(self): result = Product('El principito', 'Libro para niños').validateNameAndDescription() self.assertTrue(result)
# Initiate Database db.create_all() # Populate Database ATTR_PATH = 'csv/prod_attr.tsv' MAPPING_PATH = 'csv/subs_mapping.tsv' with open(ATTR_PATH, 'r') as f: csvreader = csv.reader(f, delimiter='\t') for row in csvreader: prod_id = row[0] name = row[1] size = row[3] uom = row[4] brand_desc = row[6] db.session.add(Product(prod_id, name, size, uom, brand_desc)) db.session.commit() with open(MAPPING_PATH, 'r') as f: csvreader = csv.reader(f, delimiter='\t') for row in csvreader: page = row[0] orig_id = row[1] pc_id = row[2] db.session.add(Mapping(page, orig_id, pc_id)) db.session.commit()
def testUserNoNegativeProductQuantity(self): product = Product('Teclado', 'Teclado razer BlackWidow') product.setStock(10) result = product.validateNoNegativeProductQuantity() self.assertTrue(result)
def testUserNoNegativePrice(self): product = Product('Teclado', 'Teclado razer BlackWidow') product.setPrice(100) result = product.validateNoNegativePrice() self.assertTrue(result)
#!./venv/bin/python from app import Product p1 = Product() p1['name'] = 'skill screw driver with 35 accessories, F 015 243 6AC' p1['brand'] = 'Skill' p1['description'] = "Automatic spindle lock to manually tighten or loosen screws and bolts Screwdriving in narrow places because of compact design with softgrip Easy-to-operate forward/reverse trigger Nickel Metal Hydride batteries have reduced 'memory' effect and are more environment friendly Voltage: 3,6 ... " p1['barcode'] = '2724309486751' p1.save() p2 = Product() p2['name'] = 'Black & Decker - Screwdriver Set - A7073' p2['brand'] = 'Black & Decker' p2['description'] = 'controls variable speed and direction saves time and effort Simply grip the trigger to activate and twist right for forward and twist left for reverse works on Battery' p2['barcode'] = '5035048010396' p2.save()
def post(self): args = parser.parse_args() response = {} try: content, status_code = (Product.decode_token( token=args['Authorization'])) if content['status'] == 'success': # Authorized seller product_args = { 'product_name': args['product_name'], 'short_description': args['short_description'], 'long_description': args['long_description'], 'quantity': args['quantity'], 'base_price': args['base_price'], 'discount': args['discount'], 'seller_id': content['data']['user_id'], 'features': [ feature.strip() for feature in args['features'].split(',') ], 'colors': [color.strip() for color in args['colors'].split(',')], 'sizes': [size.strip() for size in args['sizes'].split(',')], 'category_id': args['category_id'], 'img_base_url': args['img_base_url'], 'img_aux_urls': list( filter(lambda x: x is not None, [ args.get('aux_img_url_1'), args.get('aux_img_url_2'), args.get('aux_img_url_3') ])) } if current_app.config['DEBUG'] is True: log_info(args) log_info(product_args) # Add product product = ProductDB.add_document(**product_args) # product.extra = {} # Marshmallow-ing product object product_marshal = get_product_json(product, product_resource_fields) # Get corresponding category from SecondaryCategoryDB # SELECT * FROM SecondaryCategoryDB WHERE category_id=parent_category_id parent_category_id = product.category.category_id category_doc = (SecondaryCategoryDB.objects( category_id=parent_category_id).first()) if category_doc is not None: # Append this product into corresponding category product_id = product_marshal['product_id'] (category_doc.update( add_to_set__product_ids=[product.product_id])) # Marshmallow-ing category object category_data = object_to_json( category_doc, secondary_category_resource_fields) product_marshal['category_data'] = category_data product.save() # SELECT * FROM Seller2Products WHERE seller_id=product.seller_id s2p = (Seller2Products.objects( seller_id=product.seller_id).first()) if s2p is None: # If this seller has not has any product yet s2p = Seller2Products(seller_id=product.seller_id, product_ids=[product.product_id]) s2p.save() else: # If seller already has added few products # then append product.product_id to the # same category's product_ids list s2p.update(add_to_set__product_ids=[product.product_id]) response['status'] = 'success' response['msg'] = {'data': product_marshal} return response, 201 elif content['status'] == 'fail': return content, status_code except KeyError as e: # log_info(e) response['status'] = 'fail' response['msg'] = str(e) + " is not available in json data" return response, 400 except ValidationError as e: # log_info(e) response['status'] = 'fail' response['msg'] = str(e) return response, 400 except Exception as e: # log_info(e) response['status'] = 'fail' response['msg'] = str(e) return response, 500
def put(self, id): args = parser.parse_args() response = {} try: content, status_code = (Product.decode_token( token=args['Authorization'])) if content['status'] == 'success': # Authorized seller product_args = { 'product_name': args['product_name'], 'short_description': args['short_description'], 'long_description': args['long_description'], 'quantity': args['quantity'], 'discount': args['discount'], 'features': [ feature.strip() for feature in args['features'].split(',') ], 'colors': [color.strip() for color in args['colors'].split(',')], 'sizes': [size.strip() for size in args['sizes'].split(',')], 'img_base_url': args['img_base_url'], 'img_aux_urls': list( filter(lambda x: x is not None, [ args.get('aux_img_url_1'), args.get('aux_img_url_2'), args.get('aux_img_url_3') ])) } if current_app.config['DEBUG'] == True: log_info(args) log_info(product_args) # SELECT * FROM ProductDB WHERE product_id=id product = ProductDB.objects(product_id=id).first() # Update values setattr(product.date, 'date_last_update', datetime.utcnow()) # product.extra = {} for key, value in product_args.items(): if hasattr(product, key): setattr(product, key, value) elif hasattr(product.description, key): setattr(product.description, key, value) # Marshmallow-ing product object product_marshal = get_product_json(product, product_resource_fields) # Get corresponding category from SecondaryCategoryDB # SELECT * FROM SecondaryCategoryDB WHERE category_id=parent_category_id parent_category_id = product.category.category_id category_doc = (SecondaryCategoryDB.objects( category_id=parent_category_id).first()) if category_doc is not None: # Marshmallow-ing category object category_data = object_to_json( category_doc, secondary_category_resource_fields) product_marshal['category_data'] = category_data product.save() response['status'] = 'success' response['msg'] = {'data': product_marshal} return response, 200 elif content['status'] == 'fail': return content, status_code except KeyError as e: # log_info(e) response['status'] = 'fail' response['msg'] = str(e) + " is not available in json data" return response, 400 except ValidationError as e: # log_info(e) response['status'] = 'fail' response['msg'] = str(e) return response, 400 except Exception as e: # log_info(e) response['status'] = 'fail' response['msg'] = str(e) return response, 500
def delete(self, id): args = parser.parse_args() response = {} try: content, status_code = (Product.decode_token( token=args['Authorization'])) if content['status'] == 'success': # Authorized seller # SELECT * FROM ProductDB WHERE product_id=id product = ProductDB.objects(product_id=id).first() if product is None: response['status'] = 'fail' response['msg'] = f'product_id {id} not available in db' return response, 404 # SELECT * FROM Seller2Products WHERE seller_id=product.seller_id s2p = (Seller2Products.objects( seller_id=product.seller_id).first()) if s2p is None or id not in s2p.product_ids: # This seller has not has any product yet, or # This product was added by another seller response['status'] = 'fail' response[ 'msg'] = f'product_id {id} was not added by seller id {product.seller_id}' else: # If seller already has added few products # then remove product.product_id from the # same category's product_ids list and # add product.product_id to the # same category's archive_product_ids list s2p.update(pull__product_ids=product.product_id) s2p.update( add_to_set__archive_product_ids=[product.product_id]) # Get corresponding category from SecondaryCategoryDB # SELECT * FROM SecondaryCategoryDB WHERE category_id=parent_category_id parent_category_id = product.category.category_id category_doc = (SecondaryCategoryDB.objects( category_id=parent_category_id).first()) if category_doc is not None: # Remove product.product_id from the # same category's product_ids list category_doc.update(pull__product_ids=product.product_id) # Delete the product product.delete() response['status'] = 'success' response['msg'] = f'product_id {id} has deleted from db' return response, 200 elif content['status'] == 'fail': return content, status_code except Exception as e: # log_info(e) response['status'] = 'fail' response['msg'] = str(e) return response, 500
def test_product_price_can_not_be_less_than_zero(self): INVALID_PRICE = [-1, -5, -10000] product = Product('Producto 1', 'Descripción del Producto 1') for elem in INVALID_PRICE: with self.assertRaises(ValueError): product.price = elem
def setUp(self): self.product = Product('Producto 1', 'Descripción del Producto 1') self.product.stock = 10 self.product.price = 50.0
def test_product_stock_can_not_be_less_than_zero(self): INVALID_STOCK = [-1, -5, -10000] product = Product('Producto 1', 'Descripción del Producto 1') for elem in INVALID_STOCK: with self.assertRaises(ValueError): product.stock = elem
##### Product 1 ####### maker = 'Aquamar' type = 'Vestuário' title = 'Calça Jeans Rasgos' desc = 'Calça jeans boyfriend estonada com rasgos. Composição: 100% algodão.' time = "00:10" link = "http://www.eaquamar.com.br/calca-jeans-rasgos-03-02-0818/p" images = [ "https://filesquash.io/v1/a88010be/assets/e5fb6c30-25a4-4e2c-9047-e866dcd383be", "http://aquamar.vteximg.com.br/arquivos/ids/211427-337-501/03.02.0818_0000_3.jpg" ] size = ['36', '38', '40', '42', '44', '46'] price = 'R$ 79,99' option = "3x de R$ 26,66 sem juros no cartão." product = Product(maker, type, title, time, link, images, price) product.get_desc(desc) product.get_size(size) product.get_option(option) db.session.add(product) db.session.commit() ##################### ##### Product 2 ####### maker = "Schutz" type = "Vestuário" title = "Scarpin Stiletto Tanino"
def setUp(self): self.new_product = Product("jacket", "large", "2000", "20")
from app import db, Order_created, Product, Customer db.create_all() Tom = Customer(first_name='Tom', last_name='Hennessy') Ben = Customer(first_name='Ben', last_name='Hasketh') db.session.add(Tom) db.session.add(Ben) db.session.commit() mouse = Product(item='Razer mouse') keyboard = Product(item='Excelvan keyboard') monitor = Product(item='HP monitor') db.session.add(mouse) db.session.add(monitor) db.session.add(keyboard) db.session.commit() order_1 = Order_created(customer_id=Tom.id, product_id=mouse.id) order_2 = Order_created(customer_id=Tom.id, product_id=keyboard.id) order_3 = Order_created(customer_id=Ben.id, product_id=mouse.id) db.session.add(order_1) db.session.add(order_2) db.session.add(order_3) db.session.commit()
from app import Product from app import Store from app import User if __name__ == '__main__': db.create_all() store0 = Store(address='г. Минск 0000') store1 = Store(address='г. Минск 0001') user0 = User(name='Тимур') user1 = User(name='Слава') products = [ Product(id='4811680006239', name='name 1', price=3.4, store=store0), Product(id='9785818319315', name='name 2', price=5.7, store=store1), Product(id='40822426', name='Водичка Славы', price=2.21, store=store0), Product(id='4810431004296', name='tim copybook 1', price=4.9, store=store0), Product(id='4690338982053', name='tim copybook 2', price=1.99, store=store1), Product(id='0000000000000', name='test 1', price=17.3, store=store1), Product(id='0000000000001', name='test 2', price=32.0, store=store0), ] db.session.add(store0)