def handle(self, *args, **options): Product.objects.all().delete() shop_departments = ShopDepartment.objects.order_by('pk') pk = 0 for shop_department in shop_departments: print(shop_department.name, '############') for category in shop_department.categories.order_by('pk'): print(category.pk) print(category.name) print(category.web_url) print('000000000000000000') url = requests.get(category.web_url) soup = BeautifulSoup(url.text, 'html.parser') products = soup.find_all('li', attrs={'class', 'c_cate'}) category_obj = Category.objects.get(name=category.name) for product in products: pk = pk + 1 product_name = product.find('p', attrs={'class', 'all_proNam'}).find('a').text product_image = product.find('p', attrs={'class', 'all_proImg'}).find('a').find('img')['data-original'] product_url = product.find('p', attrs={'class', 'all_proImg'}).find('a')['href'] product_price = product.find('div', attrs={'class', 'all_price'}).find('span', attrs={'class', 'my_shop_price'}).text print (product_url) url_detail = requests.get(product_url) detail = BeautifulSoup(url_detail.text, 'html.parser') try: product_brand = detail.find('a', attrs={'class', 'brand-name'}).text brand_obj = Brand.objects.get(name=product_brand) Product( pk=pk, name=product_name, price=product_price, category=category_obj, brand=brand_obj, image=product_image, web_url=product_url, ).save() except Exception: Product( pk=pk, name=product_name, price=product_price, category=category_obj, image=product_image, web_url=product_url, ).save() print(product_brand) print('11111111111111111111111')
def setUp(self): # TODO: should do this with mock objects? dish0 = Dish(name="The bestest dish") rec0 = Recipe(dish=dish0, title="Some awesome recipe") self.pr0 = Product(name="Harina 0000") self.pr1 = Product(name="Manzana") self.am0 = Amount(quantity=Decimal('10.01')) self.am1 = Amount(quantity=Decimal('5')) self.ing0 = Ingredient(product=self.pr0, recipe=rec0, quantity=Decimal('10.01')) self.ing1 = Ingredient(product=self.pr1, recipe=rec0, quantity=Decimal('5'))
def process_item(self, item, spider): if isinstance(item, ProductItem): name = item["name"] group = item["group"] category = item["category"] subcategory = item["subcategory"] # description = item["description"] # technical_data = item["technical_data"] product_image_url = item["product_image_url"] brand_image_url = item["brand_image_url"] url_source = item["url_source"] try: group_instance = None if group is not None: group_instance, created = GroupProduct.objects.get_or_create(name=group) create_product = Product.objects.get(name=name, group=group_instance) except BaseException: create_product = Product( name=name, group=group_instance, category=category, subcategory=subcategory, product_image_url=product_image_url, brand_image_url=brand_image_url, url_source=url_source ) create_product.save() return True
def post(self, request, format=None): """ Creates a new product """ if request.user.is_admin: serializer = self.serializer_class(data=request.data) if serializer.is_valid(): # Getting serialized data request_data = serializer.data if not Product.objects.filter(name=request_data['name']): try: with transaction.atomic(): # Creating product product = Product() product.name = request_data['name'] product.category = Category.objects.get( pk=request_data['category']) product.purchase_price = request_data[ 'purchase_price'] product.sale_price = request_data['sale_price'] if 'description' in request_data and request_data[ 'description']: product.description = request_data[ 'description'] product.save() return Response({"id": product.id}, status=status.HTTP_201_CREATED) except Exception as e: return Response( { "type": "internal_server_error", "detail": str(e) }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return Response({"type": "product_already_exist"}, status=status.HTTP_409_CONFLICT) else: return Response( { "type": "validation_error", "errors": serializer.errors }, status=status.HTTP_400_BAD_REQUEST) else: return Response({"type": "unauthorized"}, status=status.HTTP_401_UNAUTHORIZED)
def execute(self): try: scrape_obj = scrape(self._link) product = Product( link=self._link, price=scrape_obj['price'], description=scrape_obj['name'], image_urls=json.dumps(scrape_obj['images']) ) product.save() return product except Exception as error: print('Caught this error: ' + repr(error)) # log to honeybadger or kibana return None
def setUp(self): self.pr1 = Product(name="Harina 0000") self.pr2 = Product(name="Choclo en grano") self.pr3 = Product(name="Porotos rojos")