def post(self, request, *args, **kwargs): cookie_list = get_cookie_list_from_cookie(request) product_id = int(request.POST.get('productId')) product_id_list = [i['product_id'] for i in cookie_list] if product_id in product_id_list: index_item = product_id_list.index(product_id) del cookie_list[index_item] product_id_list.remove(product_id) context_products = get_product_list(product_id_list, cookie_list) context = dict( header_item=get_header_item_templates( context_products["basket_prod_list"], context_products["count_container"]), sum_products=get_sum_products( context_products["products"], context_products["product_count_list"], context_products["count_without_container"], product_id_list, context_products["count_products_small"], ), ) response = JsonResponse(context) set_cookie(response, settings.BASKET_COOKIE_NAME, json.dumps(cookie_list)) return response
def post(self, request, *args, **kwargs): cookie_list = get_cookie_list_from_cookie(request) order = self.form_class(json.loads(request.POST.get('form_fields'))) price = request.POST.get('product_sum') count_container = request.POST.get('count_container') product_ids = [i['product_id'] for i in cookie_list] count = [i['count'] for i in cookie_list] count_two = [i['count_two'] for i in cookie_list] products = Product.objects.filter(id__in=product_ids) order_items = [] product_dict = dict((product.id, product) for product in products) ordered_products = [ product_dict.get(product_item_id) for product_item_id in product_ids ] sum_container = int(count_container) * 8 if order.is_valid(): order_model = order.save(commit=False) order_model.price = price order_model.count_container = f'{count_container}шт.*8сом = {sum_container}сом' order_model.save() text = get_all_telegram_text(order_model, ordered_products, price, count, count_two) send_telegram_notification(text, 853892764) for product in ordered_products: if product.price_two: price_two = product.price_two else: price_two = 0 index_prod = ordered_products.index(product) order_items.append( OrderItem( product=product, order=order_model, quantity_big=count[index_prod], quantity_small=count_two[index_prod], price_big=product.price, price_small=price_two, )) OrderItem.objects.bulk_create(order_items) clear_basket = ClearBasketView() response = clear_basket.post(self.request, *args, **kwargs) return response else: order = self.form_class(json.loads( request.POST.get('form_fields'))) message = [] for item in order.errors: message.append([item, order.errors[item]]) return JsonResponse(dict(success=False, message=message), status=400)
def post(self, request, *args, **kwargs): cookie_list = get_cookie_list_from_cookie(request) count_two = [i['count_two'] for i in cookie_list] count = [i['count'] for i in cookie_list] product_id = int(request.POST.get('productId')) product_id_list = [i['product_id'] for i in cookie_list] index_item = product_id_list.index(product_id) if count[index_item] > 0: if count_two[index_item] > 0: count = 0, product_dict = dict( product_id=product_id, count=0, count_two=count_two[index_item], ) cookie_list[index_item] = product_dict else: count = 1, product_dict = dict( product_id=product_id, count=1, count_two=count_two[index_item], ) cookie_list[index_item] = product_dict else: count = 1, product_dict = dict( product_id=product_id, count=1, count_two=count_two[index_item], ) cookie_list[index_item] = product_dict context_products = get_product_list(product_id_list, cookie_list) count_container = sum(context_products["count_without_container"]) context = dict( count_product=count_container, sum_products=get_sum_products( context_products["products"], context_products["product_count_list"], context_products["count_without_container"], product_id_list, context_products["count_products_small"], ), count_one=count, ) response = JsonResponse(context) set_cookie(response, settings.BASKET_COOKIE_NAME, json.dumps(cookie_list)) return response
def post(self, request, *args, **kwargs): cookie_list = get_cookie_list_from_cookie(request) product_id_list = [i['product_id'] for i in cookie_list] product_id = int(request.POST.get('productId')) index_product = product_id_list.index(product_id) if request.POST.get('count'): count = int(request.POST.get('count')) else: count_list = [i['count'] for i in cookie_list] count = count_list[index_product] if request.POST.get('count_two'): count_two = int(request.POST.get('count_two')) else: count_two_list = [i['count_two'] for i in cookie_list] count_two = count_two_list[index_product] product_dict = dict(product_id=product_id, count=count, count_two=count_two) product_count_list = [i['count'] for i in cookie_list] if request.POST.get('count'): product_count_list[index_product] = int(request.POST.get('count')) else: product_count_list[index_product] = int( request.POST.get('count_two')) cookie_list[index_product] = product_dict context_products = get_product_list(product_id_list, cookie_list, count) product_count_list.reverse() count_container = sum(context_products["count_without_container"]) context = dict( count_product=count_container, sum_products=get_sum_products( context_products["products"], context_products["product_count_list"], context_products["count_without_container"], product_id_list, context_products["count_products_small"], ), ) response = JsonResponse(context) set_cookie(response, settings.BASKET_COOKIE_NAME, json.dumps(cookie_list)) return response
def get_products_from_cookie(request): cookie_list = get_cookie_list_from_cookie(request) product_id_list = [i['product_id'] for i in cookie_list] context_products = get_product_list(product_id_list, cookie_list) products_sum = get_sum_products( context_products["products"], context_products["product_count_list"], context_products["count_without_container"], product_id_list, context_products["count_products_small"], ) return { 'product_list': context_products["basket_prod_list"], 'sum_products': products_sum, 'count_container': context_products["count_container"], }
def post(self, request, *args, **kwargs): cookie_list = get_cookie_list_from_cookie(request) count = 0 count_two = 1 product_id = int(request.POST.get('productId')) product_id_list = [i['product_id'] for i in cookie_list] product_count_list = [i['count'] for i in cookie_list] product_count_two_list = [i['count_two'] for i in cookie_list] if product_id in product_id_list: index_item = product_id_list.index(product_id) count = product_count_list[index_item] if product_count_list[index_item] < 1: del cookie_list[index_item] product_id_list.remove(product_id) else: if product_count_two_list[index_item] > 0: product_dict = dict( product_id=product_id, count=count, count_two=0, ) cookie_list[index_item] = product_dict else: product_dict = dict( product_id=product_id, count=count, count_two=count_two, ) cookie_list[index_item] = product_dict else: product_id_list.append(product_id) product_dict = dict( product_id=product_id, count=count, count_two=count_two, ) cookie_list.append(product_dict) context_products = get_product_list(product_id_list, cookie_list, count) context = dict( header_item=get_header_item_templates( context_products['basket_prod_list'], context_products['count_container'], ), sum_products=get_sum_products( context_products['products'], context_products["product_count_list"], context_products["count_without_container"], product_id_list, context_products['count_products_small'], ), ) response = JsonResponse(context) set_cookie(response, settings.BASKET_COOKIE_NAME, json.dumps(cookie_list)) return response