def db_init(): data = Category(name="Pate à tartiner") data.save() data = Product( name="Ovomaltine", category_id=Category.objects.get(name="Pate à tartiner"), store="Leclerc, BioCoop", nutriscore="a", barcode="0189654870000", url="https://ovomaltine.fr", image="https://ovomaltine.fr/photo.jpg", lipids_for_100g="4.59", saturated_fats_for_100g="0.02", sugars_for_100g="1.54", salt_for_100g="3.25", ) data.save() data = Product( name="Nutella", category_id=Category.objects.get(name="Pate à tartiner"), store="Auchan", nutriscore="b", barcode="012232370000", url="https://nutella.fr", image="https://nutella.fr/photo.jpg", lipids_for_100g="1.64", saturated_fats_for_100g="0.33", sugars_for_100g="2.20", salt_for_100g="1.06", ) data.save()
def choices(): allegro = Allegro() choice = allegro.get_categories() comma = "," for item in choice: if item['parent'] == 0: name = item['name'] if comma in name: name = name.replace(",", " -") entry = Category(category_id=item['id'], category_name=item['name']) entry.save()
def post(self, request): Product.objects.all().delete() Category.objects.all().delete() category = Category(name='CPU') category.save() category2 = Category(name='Graphic Card') category2.save() Product(name="Intel Core i5", price=5500, category=category, description="First CPU test description").save() Product(name="Intel Core i7-8700K", price=9800, category=category, description="Second CPU another description").save() Product(name="GeForce GTX 1080", price=35000, category=category2, description="Description for gtx video card").save() return Response({'success': True}, status.HTTP_200_OK)
def db_init(): """Create temp products to perform tests""" data = Category(name="Pate à tartiner") data.save() data = Product( name="Beurre de cacahuètes", category_id=Category.objects.get(name="Pate à tartiner"), store="Carrefour", nutriscore="c", barcode="012456870000", url="https://peanutbutter.fr", image="https://peanutbutter.fr/photo.jpg", lipids_for_100g="2.60", saturated_fats_for_100g="0.59", sugars_for_100g="0.11", salt_for_100g="3.51", ) data.save() data = Product( name="Ovomaltine", category_id=Category.objects.get(name="Pate à tartiner"), store="Leclerc, BioCoop", nutriscore="a", barcode="0189654870000", url="https://ovomaltine.fr", image="https://ovomaltine.fr/photo.jpg", lipids_for_100g="4.59", saturated_fats_for_100g="0.02", sugars_for_100g="1.54", salt_for_100g="3.25", ) data.save() data = Product( name="Nutella", category_id=Category.objects.get(name="Pate à tartiner"), store="Auchan", nutriscore="b", barcode="012232370000", url="https://nutella.fr", image="https://nutella.fr/photo.jpg", lipids_for_100g="1.64", saturated_fats_for_100g="0.33", sugars_for_100g="2.20", salt_for_100g="1.06", ) data.save()
def handle(self, *args, **options): dic = ["Produits à tartiner", "Plats préparés", "Céréales pour petit-déjeuner", "Pizzas", "Confiseries", "Boissons"] cpt = 0 cpt_product = 0 cpt_boucle = 0 while len(dic) > cpt: category = Category(name=dic[cpt]) print(category.name) category.save() cpt += 1 while cpt_boucle <= 5: while cpt_product < 100: try: r = requests.get("https://fr.openfoodfacts." "org/cgi/search.pl?action=process" "&tagtype_0=categories&tag_contains_" "0=contains&tag_0=" + str(dic[cpt_boucle]) + "&json=true&page_size=200") results = r.json()["products"] name = results[cpt_product]["product_name"] score = results[cpt_product]["nutriscore_grade"] img_url = results[cpt_product]["image_small_url"] url = results[cpt_product]["url"] ingredients = results[cpt_product]["ingredients_text_fr"] print(name, score, img_url) cpt_product += 1 print(cpt_product) try: Product.objects.get(name=name) except ObjectDoesNotExist: cat = Category.objects.get(id=cpt_boucle + 1) product_add = Product(name=name, score=score, img_url=img_url, category_product=cat, url=url, ingredient=ingredients) product_add.save() except KeyError: r = requests.get("https://fr.openfoodfacts." "org/cgi/search.pl?action=process" "&tagtype_0=categories&tag_contains_" "0=contains&tag_0=" + str(dic[cpt_boucle]) + "&json=true&page_size=200") results = r.json()["products"] name = results[cpt_product]["product_name"] score = 'e' try: img_url = results[cpt_product]["image_thumb_url"] except KeyError: img_url = "https://upload.wikimedia.org/wikipedia/commons/a/ac/No_image_available.svg" print(name, score, img_url) cpt_product += 1 print(cpt_product) try: Product.objects.get(name=name) except ObjectDoesNotExist: cat = Category.objects.get(id=cpt_boucle + 1) product_add = Product(name=name, score=score, img_url=img_url, category_product=cat, url=url, ingredient=ingredients) product_add.save() cpt_product = 0 cpt_boucle += 1