def handle(self, *args, **options): url = f"https://fr.openfoodfacts.org/cgi/search.pl?action=process&tagtype_0=unique_scans_n&?sort_by=popularity&page_size=1000&json=true" req = requests.get(url) data = req.json() prod = data["products"] number_prod = 0 for x in prod: try: name = x["product_name"] image_url = x["image_url"] categories_tag = x["categories_hierarchy"] categories = [] for cat in categories_tag: categories.append(cat[3:]) nutriscore_grade = x["nutriscore_grade"] image_nutrition = x["image_nutrition_url"] barcode = x["code"] url = x["url"] product = Product(name=name, categories=categories, image_url=image_url, nutriscore_grade=nutriscore_grade, image_nutriments=image_nutrition, barcode=barcode, url=url) if not Product.objects.filter(name=product.name): product.save() number_prod += 1 if number_prod % 10 == 0: self.stdout.write( f'Loading: "{number_prod}" products are saved!') except: self.stdout.write(f'Exception : {name} not saved ...') self.stdout.write(f'Fill_db : {number_prod} products are saved!')
def create_new_sale(request): if request.method == "POST": form = ProductForm(request.POST) if form.is_valid(): name = form.cleaned_data["name"] description = form.cleaned_data["description"] price = form.cleaned_data["price"] product = Product(name=name, description=description, price=price, owner=request.user) product.save() solvers = form.cleaned_data["solvers"] for solver in solvers: product.solver_list.add(solver) return redirect(reverse("homepage")) else: form = ProductForm() context = {"form": form} return render(request, "catalogue/create_new_sale.html", context=context)
# create two products from django.core.files import File granola = Product( category=artisinal_foods, name="Gourmet Granola", slug="gourmet-granola", description="Liliana's gourmet granola.", price_in_cop=18.000) granola.photo.save("granola.gif", File(open("img/granola_foto.gif"))) granola.save() eggs = Product( category=fine_produce, name="Big Red Eggs", slug="big-reg-eggs", description="Super red yolks from uber healthy chickens.", price_in_cop=6.000) eggs.photo.save("huevos_foto.jpg", File(open("img/huevos_foto.jpg"))) eggs.save() # create featured products
def create_or_update_products(logger, cmd_name, client, recursive=True): fn = "create_or_update_products" client.set_cece_token_headers(logger) # Retrieve the (paginated) data uri = settings.CECE_API_URI + "mancelot/catalog/product?page_size=1000" logger.debug("{0}: GET {1} <-- recursive = {2}".format(fn, uri, recursive)) data = client.get_list(logger, uri, recursive=recursive) logger.debug("{0}: received {1} products".format(fn, len(data))) # Iterate through the Cece data section_map = {v: k for k, v in dict(Category.SECTIONS).items()} for i, p in enumerate(data): try: logger.debug("\n{0} / {1}\n{2}".format(i + 1, len(data), p["productID"])) # Related fields: external brand and store are FK, serialized # as string (brand_name, store_name) brand = get_or_create_brand(logger, p, brand_ctpk, client, cmd_name) store = get_or_create_store(logger, p, store_ctpk, client, cmd_name) ### Start of product # Get or create Product. Match on **cece_id<-->productID** only! product_exists = Product.objects.filter( cece_id=p["productID"]).exists() if product_exists: product = Product.objects.get(cece_id=p["productID"]) created = False else: product = Product(cece_id=p["productID"]) created = True # Overwrite all fields product.name = p["title"] product.info = p["text"] product.extra_info = p["extra_text"] product.url = p["link"] product.price = Money(p["price"], "EUR") product.from_price = (Money(p["old_price"], "EUR") if p["old_price"] else None) # product.main_image and product.extra_images all the way at the end product.store = store product.brand = brand product.cece_api_url = "{0}{1}/".format(uri, p["id"]) product.last_updated_by = client.ceceuser product.save() logger.debug(" {0} Product: {1}".format( "Created" if created else "Have", product)) # end of fields # Related field: external color is a string (one color per instance) get_or_create_color(logger, p, product, product_ctpk, color_ctpk, client, cmd_name) # Related fields: external category (M2M) and subcategory (FK to category) get_or_create_categories(logger, p, product, product_ctpk, category_ctpk, client, cmd_name) get_or_create_subcategories( logger, p, product, product_ctpk, category_ctpk, subcategory_ctpk, client, cmd_name, ) get_or_create_material(logger, p, product, product_ctpk, material_ctpk, client, cmd_name) get_or_create_size(logger, p, product, product_ctpk, size_ctpk, client, cmd_name) ### End of product ### Download the images. extra_images = (p["extra_images"].replace("[", "").replace( "]", "").replace("'", "").split(", ") ) # p["extra_images"] is str, and json.loads fails # Create folder if it does not exist img_dir = "{0}/img/products/{1}".format(settings.STATIC_ROOT, store.slug) if not os.path.exists(img_dir) or not os.path.isdir(img_dir): os.makedirs(img_dir) logger.debug(" Created folder '{0}'".format(img_dir)) # Iterate through all images all_extra_images = [] for i, img_url in enumerate([p["primary_image"]] + extra_images): if not img_url: continue fname = os.path.basename(urlparse(img_url).path) logger.debug(" Fetch '{0}' from Cece".format(img_url)) if "cece" not in img_url: if img_url.startswith("//"): img_url = img_url.replace("//", "https://") headers = {"user-agent": "Mancelot Bot v1.3.3.7"} response = requests.head(img_url, headers=headers) if response.status_code != 200: logger.error( " Could not retrieve img_url = '{0}'.".format( img_url)) continue extension = response.headers["Content-Type"].replace( "image/", "") fname = "{0}.{1}".format( hashlib.md5(img_url.encode("utf-8")).hexdigest()[0:7], extension) save_to = "{0}/img/products/{1}/{2}".format( settings.STATIC_ROOT, store.slug, fname) download_success = call_download_image( logger, img_url, save_to, product, "main_image" if i is 0 else "extra_images", product_ctpk, client.ceceuser.pk, cmd_name, ) logger.debug( " download_success: {0}".format(download_success)) if download_success: for size in [ (64, 64), (128, 128), (256, 256), (512, 512), (1024, 1024), ]: # generate_thumbnail also calls optimize_image on thumb generate_thumbnail(logger, save_to, size=size, w=" ") if i > 0: # b/c set to single image above all_extra_images.append(product.extra_images) thumbnail = product.main_image.replace(".jpg", "_256x256.jpg") thumbnail_path = "{0}/{1}".format( str(settings.STATIC_ROOT), urlparse(thumbnail).path).replace("//static", "") if os.path.exists(thumbnail_path) and os.path.isfile( thumbnail_path): product.thumbnail = thumbnail product.save() product.extra_images = all_extra_images product.save() ### End of logo download except Exception as e: # Pushes to Sentry logger.error("Caught error in '{0}': {1}".format(cmd_name, e))
description="Artisinal foods.") artisinal_foods.save() # create two products from django.core.files import File granola = Product(category=artisinal_foods, name="Gourmet Granola", slug="gourmet-granola", description="Liliana's gourmet granola.", price_in_cop=18.000) granola.photo.save("granola.gif", File(open("img/granola_foto.gif"))) granola.save() eggs = Product(category=fine_produce, name="Big Red Eggs", slug="big-reg-eggs", description="Super red yolks from uber healthy chickens.", price_in_cop=6.000) eggs.photo.save("huevos_foto.jpg", File(open("img/huevos_foto.jpg"))) eggs.save() # create featured products fgranola = FeaturedProduct(product=granola) fgranola.save()