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 warehouse_product_report_view(request): date_start, date_end = date_range_filter(request) products = Product.objects.none() if 'get_button' in request.GET: products = Product.filters_data(request, Product.objects.filter(active=True)) vendor_filter, date_filter, search_filter = [True] * 3 vendors, categories = Vendor.objects.filter( active=True), WarehouseCategory.objects.filter(active=True) # get warehouse warehouse_data = InvoiceOrderItem.objects.filter( order__date_expired__range=[date_start, date_end], product__in=products) warehouse_movement = warehouse_data.values_list('product__title').annotate(qty=Sum('qty'), total_value=Sum('total_final_value') ).order_by('product__title') \ if warehouse_data else 'No data' # get sells sells_data = OrderItem.objects.filter( order__date_expired__range=[date_start, date_end], title__in=products) sell_movements = sells_data.values_list('title__title', 'title__vendor__title')\ .annotate(qty=Sum('qty'), total_cost=Sum('total_cost_value'), total_value=Sum('total_value') ).order_by('title__title') return render(request, 'reports/warehouse_product.html', context=locals())
def get_context_data(self, **kwargs): context = super(ChartSizeManagerView, self).get_context_data(**kwargs) brands = Brand.objects.filter(active=True) categories = Category.objects.filter(active=True) products = Product.filters_data( self.request, Product.my_query.active_for_site())[:15] search_filter, brand_filter = [True] * 2 context.update(locals()) return context
def ajax_presentation_filter_products_view(request): products = Product.filters_data(request, Product.objects.all()) products = products[:30] data = dict() data['result'] = render_to_string('dashboard/ajax_calls/product_presentation_container.html', request=request, context={ 'product_list': products }) return JsonResponse(data)
def ajax_products_discount_add(request, pk): data = dict() discount_order = get_object_or_404(ProductDiscount, id=pk) products = Product.filters_data(request, Product.objects.all()) products = list(products) discount_order.products_related.add(*products) discount_order.save() discount_order.refresh_from_db() data['result'] = render_to_string(template_name='dashboard/ajax_calls/discount_result.html', request=request, context={'object': discount_order}) return JsonResponse(data)
def ajax_search_products(request): products = Product.filters_data(request, Product.objects.all()) print('hey!', products.count()) data = dict() queryset_table = TableProduct(products) RequestConfig(request, paginate={"per_page": 30}).configure(queryset_table) data['result'] = render_to_string('dashboard/ajax_calls/queryset_table.html', request=request, context={ 'product_list': products, 'queryset_table': queryset_table }) return JsonResponse(data)
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)
def ajax_search_products(request, pk): instance = get_object_or_404(SalesInvoice, id=pk) products = Product.filters_data(request, Product.objects.filter(active=True)) data = dict() data['result'] = render_to_string( template_name='point_of_sale/ajax/product_container.html', request=request, context={ 'object': instance, 'products': products }) return JsonResponse(data)
def get_queryset(self): qs = Product.my_query.active_for_site() qs = Product.filters_data(self.request, qs) self.initial_queryset = qs if self.request.GET.getlist('char_name', None): try: ids = ProductCharacteristics.filters_data( self.request, ProductCharacteristics.objects.all()).values_list( 'product_related__id') qs = qs.filter(id__in=ids) except: qs = qs return qs
def update_sale_invoice_view(request, pk): instance = get_object_or_404(SalesInvoice, id=pk) form = SalesInvoiceForm(request.POST or None, instance=instance) form.fields['costumer'].widget = forms.HiddenInput() if form.is_valid(): form.save() return redirect(instance.get_edit_url()) back_url = instance.get_edit_url() products = Product.filters_data(request, Product.objects.filter(active=True))[:10] return render(request, 'point_of_sale/update_view.html', context={'form': form, 'object': instance, 'products': products, 'back_url': back_url })
def get_queryset(self): self.initial_queryset = Product.my_query.products_with_offer() qs = Product.filters_data(self.request, self.initial_queryset) if self.request.GET.get('attr_name', None): qs = Attribute.product_filter_data(self.request, qs) if self.request.GET.getlist('char_name', None): try: ids = ProductCharacteristics.filters_data( self.request, ProductCharacteristics.objects.all()).values_list( 'product_related__id') qs = qs.filter(id__in=ids) except: qs = qs return qs
def get_queryset(self): brand = self.brand = get_object_or_404(Brand, slug=self.kwargs['slug']) qs = Product.my_query.active_for_site().filter(brand=brand) self.initial_queryset = qs qs = Product.filters_data(self.request, qs) if self.request.GET.getlist('char_name', None): try: ids = ProductCharacteristics.filters_data( self.request, ProductCharacteristics.objects.all()).values_list( 'product_related__id') qs = qs.filter(id__in=ids) except: qs = qs return qs
def ajax_warehouse_analysis(request): print('here') data = dict() products = Product.filters_data(request, Product.objects.all()) queryset = InvoiceOrderItem.objects.filter(product__in=products) queryset_table = queryset.values_list('product__title', 'product__qty','product__vendor')\ .annotate(qty=Sum('qty'), total_value=Sum('total_final_value') ).order_by('product__title') \ if queryset else 'No data' print(queryset_table) data['result_analysis'] = render_to_string( template_name='reports/ajax_analysis.html', request=request, context={'queryset_table': queryset_table}) return JsonResponse(data)
def get_queryset(self): self.initial_queryset = Product.my_query.active_for_site().filter( timestamp__gt=datetime.datetime.today() - datetime.timedelta(days=60)) qs = Product.filters_data(self.request, self.initial_queryset) if self.request.GET.getlist('attr_name', None): qs = Attribute.product_filter_data(self.request, qs) if self.request.GET.getlist('char_name', None): try: ids = ProductCharacteristics.filters_data( self.request, ProductCharacteristics.objects.all()).values_list( 'product_related__id') qs = qs.filter(id__in=ids) except: qs = qs return qs
def ajax_product_calculate_view(request): data = dict() qs = Product.filters_data(request, Product.objects.all()) total_qty = qs.aggregate(Sum('qty'))['qty__sum'] if qs.exists() else 0 total_cost_value = qs.aggregate( total=Sum(F('qty') * F('price_buy')))['total'] if qs.exists() else 0 total_value = qs.aggregate( total=Sum(F('qty') * F('final_price')))['total'] if qs.exists() else 0 qs_vendor = qs.values_list('vendor__title').annotate( qty_=Sum('qty'), total_cost_=Sum(F('qty') * F('price_buy')), total_value_=Sum(F('qty') * F('final_price'))).order_by('qty_') data['report_result'] = render_to_string( template_name='dashboard/ajax_calls/product_result.html', request=request, context={ 'currency': CURRENCY, 'qs_vendor': qs_vendor[:10], 'total_qty': total_qty, 'total_cost_value': total_cost_value, 'total_value': total_value }) return JsonResponse(data)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) products = Product.objects.none() if 'search_button' in self.request.GET: products = Product.filters_data(self.request, Product.objects.all()) back_url = reverse('dashboard:discount_manager') # filters brand_filter, category_filter, search_filter, vendor_filter = [True ] * 4 vendors, categories, brands = Vendor.objects.filter( active=True), Category.objects.filter( active=True), Brand.objects.filter(active=True) # ajax filter url get_params = self.request.get_full_path().split( '?', 1)[1] if '?' in self.request.get_full_path() else '' ajax_add_url = reverse('dashboard:ajax_products_discount_add', kwargs={'pk': self.object.id }) + '?' + get_params print(ajax_add_url) context.update(locals()) return context
def get_queryset(self): queryset = Product.filters_data(self.request, Product.objects.all()) self.total_products = queryset.count() if queryset else 0 return queryset
def test_string_representation(self): obj = Product(name="name") self.assertEqual(str(obj), "{0}".format(obj.name))
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))
def get_queryset(self): qs = self.model.my_query.active() qs = Product.filters_data(self.request, qs) return qs
artisinal_foods = CatalogueCategory( catalogue=catalogue, name="Artisinal Foods", slug="artisinal-foods", 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)
fine_produce.save() artisinal_foods = CatalogueCategory(catalogue=catalogue, name="Artisinal Foods", slug="artisinal-foods", 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")))
'filters': applied_filters, 'brands': get_brands(pre_filter_product_list, final_product_subquery, applied_filters), 'themes': get_themes(pre_filter_product_list, final_product_subquery, applied_filters), 'prices': get_prices(pre_filter_product_list, final_product_subquery, applied_filters), 'ages': get_ages(pre_filter_product_list, final_product_subquery, applied_filters), 'features': get_features(final_product_subquery, applied_filters), 'colors': get_colors(pre_filter_product_list, final_product_subquery, applied_filters), 'countries': get_countries(pre_filter_product_list, final_product_subquery, applied_filters), } return render_to_response("category.html", context, context_instance=RequestContext(request)) PRODUCT_SORTS = { 'bestselling': lambda q: q, # TODO # http://stackoverflow.com/questions/981375/using-a-django-custom-model-method-property-in-order-by 'priceMin': lambda q: Product.select_current_price(q).order_by("current_price"), 'priceMax': lambda q: Product.select_current_price(q).order_by("-current_price"), 'nameA': lambda q: q.order_by('name'), 'nameZ': lambda q: q.order_by('-name'), 'rating': lambda q: q.extra(select={'null_rating': "rating is null"}, order_by=['null_rating', '-rating']), 'new': lambda q: q.order_by('-created_at'), } def _sort_by_best_match(queryset, score_map): """ WARNING: this will force the evaluation of the queryset """ if not score_map: return queryset