def basket_success(request, basket_key): """ A view to avoid resubmitting form on refresh of basket """ """ set all the variables to blank """ product = "" category = "" this_product = "" selected = "" baskets = "" cookie_key = "" basket_total = "" menu = Category.objects.all().order_by('id_no') """ check for a basket cookie """ context_items = basket_context(request) cookie = context_items['cookie'] cookie_key = context_items['cookie_key'] """ fetch the datasets from the models """ products = Product.objects.all() options = Options.objects.all() """ get the current basket list """ this_basket = Basket.objects.filter(cookie=basket_key) """ if only one basket item, use get_object_or_404 """ if this_basket.count() == 1: this_basket = get_object_or_404(Basket, cookie=basket_key) this_product = products.filter(name=this_basket.name) selected = this_basket.option product = this_basket.name category = this_basket.category basket_key = this_basket.cookie # Credit: https://stackoverflow.com/questions/ # 42132091/using-aggregation-api-django basket_total = (Basket.objects.filter(cookie=basket_key).aggregate( Sum('total_price'))) baskets = Basket.objects.filter(cookie=basket_key) # Credit: https://stackoverflow.com/questions/8786175/ # django-order-by-on-queryset-objects # Credit: https://stackoverflow.com/questions/9834038 # /django-order-by-query-set-ascending-and-descending baskets = baskets.order_by('-pk') context = { 'products': products, 'product': product, 'category': category, 'this_product': this_product, 'options': options, 'selected': selected, 'baskets': baskets, 'cookie_key': cookie_key, 'cookie': cookie, 'basket_total': basket_total, 'menu': menu, } return render(request, 'basket/basket.html', context)
def service(request): """ A view to recieve questions from customers """ """ check for a basket cookie """ context_items = basket_context(request) basket_total = context_items['basket_total'] cookie_key = context_items['cookie_key'] menu = Category.objects.all().order_by('id_no') """ check for a servings variable from the form """ if request.POST: """ validate the form data """ validate_name = request.POST['name'] validate_email = request.POST['email'] validate_question = request.POST['question'] if not re.match("^[a-zA-Z ]+$", ''.join(validate_name)): messages.success( request, mark_safe('There was a problem with \ name <br> Please try again.')) return redirect(reverse('home')) if not re.match(r"^(\w|\.|\_|\-)+[@](\w|\_|\-|\.)+[.]\w{2,3}$", ''.join(validate_email)): messages.success( request, mark_safe('There was a problem with \ email <br> Please try again.')) return redirect(reverse('home')) if not re.match("^[a-zA-Z0-9.?* ]+$", ''.join(validate_question)): messages.success( request, mark_safe('There was a problem with \ question <br> Please try again.')) return redirect(reverse('home')) if 'name' in request.POST: name = request.POST["name"] if 'email' in request.POST: email = request.POST["email"] if 'question' in request.POST: question = request.POST["question"] """ save the updated basket and delete the existing """ new_question = Questions(name=name, email=email, question=question) new_question.save() messages.success( request, 'Your question has been sent - \ we will reply shortly') return redirect(reverse('home')) context = { 'basket_total': basket_total, 'cookie_key': cookie_key, 'menu': menu, } return render(request, 'service/service.html', context)
def bartholemew(request): """ A view to return the bartholemew page """ """ check for a basket cookie """ context_items = basket_context(request) basket_total = context_items['basket_total'] cookie_key = context_items['cookie_key'] menu = Category.objects.all().order_by('id_no') context = { 'cookie_key': cookie_key, 'basket_total': basket_total, 'menu': menu, } return render(request, 'bartholemew/bartholemew.html', context)
def empty_basket(request): """ A view to empty the basket """ """ check for a basket cookie """ context_items = basket_context(request) cookie = context_items['cookie'] menu = Category.objects.all().order_by('id_no') baskets = Basket.objects.filter(cookie=cookie) for basket in baskets: basket.delete() context = { 'menu': menu, } return render(request, 'basket/basket.html', context)
def products(request): """ A view to show and filter products """ """ check for a basket cookie """ context_items = basket_context(request) basket_total = context_items['basket_total'] cookie_key = context_items['cookie_key'] menu = Category.objects.all().order_by('id_no') category = "" range = "" image = "" categories = Category.objects.all() products = Product.objects.all() if request.GET: if 'category' in request.GET: category = request.GET['category'] products = products.filter(category__name=category) range = "standard" image = categories.filter(name=category) if 'category_range' in request.GET: category_range = request.GET['category_range'] category_range_list = category_range.split(',') category = category_range_list[0] range = category_range_list[1] image = categories.filter(name=category) context = { 'products': products, 'category': category, 'range': range, 'image': image, 'cookie_key': cookie_key, 'basket_total': basket_total, 'menu': menu, } return render(request, 'products/products.html', context)
def product_detail(request): """ A view to show product options """ """ check for a basket cookie """ context_items = basket_context(request) basket_total = context_items['basket_total'] cookie_key = context_items['cookie_key'] menu = Category.objects.all().order_by('id_no') category = "" products = "" selected = "" image = "" options = "" item_number = "" categories = Category.objects.all() products = Product.objects.all() options = Options.objects.all() if request.GET: if 'category_product' in request.GET: category_product = request.GET['category_product'] category_product_list = category_product.split(',') category = category_product_list[0] product = category_product_list[1] selected = category_product_list[2] products = products.filter(name=product) image = categories.filter(name=category) options = options.filter(category__in=categories) context = { 'products': products, 'category': category, 'product': product, 'selected': selected, 'image': image, 'options': options, 'cookie_key': cookie_key, 'basket_total': basket_total, 'menu': menu, } return render(request, 'products/product_detail.html', context) if 'product_edit' in request.GET: product_edit = request.GET['product_edit'] product_edit_list = product_edit.split(',') category = product_edit_list[0] product = product_edit_list[1] selected = product_edit_list[2] item_number = product_edit_list[3] servings = product_edit_list[4] total_price = product_edit_list[5] servings_plusten = float(servings) + 10 products = products.filter(name=product) image = categories.filter(name=category) options = options.filter(category__in=categories) context = { 'products': products, 'category': category, 'product': product, 'selected': selected, 'image': image, 'options': options, 'servings': servings, 'total_price': total_price, 'item_number': item_number, 'servings_plusten': servings_plusten, 'cookie_key': cookie_key, 'basket_total': basket_total, 'menu': menu, } return render(request, 'products/edit_product.html', context) return render(request, 'products/product_detail.html', context)
def checkout(request): """ A view to return the checkout page """ """ check for a basket cookie """ context_items = basket_context(request) cookie = context_items['cookie'] basket_total = context_items['basket_total'] if request.user.is_authenticated: delivery_cost = 0 else: delivery_cost = float(basket_total['total_price__sum']) / 10 if delivery_cost > 49.99: delivery_cost = 49.99 grand_total = float(basket_total['total_price__sum']) + delivery_cost cookie_key = context_items['cookie_key'] baskets = "" menu = Category.objects.all().order_by('id_no') """ fetch the datasets from the models """ basket = Basket.objects.all() categories = Category.objects.all() products = Product.objects.all() options = Options.objects.all() # Attempt to prefill the form with any # info the user maintains in their profile if request.user.is_authenticated: try: profile = UserProfile.objects.get(user=request.user) order_form = OrderForm(initial={ 'full_name': profile.default_full_name, 'email': profile.user.email, 'phone_number': profile.default_phone_number, 'country': profile.default_country, 'postcode': profile.default_postcode, 'town_or_city': profile.default_town_or_city, 'street_address1': profile.default_street_address1, 'street_address2': profile.default_street_address2, 'county': profile.default_county, }) except UserProfile.DoesNotExist: order_form = OrderForm() else: order_form = OrderForm() """ fetch the Stripe keys """ stripe_public_key = settings.STRIPE_PUBLIC_KEY stripe_secret_key = settings.STRIPE_SECRET_KEY if basket_total: """ create the Stripe payment intent """ stripe_total = round(float(grand_total) * 100) stripe.api_key = stripe_secret_key intent = stripe.PaymentIntent.create( amount=stripe_total, currency=settings.STRIPE_CURRENCY, ) client_secret = intent.client_secret else: client_secret = "" if request.GET: if 'basket_number' in request.GET: basket_number = request.GET['basket_number'] baskets = basket.filter(cookie=basket_number) baskets = baskets.order_by('-pk') context = { 'cookie': cookie, 'cookie_key': cookie_key, 'basket_total': basket_total, 'delivery_cost': delivery_cost, 'grand_total': grand_total, 'baskets': baskets, 'products': products, 'categories': categories, 'options': options, 'order_form': order_form, 'stripe_public_key': stripe_public_key, 'client_secret': client_secret, 'menu': menu, } return render(request, 'checkout/checkout.html', context)
def basket(request): """ A view to show the current basket """ """ check for a basket cookie """ context_items = basket_context(request) cookie = context_items['cookie'] """ set all the variables to blank """ category = "" selected = "" options = "" baskets = "" servings = "" """ fetch the datasets from the models """ categories = Category.objects.all() products = Product.objects.all() options = Options.objects.all() """ check for a servings variable from the form """ if request.POST: if 'servings' in request.POST: servings = request.POST["servings"] if not re.match("^[0-9]+$", servings): messages.success( request, mark_safe('There was a problem! \ <br> Please reselect a product and try again.')) return redirect(reverse('home')) if request.GET: """ check for a product_add variable from the template """ if 'product_add' in request.GET: product_add = request.GET['product_add'] """ split the product_add variable into its components """ product_add_list = product_add.split(',') category = product_add_list[0] product = product_add_list[1] selected = product_add_list[2] productid = product_add_list[3] """ filter the datasets on the variables from product_add """ options = options.filter(category__in=categories) # Credit: http://morozov.ca/ # tip-how-to-get-a-single-objects-value-with-django-orm.html """ price = products.get(id_no=productid).price """ check for existing basket(s) with the current cookie value """ try: existing_basket = Basket.objects.get(cookie=cookie, category=category, name=product, option=selected) existing_servings = existing_basket.servings """ add the new servings variable to the existing """ updated_servings = existing_servings + int(servings) total_price = float(price) * float(updated_servings) """ save the updated basket and delete the existing """ updated_basket = Basket(cookie=cookie, category=category, name=product, servings=updated_servings, option=selected, total_price=total_price) updated_basket.save() messages.success(request, 'item quantity increased!') existing_basket.delete() except ObjectDoesNotExist: # Credit: https://stackoverflow.com/questions/ # 12572741/get-single-record-from-database-django """ if there is no existing basket, create a new one """ total_price = float(price) * float(servings) basket = Basket(cookie=cookie, category=category, name=product, servings=servings, option=selected, total_price=total_price) basket.save() messages.success(request, 'item added to basket!') baskets = Basket.objects.filter(cookie=cookie) baskets = baskets.order_by('-pk') """ check for a product_edit variable from the template """ if 'product_edit' in request.GET: product_edit = request.GET['product_edit'] """ split the product_add variable into its components """ product_edit_list = product_edit.split(',') updated_selected = product_edit_list[2] item_number = product_edit_list[3] productid = product_edit_list[4] """ check for existing basket(s) with the current cookie value """ try: existing_basket = Basket.objects.get(pk=item_number) product = existing_basket.name category = existing_basket.category price = products.get(id_no=productid).price if servings == "": servings = existing_basket.servings total_price = total_price = float(price) * float(servings) else: total_price = total_price = float(price) * float(servings) """ filter the datasets on the variables from product_add """ options = options.filter(category__in=categories) # Credit: http://morozov.ca/ # tip-how-to-get-a-single-objects-value-with-django-orm.html """ save the updated basket and delete the existing """ updated_basket = Basket(cookie=cookie, category=category, name=product, servings=servings, option=updated_selected, total_price=total_price) updated_basket.save() messages.success(request, 'basket updated!') existing_basket.delete() except ObjectDoesNotExist: # Credit: https://stackoverflow.com/questions/ # 12572741/get-single-record-from-database-django return redirect(reverse('basket_success', args=[cookie])) basket_key = cookie return redirect(reverse('basket_success', args=[basket_key]))
def edit_basket_item(request): """ A view to edit basket items """ """ check for a basket cookie """ context_items = basket_context(request) basket_total = context_items['basket_total'] cookie_key = context_items['cookie_key'] menu = Category.objects.all().order_by('id_no') category = "" products = "" product = "" selected = "" image = "" options = "" servings = "" total_price = "" servings_plusten = "" edit = "edit" """ fetch the datasets from the models """ categories = Category.objects.all() products = Product.objects.all() options = Options.objects.all() basket = Basket.objects.all() if request.GET: if 'item_number' in request.GET: item_number = request.GET['item_number'] item_number_list = item_number.split(',') item_number = item_number_list[0] productid = item_number_list[1] category = basket.get(pk=item_number).category selected = basket.get(pk=item_number).option product = basket.get(pk=item_number).name price = products.get(id_no=productid).price products = products.filter(name=product) image = categories.filter(name=category) options = options.filter(category__in=categories) servings = basket.get(pk=item_number).servings servings_plusten = float(servings) + 10 total_price = float(servings) * float(price) # Credit: https://tutorialdeep.com/knowhow/ # limit-float-to-two-decimal-places-python/ total_price = format(float(total_price), '.2f') context = { 'products': products, 'category': category, 'product': product, 'selected': selected, 'image': image, 'options': options, 'servings': servings, 'total_price': total_price, 'item_number': item_number, 'servings_plusten': servings_plusten, 'edit': edit, 'cookie_key': cookie_key, 'basket_total': basket_total, 'menu': menu, } return render(request, 'products/edit_product.html', context)
def bartholemew_basket(request): """ A view to let bartholemew work his magic! """ """ check for a basket cookie """ context_items = basket_context(request) cookie = context_items['cookie'] option = 1 menu = Category.objects.all().order_by('id_no') if request.POST: bartholemew_event_type = request.POST['bartholemew_eventType'] bartholemew_min_guests = int( float(request.POST['bartholemew_minGuests'])) bartholemew_max_guests = int( float(request.POST['bartholemew_maxGuests'])) bartholemew_vegan_guests = int( float(request.POST['bartholemew_veganGuests'])) bartholemew_veggie_guests = int( float(request.POST['bartholemew_veggieGuests'])) bartholemew_pesc_guests = int( float(request.POST['bartholemew_pescGuests'])) bartholemew_hotProportion = request.POST['bartholemew_hotProportion'] bartholemew_allergyProportion = ( request.POST['bartholemew_allergyProportion']) """ Calculate the average number of guests """ if bartholemew_max_guests == "": bartholemew_ave_guests = bartholemew_min_guests else: bartholemew_ave_guests = int( (bartholemew_min_guests + bartholemew_max_guests) / 2) bartholemew_nonspec_guests = int(bartholemew_ave_guests - bartholemew_vegan_guests - bartholemew_veggie_guests - bartholemew_pesc_guests) """ Calculate the number of non-allergy meals """ if bartholemew_allergyProportion == "normal": bartholemew_allergyNumber = int(bartholemew_nonspec_guests * 0.2) elif bartholemew_allergyProportion == "medium": bartholemew_allergyNumber = int(bartholemew_nonspec_guests * 0.3) elif bartholemew_allergyProportion == "high": bartholemew_allergyNumber = int(bartholemew_nonspec_guests * 0.4) """ Calculate the number of hot meals """ if bartholemew_hotProportion == "No": bartholemew_hotNumber = int(bartholemew_nonspec_guests * 0.2) elif bartholemew_hotProportion == "Medium amount of": bartholemew_hotNumber = int(bartholemew_nonspec_guests * 0.3) elif bartholemew_hotProportion == "Lots of": bartholemew_hotNumber = int(bartholemew_nonspec_guests * 0.4) """ Calculate the total of specified meals """ bartholemew_specified = int(bartholemew_vegan_guests + bartholemew_veggie_guests + bartholemew_pesc_guests + bartholemew_allergyNumber + bartholemew_hotNumber) bartholemew_unspecified = int(bartholemew_ave_guests - bartholemew_specified) # Credit: https://stackoverflow.com/questions/32389519/ # django-get-10-random-instances-from-a-queryset-and- # order-them-into-a-new-querys """ MAIN COURSES """ """ Generate 3 random main courses for each unspecified guest """ products_main = [ i.id for i in Product.objects.filter(category__course="main") ] products_main_unspecified = [] for i in range(bartholemew_unspecified): random.shuffle(products_main) products_main_shuffled = ([ Product.objects.get(id=i) for i in products_main ]) products_main_unspecified = (products_main_unspecified + products_main_shuffled[0:3]) """ Generate 3 random main courses for each vegan guest """ products_main = ([ i.id for i in Product.objects.filter(category__course="main", allergies__contains="v") ]) products_main_vegan = [] for i in range(bartholemew_vegan_guests): random.shuffle(products_main) products_main_shuffled = ([ Product.objects.get(id=i) for i in products_main ]) products_main_vegan = (products_main_vegan + products_main_shuffled[0:3]) """ Generate 3 random main courses for each vegetarian guest """ products_main = ([ i.id for i in Product.objects.filter(category__course="main", allergies__contains="g") ]) products_main_veggie = [] for i in range(bartholemew_veggie_guests): random.shuffle(products_main) products_main_shuffled = ([ Product.objects.get(id=i) for i in products_main ]) products_main_veggie = (products_main_veggie + products_main_shuffled[0:3]) """ Generate 3 random main courses for each pescatarian guest """ products_main = ([ i.id for i in Product.objects.filter(category__course="main", allergies__contains="p") ]) products_main_pesc = [] for i in range(bartholemew_pesc_guests): random.shuffle(products_main) products_main_shuffled = ([ Product.objects.get(id=i) for i in products_main ]) products_main_pesc = (products_main_pesc + products_main_shuffled[0:3]) """ Generate 3 random main courses for hot food proportion """ products_main = ([ i.id for i in Product.objects.filter(category__course="main", allergies__contains="h") ]) products_hot_food = [] for i in range(bartholemew_hotNumber): random.shuffle(products_main) products_main_shuffled = ([ Product.objects.get(id=i) for i in products_main ]) products_hot_food = (products_hot_food + products_main_shuffled[0:3]) """ SIDE COURSES """ """ Generate 2 random side courses for each unspecified guest """ products_side = ([ i.id for i in Product.objects.filter(category__course="side") ]) products_side_unspecified = [] for i in range(bartholemew_unspecified): random.shuffle(products_side) products_side_shuffled = ([ Product.objects.get(id=i) for i in products_side ]) products_side_unspecified = (products_side_unspecified + products_side_shuffled[0:2]) """ Generate 2 random side courses for each vegan guest """ products_side = ([ i.id for i in Product.objects.filter(category__course="side", allergies__contains="v") ]) products_side_vegan = [] for i in range(bartholemew_vegan_guests): random.shuffle(products_side) products_side_shuffled = ([ Product.objects.get(id=i) for i in products_side ]) products_side_vegan = (products_side_vegan + products_side_shuffled[0:2]) """ Generate 2 random side courses for each vegetarian guest """ products_side = ([ i.id for i in Product.objects.filter(category__course="side", allergies__contains="g") ]) products_side_veggie = [] for i in range(bartholemew_veggie_guests): random.shuffle(products_side) products_side_shuffled = ([ Product.objects.get(id=i) for i in products_side ]) products_side_veggie = (products_side_veggie + products_side_shuffled[0:2]) """ Generate 2 random side courses for each pescatarian guest """ products_side = ([ i.id for i in Product.objects.filter(category__course="side", allergies__contains="p") ]) products_side_pesc = [] for i in range(bartholemew_pesc_guests): random.shuffle(products_side) products_side_shuffled = ([ Product.objects.get(id=i) for i in products_side ]) products_side_pesc = (products_side_pesc + products_side_shuffled[0:2]) """ DESSERT COURSES """ """ Generate 1 random dessert courses for each unspecified guest """ products_dessert = ([ i.id for i in Product.objects.filter(category__course="dessert") ]) products_dessert_unspecified = [] for i in range(bartholemew_unspecified): random.shuffle(products_dessert) products_dessert_shuffled = ([ Product.objects.get(id=i) for i in products_dessert ]) products_dessert_unspecified = (products_dessert_unspecified + products_dessert_shuffled[0:1]) """ Generate 1 random dessert courses for each vegan guest """ products_dessert = ([ i.id for i in Product.objects.filter(category__course="dessert", allergies__contains="v") ]) products_dessert_vegan = [] for i in range(bartholemew_vegan_guests): random.shuffle(products_dessert) products_dessert_shuffled = ([ Product.objects.get(id=i) for i in products_dessert ]) products_dessert_vegan = (products_dessert_vegan + products_dessert_shuffled[0:1]) """ Generate 1 random dessert courses for each vegetarian guest """ products_dessert = ([ i.id for i in Product.objects.filter(category__course="dessert", allergies__contains="g") ]) products_dessert_veggie = [] for i in range(bartholemew_veggie_guests): random.shuffle(products_dessert) products_dessert_shuffled = ([ Product.objects.get(id=i) for i in products_dessert ]) products_dessert_veggie = (products_dessert_veggie + products_dessert_shuffled[0:1]) """ Generate 1 random dessert courses for each pescatarian guest """ products_dessert = ([ i.id for i in Product.objects.filter(category__course="dessert", allergies__contains="p") ]) products_dessert_pesc = [] for i in range(bartholemew_pesc_guests): random.shuffle(products_dessert) products_dessert_shuffled = ([ Product.objects.get(id=i) for i in products_dessert ]) products_dessert_pesc = (products_dessert_pesc + products_dessert_shuffled[0:1]) products_list_main = (products_main_unspecified + products_main_vegan + products_main_veggie + products_main_pesc + products_hot_food) products_list_side = (products_side_unspecified + products_side_vegan + products_side_veggie + products_side_pesc) products_list_dessert = (products_dessert_unspecified + products_dessert_vegan + products_dessert_veggie + products_dessert_pesc) total_cost = 0 for product in products_list_dessert: category = product.category name = product.name price = product.price servings = 1 """ Randomise the available option """ try: category_option = Options.objects.get(category=category) rnd = random.randint(1, 3) if rnd == 1: option = "selected-one" elif rnd == 2: option = "selected-two" elif rnd == 3: option = "selected-three" except ObjectDoesNotExist: option = "none" """ check for existing basket(s) with the current cookie value """ try: existing_basket = Basket.objects.get(cookie=cookie, category=category, name=product, option=option) existing_servings = existing_basket.servings """ add the new servings variable to the existing """ updated_servings = existing_servings + int(servings) price_per_person = float(price) * float(servings) total_price = float(price) * float(updated_servings) """ save the updated basket and delete the existing """ updated_basket = Basket(cookie=cookie, category=category, name=name, servings=updated_servings, option=option, total_price=total_price) updated_basket.save() existing_basket.delete() except ObjectDoesNotExist: """ if there is no existing basket, create a new one """ total_price = float(price) * float(servings) price_per_person = total_price basket = Basket(cookie=cookie, category=category, name=name, servings=servings, option=option, total_price=total_price) basket.save() total_cost = total_cost + price_per_person for product in products_list_side: category = product.category name = product.name price = product.price servings = 1 """ Randomise the available option """ try: category_option = Options.objects.get(category=category) rnd = random.randint(1, 3) if rnd == 1: option = "selected-one" elif rnd == 2: option = "selected-two" elif rnd == 3: option = "selected-three" except ObjectDoesNotExist: option = "none" """ check for existing basket(s) with the current cookie value """ try: existing_basket = Basket.objects.get(cookie=cookie, category=category, name=product, option=option) existing_servings = existing_basket.servings """ add the new servings variable to the existing """ updated_servings = existing_servings + int(servings) price_per_person = float(price) * float(servings) total_price = float(price) * float(updated_servings) """ save the updated basket and delete the existing """ updated_basket = Basket(cookie=cookie, category=category, name=name, servings=updated_servings, option=option, total_price=total_price) updated_basket.save() existing_basket.delete() except ObjectDoesNotExist: """ if there is no existing basket, create a new one """ total_price = float(price) * float(servings) price_per_person = total_price basket = Basket(cookie=cookie, category=category, name=name, servings=servings, option=option, total_price=total_price) basket.save() total_cost = total_cost + price_per_person for product in products_list_main: category = product.category name = product.name price = product.price servings = 1 """ Randomise the available option """ try: category_option = Options.objects.get(category=category) rnd = random.randint(1, 3) if rnd == 1: option = "selected-one" elif rnd == 2: option = "selected-two" elif rnd == 3: option = "selected-three" except ObjectDoesNotExist: option = "none" """ check for existing basket(s) with the current cookie value """ try: existing_basket = Basket.objects.get(cookie=cookie, category=category, name=product, option=option) existing_servings = existing_basket.servings """ add the new servings variable to the existing """ updated_servings = existing_servings + int(servings) price_per_person = float(price) * float(servings) total_price = float(price) * float(updated_servings) """ save the updated basket and delete the existing """ updated_basket = Basket(cookie=cookie, category=category, name=name, servings=updated_servings, option=option, total_price=total_price) updated_basket.save() existing_basket.delete() except ObjectDoesNotExist: """ if there is no existing basket, create a new one """ total_price = float(price) * float(servings) price_per_person = total_price basket = Basket(cookie=cookie, category=category, name=name, servings=servings, option=option, total_price=total_price) basket.save() total_cost = total_cost + price_per_person average_cost = total_cost / bartholemew_ave_guests # https://stackoverflow.com/questions/1995615/ # how-can-i-format-a-decimal-to-always-show-2-decimal-places context = { 'bartholemew_event_type': bartholemew_event_type, 'products_list_main': products_list_main, 'products_list_side': products_list_side, 'products_list_dessert': products_list_dessert, 'total_cost': f'{total_cost:.2f}', 'average_cost': f'{average_cost:.2f}', 'menu': menu, } return render(request, 'bartholemew/bartholemew_output.html', context)