async def courier_new(): restaurant_id = request.args.get('restaurant') if not restaurant_id: return redirect(url_for('home')) restaurant = db_session.query(Restaurant).get(restaurant_id) if request.method == 'POST': form = CourierRegisterForm(await request.form) if form.validate(): courier = Courier(vk_id=form.vk_id.data, restaurant=restaurant) db_session.add(courier) db_session.commit() return redirect(url_for('courier.courier_finish')) return await render_template('courier_new.html', title='Стать курьером', logged_in=logged_in(), basket_len=basket_len(), form=form, restaurant=restaurant) form = CourierRegisterForm() return await render_template('courier_new.html', title='Стать курьером', logged_in=logged_in(), basket_len=basket_len(), form=form, restaurant=restaurant)
async def restaurant_orders(): if not logged_in(): return redirect(url_for('login')) restaurant = get_current_restaurant() orders = (order for order in restaurant.orders if order.state != 'Выполнен') return await render_template('restaurant_orders.html', title='Заказы', logged_in=logged_in(), basket_len=basket_len(), orders=orders)
async def menu_add(): if not logged_in(): return redirect(url_for('login')) if request.method == 'POST': form = ProductForm(await request.form) if form.validate(): restaurant = get_current_restaurant() product = Product(name=form.name.data, price=form.price.data, description=form.description.data) restaurant.menu.append(product) db_session.commit() return redirect(url_for('restaurant')) return await render_template('menu_add.html', title='Дополнить меню', form=form, logged_in=True, basket_len=basket_len()) form = ProductForm() return await render_template('menu_add.html', title='Дополнить меню', form=form, logged_in=True, basket_len=basket_len())
async def basket(): if 'basket' not in session: session['basket'] = dict() products = list() for product_id, count in session['basket'].items(): products.append((db_session.query(Product).get(product_id), count)) return await render_template('basket.html', title='Корзина', products=products, basket_len=basket_len(), logged_in=logged_in())
async def menu_remove(product_id): if not logged_in(): return redirect(url_for('login')) product = db_session.query(Product).get(product_id) if not product: abort(404) if product.restaurant.id != get_current_restaurant().id: abort(403) db_session.delete(product) db_session.commit() return redirect(url_for('restaurant', restaurant_id=product.restaurant.id))
async def order_handle(order_id, method): if not logged_in(): return redirect(url_for('login')) if method not in ('upgrade', 'downgrade'): return 'not ok' order = db_session.query(Order).get(order_id) if not order: return 'not ok' restaurant = get_current_restaurant() if order.restaurant.id != restaurant.id: return 'not ok' order_stages = { 'upgrade': { 'В обработке': 'Готовится', 'Готовится': 'Доставляется', 'Доставляется': 'Доставляется', }, 'downgrade': { 'Доставляется': 'Готовится', 'Готовится': 'В обработке', 'В обработке': 'В обработке' } } order.state = order_stages[method][order.state] db_session.merge(order) db_session.commit() return redirect(request.referrer or url_for('order.restaurant_orders'))
async def orders_template(**context): return await render_template('orders.html', logged_in=logged_in(), basket_len=basket_len(), title='Заказы', **context)
async def make_order_template(**context): return await render_template('order_make.html', logged_in=logged_in(), basket_len=basket_len(), title='Оформление заказа', **context)
async def courier_finish(): return await render_template('courier_finish.html', title='Почти курьер', logged_in=logged_in(), basket_len=basket_len())