def add_shopping_list(request): form = ShoppingListForm(request.POST) if form.is_valid(): new_shopping_list = ShoppingList(name=request.POST['name'].upper(), user_id=request.user) new_shopping_list.save() return redirect('shopping:shopping_list_index')
def view_shopping_list(request): if request.session.get("shopping_list"): sl_id = request.session.get("shopping_list") try: sl = ShoppingList.objects.get(id=sl_id) except ShoppingList.DoesNotExist: sl = ShoppingList() sl.save() request.session["shopping_list"] = sl.id l = list(sl.amount.all()) else: l = list() return render(request, "shopping/shoppinglist_detail.html", {"amount_list": l})
def view_shopping_list(request): if request.session.get('shopping_list'): sl_id = request.session.get('shopping_list') try: sl = ShoppingList.objects.get(id=sl_id) except ShoppingList.DoesNotExist: sl = ShoppingList() sl.save() request.session['shopping_list'] = sl.id l = list(sl.amount.all()) else: l = list() return render(request, 'shopping/shoppinglist_detail.html', {'amount_list': l})
def add_item(request): """ Add an ingredient with amount into shopping list """ if request.is_ajax() and request.method == 'POST': amount_id = int(request.POST['amount']) amount_item = get_object_or_404(Amount, pk=amount_id) if request.session.get('shopping_list'): sl_id = request.session.get('shopping_list') try: sl = ShoppingList.objects.get(id=sl_id) except ShoppingList.DoesNotExist: sl = ShoppingList() sl.save() request.session['shopping_list'] = sl.id sl.amount.add(amount_item) else: sl = ShoppingList() sl.save() sl.amount.add(amount_item) request.session['shopping_list'] = sl.id return HttpResponse(json.dumps({'success': 'true'}), mimetype="application/json") else: raise Http404
def add_item(request): """ Add an ingredient with amount into shopping list """ if request.is_ajax() and request.method == "POST": amount_id = int(request.POST["amount"]) amount_item = get_object_or_404(Amount, pk=amount_id) if request.session.get("shopping_list"): sl_id = request.session.get("shopping_list") try: sl = ShoppingList.objects.get(id=sl_id) except ShoppingList.DoesNotExist: sl = ShoppingList() sl.save() request.session["shopping_list"] = sl.id sl.amount.add(amount_item) else: sl = ShoppingList() sl.save() sl.amount.add(amount_item) request.session["shopping_list"] = sl.id return HttpResponse(json.dumps({"success": "true"}), mimetype="application/json") else: raise Http404
def generate_shopping_list_for_meal(request, meal_id): portions = request.POST.get('portions') append_to_existing_shopping_lists = request.POST.get('append_to_existing_shopping_lists') meal_ingredients = MealIngredient.objects.select_related('ingredient_id').filter(meal_id_id=meal_id) meal_name = Meal.objects.filter(pk=meal_id)[0].name shop_dict = {} user_shops_query = Shop.objects.filter(user_id=request.user) existing_shopping_lists = {} for shopping_list in ShoppingList.objects.filter(user_id_id=request.user): existing_shopping_lists[shopping_list.id] = shopping_list.name for shop in user_shops_query: shop_dict[shop.id] = shop.name data_to_insert = [] for ingredient in meal_ingredients: if ingredient.ingredient_id.weight_per_unit > 0: quantity = round(ingredient.quantity / ingredient.ingredient_id.weight_per_unit, 2) unit = 1 else: quantity = ingredient.quantity unit = 2 final_quantity = round(quantity * int(portions), 2) data_to_insert.append( {'ingredient': ingredient, 'quantity': final_quantity, 'unit': unit, 'division': ingredient.ingredient_id.division, 'shop_id': shop_dict.get(ingredient.ingredient_id.shop_id)}) if append_to_existing_shopping_lists: shopping_list_positions_dict = {} for position in data_to_insert: if position.get('shop_id'): for key, value in existing_shopping_lists.items(): if value == position.get('shop_id'): if position.get('shop_id') not in shopping_list_positions_dict.keys(): shopping_list_positions_dict[existing_shopping_lists.get(key)] = Products.objects.filter( shopping_list_id=key) else: for key, value in existing_shopping_lists.items(): if value == 'NIEPRZYPISANY': if 'NIEPRZYPISANY' not in shopping_list_positions_dict.keys(): shopping_list_positions_dict[existing_shopping_lists.get(key)] = Products.objects.filter( shopping_list_id=key) for position in data_to_insert: if position.get('shop_id'): shop_to_alter = position.get('shop_id') else: shop_to_alter = 'NIEPRZYPISANY' if shopping_list_positions_dict.get(shop_to_alter) is None: if shop_to_alter not in existing_shopping_lists.values(): new_shopping_list = ShoppingList(user_id=request.user, name=shop_to_alter, generated=1) new_shopping_list.save() new_product = Products(product_name=position.get('ingredient'), quantity=position.get('quantity'), shopping_list_id_id=new_shopping_list.id, unit_id=position.get('unit'), division_id=position.get('division')).save() existing_shopping_lists[new_shopping_list.id] = new_shopping_list.name shopping_list_positions_dict[new_shopping_list.name] = Products.objects.filter( product_name=position.get('ingredient'), shopping_list_id__name=new_shopping_list.name) else: if str(position.get('ingredient')) in [str(item.product_name) for item in shopping_list_positions_dict.get(shop_to_alter)]: product_to_alter = Products.objects.filter(product_name=str(position.get('ingredient')), shopping_list_id_id__name=shop_to_alter)[0] product_to_alter.quantity = round(product_to_alter.quantity + float(position.get('quantity')), 2) product_to_alter.save() else: for k, v in existing_shopping_lists.items(): if shop_to_alter == v: shop_to_alter = k new_product = Products(product_name=position.get('ingredient'), quantity=position.get('quantity'), shopping_list_id_id=shop_to_alter, unit_id=position.get('unit'), division_id=position.get('division')).save() else: new_shopping_list = ShoppingList(user_id=request.user, name=meal_name, generated=1) new_shopping_list.save() new_list_position_list = [] for position in data_to_insert: position['shop_id'] = new_shopping_list.id new_list_position = Products(product_name=position.get('ingredient'), quantity=position.get('quantity'), shopping_list_id_id=position.get('shop_id'), unit_id=position.get('unit'), division_id=position.get('division')) new_list_position_list.append(new_list_position) Products.objects.bulk_create(new_list_position_list) return redirect('shopping:shopping_list_index')
def generate_shopping_lists(request): meals = MealsList.objects.select_related('meal', 'extras').filter(user=request.user, current=1) shops = Shop.objects.filter(user_id=request.user) shops = list(shops) shops.append(None) ingredients_list = [] ingr_qt_dict = {} shopping_lists = [] how_many_people = request.POST['how_many_people'] delete_generated_shopping_lists = request.POST.get('delete_generated_shopping_lists', False) if delete_generated_shopping_lists: ShoppingList.objects.filter(user_id=request.user, generated=1).delete() if how_many_people == '': how_many_people = 1 else: how_many_people = int(how_many_people) for meal in meals: if meal.extras: meal_instance = meal.extras.id for ingredient in MealIngredient.objects.select_related('ingredient_id').filter(meal_id=meal_instance): ingredients_list.append(ingredient) if meal.meal_id: meal_instance = meal.meal_id for ingredient in MealIngredient.objects.select_related('ingredient_id').filter(meal_id=meal_instance): ingredients_list.append(ingredient) for shop in shops: for ingredient in ingredients_list: ingredient_object = ingredient.ingredient_id shop_id = ingredient.ingredient_id.shop_id try: id = shop.id except Exception: id = None if id == shop_id: if ingredient_object in ingr_qt_dict.keys(): qt = ingr_qt_dict[ingredient_object][0] qt += ingredient.quantity ingr_qt_dict[ingredient_object] = [qt, ingredient_object.weight_per_unit] else: ingr_qt_dict[ingredient_object] = [ingredient.quantity, ingredient_object.weight_per_unit] for ingr, qt in ingr_qt_dict.items(): wpu = ingr.weight_per_unit if wpu == 0: wpu = 1 ingr_qt_dict[ingr][0] = round((qt[0] / int(wpu)) * how_many_people, 2) if len(ingr_qt_dict) > 0: shopping_lists.append({shop: ingr_qt_dict}) ingr_qt_dict = {} for item in shopping_lists: shop = list(item.keys())[0] if shop is None: shop = 'NIEPRZYPISANY' new_shopping_list = ShoppingList(user_id=request.user, name=shop, generated=1) new_shopping_list.save() for shop, shoping_list_items in item.items(): new_list_position_list = [] for shopping_item, qt in shoping_list_items.items(): if qt[1] != 0: unit = 1 else: unit = 2 new_list_position = Products(product_name=shopping_item, quantity=qt[0], shopping_list_id_id=new_shopping_list.id, unit_id=unit, division_id=shopping_item.division) new_list_position_list.append(new_list_position) Products.objects.bulk_create(new_list_position_list) return redirect('shopping:shopping_list_index')