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 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')
class ProductTests(TestCase): def setUp(self): self.pr1 = Product(name="Harina 0000") self.pr2 = Product(name="Choclo en grano") self.pr3 = Product(name="Porotos rojos") def test_str_is_name(self): self.assertEqual(self.pr1.__str__(), "Harina 0000") self.assertEqual(self.pr2.__str__(), "Choclo en grano") self.assertEqual(self.pr3.__str__(), "Porotos rojos")
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 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 reject_or_accept_product_offer(request): data = request.data try: product_id = data['product_id'] offer_id = data.get('offer_id') offer_accepted = data['offer_accepted'] if offer_accepted is True: # validate product if Product.objects.filter( id=product_id, product_status=Product.sale).exists() is not True: raise Product.DoesNotExist() offer = Offer.objects.get(id=offer_id) offer.offer_status = Offer.accept offer.save() Offer.objects.filter( product_id=product_id, offer_status=Offer.pending).update(offer_status=Offer.rejected) return Response(status=True) except (KeyError, Product.DoesNotExist): return Response(status=status.HTTP_400_BAD_REQUEST, data={'message': 'request is invalid jaa'})
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 create_product(self, row, org): if "blog" in row.get("Source Type"): ptype = ProductType.objects.get_or_create(name="Blog")[0] else: ptype = ProductType.objects.get_or_create(name="Website")[0] p = Product( name=row.get("Source Name"), homepage=row.get("Home Page URL"), # XXX: ew product_type=ptype, organization=org, modified_by=User.objects.get(username="******"), ) p.save() Activity(user=p.modified_by, content_object=p).save() return p
class IngredientTests(TestCase): 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 test_str_includes_amount_and_product(self): self.assertIn(self.am0.__str__(), self.ing0.__str__()) self.assertIn(self.pr0.__str__(), self.ing0.__str__()) self.assertIn(self.am1.__str__(), self.ing1.__str__()) self.assertIn(self.pr1.__str__(), self.ing1.__str__())
def create(request): p = Product(brand=request.POST['brand'], name=request.POST['name'], price=request.POST['price'], created_at=timezone.now(), updated_at=timezone.now()) p.save() return HttpResponseRedirect('/')
def setUp(self): self.pr1 = Product(name="Harina 0000") self.pr2 = Product(name="Choclo en grano") self.pr3 = Product(name="Porotos rojos")