166 lines
4.1 KiB
Python
166 lines
4.1 KiB
Python
from flask import Blueprint, redirect, render_template, request, session
|
|
|
|
from app.services.order import create_order
|
|
|
|
shop_bp = Blueprint("shop", __name__)
|
|
|
|
MENU = [
|
|
{
|
|
"id": 1,
|
|
"name": "Пепперони",
|
|
"price": 450,
|
|
"category": "Пицца",
|
|
},
|
|
{
|
|
"id": 2,
|
|
"name": "Маргарита",
|
|
"price": 350,
|
|
"category": "Пицца",
|
|
},
|
|
{
|
|
"id": 3,
|
|
"name": "Цезарь",
|
|
"price": 320,
|
|
"category": "Салаты",
|
|
},
|
|
{
|
|
"id": 4,
|
|
"name": "Греческий",
|
|
"price": 280,
|
|
"category": "Салаты",
|
|
},
|
|
{
|
|
"id": 5,
|
|
"name": "Кола 0.5",
|
|
"price": 100,
|
|
"category": "Напитки",
|
|
},
|
|
{
|
|
"id": 6,
|
|
"name": "Сок апельсиновый",
|
|
"price": 150,
|
|
"category": "Напитки",
|
|
},
|
|
{
|
|
"id": 7,
|
|
"name": "Сок яблочный",
|
|
"price": 150,
|
|
"category": "Напитки",
|
|
},
|
|
{
|
|
"id": 8,
|
|
"name": "Сок ягодный",
|
|
"price": 150,
|
|
"category": "Напитки",
|
|
},
|
|
{
|
|
"id": 9,
|
|
"name": "Сок цитрусовый",
|
|
"price": 150,
|
|
"category": "Напитки",
|
|
},
|
|
]
|
|
|
|
|
|
def _get_item(item_id: int):
|
|
return next((item for item in MENU if item["id"] == item_id), None)
|
|
|
|
|
|
def _collect_cart():
|
|
items = []
|
|
item_names = []
|
|
total = 0
|
|
for item_id in session.get("cart", []):
|
|
item = _get_item(item_id)
|
|
if item:
|
|
items.append(item)
|
|
item_names.append(item["name"])
|
|
total += item["price"]
|
|
return items, item_names, total
|
|
|
|
|
|
@shop_bp.route("/")
|
|
def index():
|
|
return render_template("index.html", menu=MENU)
|
|
|
|
|
|
@shop_bp.route("/add_to_cart/<int:item_id>", methods=["POST"])
|
|
def add_to_cart(item_id: int):
|
|
if "cart" not in session:
|
|
session["cart"] = []
|
|
session["cart"].append(item_id)
|
|
return redirect("/")
|
|
|
|
|
|
@shop_bp.route("/remove_from_cart/<int:item_id>", methods=["POST"])
|
|
def remove_from_cart(item_id: int):
|
|
if "cart" in session and item_id in session["cart"]:
|
|
session["cart"].remove(item_id)
|
|
return redirect("/cart")
|
|
|
|
|
|
@shop_bp.route("/cart")
|
|
def cart():
|
|
cart_items, _, total = _collect_cart()
|
|
return render_template("cart.html", items=cart_items, total=total)
|
|
|
|
|
|
@shop_bp.route("/order")
|
|
def order():
|
|
_, _, total = _collect_cart()
|
|
return render_template(
|
|
"order.html",
|
|
cart_total=total,
|
|
form_data={"name": "", "phone": "", "address": "", "comment": ""},
|
|
)
|
|
|
|
|
|
@shop_bp.route("/place_order", methods=["POST"])
|
|
def place_order():
|
|
name = request.form.get("name", "").strip()
|
|
phone = request.form.get("phone", "").strip()
|
|
address = request.form.get("address", "").strip()
|
|
comment = request.form.get("comment", "").strip()
|
|
_, cart_item_names, total = _collect_cart()
|
|
|
|
if not cart_item_names:
|
|
return render_template(
|
|
"order.html",
|
|
error="Корзина пуста. Добавьте товары перед оформлением.",
|
|
cart_total=total,
|
|
form_data={
|
|
"name": name,
|
|
"phone": phone,
|
|
"address": address,
|
|
"comment": comment,
|
|
},
|
|
), 400
|
|
|
|
if not name or not phone or not address:
|
|
return render_template(
|
|
"order.html",
|
|
error="Заполните имя, телефон и адрес.",
|
|
cart_total=total,
|
|
form_data={
|
|
"name": name,
|
|
"phone": phone,
|
|
"address": address,
|
|
"comment": comment,
|
|
},
|
|
), 400
|
|
|
|
create_order(
|
|
customer_name=name,
|
|
customer_phone=phone,
|
|
customer_address=address,
|
|
customer_comment=comment,
|
|
order_items=", ".join(cart_item_names),
|
|
total_price=total,
|
|
)
|
|
session.pop("cart", None)
|
|
return render_template(
|
|
"order_success.html",
|
|
customer_name=name,
|
|
total=total,
|
|
)
|