def create_and_setup_categories(category_obj): category_soup = get_soup_for_obj(category_obj) try: category_obj.description = category_soup.\ find('div', {'class': 'categoryDesc'}).\ find('p').text except: category_obj.description = '' category_obj.save() try: sub_category_tags = category_soup.\ find('table', {'class': 'categoryList'}). \ find_all('td', {'class': 'categoryListItem'}) for sub_category_tag in sub_category_tags[:5]: category_obj.sub_categories.append( get_sub_category(sub_category_tag)) category_obj.save() except: product_tags = category_soup.find_all('td', {'class': 'prodWrap'}) for product_tag in product_tags[:5]: product_obj = Product() product_obj.category = category_obj product_obj.href = product_tag.find('a').get('href') product_obj.image_url = product_tag.\ find('td', {'class': 'imageWrap'}).find('img').get('src') product_obj.title = product_tag.\ find('div', {'class': 'productsname'}).find('a').text product_obj.save()
def create_products(): product_id = 0 for category_obj in Category.objects(): category_soup = BeautifulSoup(r.get(f'{SHOP_URL}{category_obj.href}').content) product_tags = category_soup.find_all('td', {'class': 'prodWrap'}) for product_tag in product_tags: product_obj = Product() product_obj.product_id = product_id product_obj.category = category_obj product_obj.items = randint(0, 10) product_obj.availability = (product_obj.items != 0) product_obj.views = 0 product_name = product_tag.find('div', {'class': 'productsname'}).\ find('a').text product_obj.name = product_name print(product_name) try: product_price = product_tag.find('span', {'class': 'price'}).\ find('b', {'class': 'int'}).text except: break product_price = product_price.replace('\xa0', '') product_obj.price = int(product_price) product_obj.save() product_id += 1
def add(self, nombre, proveedor, categoria, precio, existencias): product = Product() product.name_product = nombre result = product.read() if result: return -1 else: product.provider = proveedor product.category = categoria product.unit_price = precio product.stock = existencias product.create() return product
def create_product(shop): category = Category() category.title = 'test_title' category.description = 'test_description' category.shop = shop.id category.save() product = Product() product.title = 'test_title' product.description = 'test_description' product.category = category.id product.price = 1000 product.image = 'test_image' product.save() return product
def test_products(self): """ Test the products @property """ shop = create_shop() category = Category() category.title = 'test_title' category.description = 'test_description' category.shop = shop.id category.save() product = Product() product.title = 'test_title' product.description = 'test_description' product.price = 1000 product.category = category.id product.image = 'test_image' product.save() storage.close() savedProd = category.products[0] self.assertEqual(savedProd.id, product.id) us = storage.get(User, shop.user) storage.delete(us) storage.save() storage.close()
def create_product(): """ Creates a new product linked to a category --- parameters: - in: header name: authorization required: true schema: type: string - in: query name: title required: true schema: type: string - in: query name: description required: true schema: type: string - in: query name: category required: true schema: type: string - in: query name: price required: true schema: type: integer - in: query name: category required: true schema: type: atring - in: query name: image required: true schema: type: string responses: 200: description: success at storing the new Product schema: type: object parameters: id: type: string example: 23refe34r23-4r234r-234r234r234r-r23423r4 shop: type: boolean example: true """ user = storage.get(User, request.user) state = request.get_json() product = Product() product.title = state['title'] product.description = state['description'] product.category = state['category'] product.price = str(state['price']) # check the values imagePath = '/usr/src/app/api/v1/shop/images/{}.b64'.format(product.id) print(request.user) with open(imagePath, 'w') as imageFile: imageFile.write(state['image']) product.image = imagePath product.save() # print(state) if not user.shops[0].country: return jsonify(shop='true') return jsonify(id=product.id)