def process_item(self, item, spider): # Check if the Product already exists product = (self.session.query(Product).filter_by( store=item["store"], sku=item["sku"]).first()) if product is None: product = Product(store=item["store"], sku=item["sku"]) product.barcodes = item["barcodes"] product.brand = item["brand"] product.name = item["name"] product.description = item["description"] product.image_url = item["image_url"] self.session.add(product) self.session.commit() # Check if the BranchProduct already exists branch_product = (self.session.query(BranchProduct).filter_by( product=product, branch=item["branch"]).first()) if branch_product is None: branch_product = BranchProduct(product=product, branch=item["branch"]) branch_product.stock = item["stock"] branch_product.price = item["price"] self.session.add(branch_product) self.session.commit() return item
def products_to_db(products): """ It saves the products in the database :param products: dictionary with the desired information """ session = load_session() for key, item in products.items(): print('\n>>> Processing:', key, item['NAME']) product = (session.query(Product).filter_by(store="Richart's", sku=item["SKU"]).first()) if product is None: product = Product(store="Richart's", sku=item["SKU"]) product.barcodes = item["BARCODES"] product.brand = item["BRAND"].capitalize() product.name = item["NAME"].capitalize() description = remove_html_tags(item["DESCRIPTION"]) product.description = description.capitalize() product.image_url = item["IMAGE_URL"] product.category = item["FULL_CATEGORY"] product.package = product.description.replace(product.name, '') session.add(product) session.commit() # Check if the BranchProduct already exists branch_product = (session.query(BranchProduct).filter_by( product=product, branch=item["BRANCH"]).first()) if branch_product is None: branch_product = BranchProduct(product=product, branch=item["BRANCH"]) branch_product.stock = item["STOCK"] branch_product.price = item["PRICE"] session.add(branch_product) session.commit() session.close()
def process_item(item): Session = sessionmaker(bind=engine) session = Session() # Check if the Product already exists product = (session.query(Product).filter_by(store=item["store"], sku=item["sku"]).first()) if product is None: product = Product(store=item["store"], sku=item["sku"]) product.barcodes = item["barcodes"] product.brand = item["brand"] product.name = item["name"] product.description = item["description"] product.image_url = item["image_url"] product.category = item["category"] product.package = item["package"] session.add(product) session.commit() # Check if the BranchProduct already exists branch_product = (session.query(BranchProduct).filter_by( product=product, branch=item["branch"]).first()) if branch_product is None: branch_product = BranchProduct(product=product, branch=item["branch"]) branch_product.stock = item["stock"] branch_product.price = item["price"] session.add(branch_product) session.commit() return item