def admin_reorder_type(type_): data = {int(k): int(v) for k, v in request.json.items()} if type_ == 'drinks': Drink.set_order(data) elif type_ == 'drink-components': DrinkComponent.set_order(data) else: abort(400, "Invalid type: " + str(type_)) return 'OK'
def get_order_printable(order): drink = None if order.drink: drink = Drink.get(order.drink) drink_components = None if order.drink_components: drink_components = DrinkComponent.find(*order.drink_components) strength = f' [{order.strength}]' if order.strength else '' c = RuntimeConfig.get_single() logo = None if c.logo: logo = url_for('index.images', name=c.logo, mode='full', _external=True) return { 'id': order.doc_id, 'logo': logo, 'name': order.name, 'drink_name': (order.drink_name + strength) if order.drink_name else None, 'drink': (drink.name + strength) if drink else None, 'drink_components': ', '.join( (c.name for c in drink_components)) if drink_components else None }
def delete_drink(id): drink = Drink.get(id) if not drink: abort(404, "No such drink") drink.delete() flash("Deleted drink " + drink.name, 'info') return redirect(url_for('.drinks'))
def edit_drink(id=None): drink = None is_new = True if id: is_new = False drink = Drink.get(id) if not drink: abort(404, "No such drink") form = DrinkForm(obj=drink) if form.validate_on_submit(): if not drink: drink = Drink() form.populate_obj(drink) drink.save() flash("Your changes have been saved.", 'success') if is_new: return redirect(url_for('.edit_drink', id=drink.doc_id)) return render_template('admin/edit_drink.jinja.html', form=form)
def add_stat(dest, stat): if stat.drink: drink = Drink.get(stat.drink) if drink: dest['drinks'].setdefault(drink.name, 0) dest['drinks'][drink.name] += 1 dest['strengths'].setdefault(stat.strength, 0) dest['strengths'][stat.strength] += 1 dest['count'] += 1 dest['total_oz'] += est_oz.get(stat.strength, 2)
def complete_order(id): order = Order.get(id) if not order: flash("No such order", 'danger') else: if order.drink: drink = Drink.get(order.drink) if drink and drink.inventory_level is not None: drink.inventory_level = max(0, drink.inventory_level - 1) if drink.inventory_level == 0: drink.in_stock = False drink.save() OrderStat.from_order(order) order.delete() flash(f"Completed order {order.drink_name} for {order.name}", 'success') return redirect(url_for('.orders'))
def index(): drinks = Drink.find(is_orderable=True, in_stock=True, sort_key='order') saved_orders = SavedOrder.all() drink_components = DrinkComponent.find(in_stock=True, sort_key='order') in_stock_drink_components = set([d.doc_id for d in drink_components]) def _filter_saved(o): o_coms = set(o.drink_components) have_coms = o_coms & in_stock_drink_components return have_coms == o_coms saved_orders = list(filter(_filter_saved, saved_orders)) def get_components(ids): return DrinkComponent.find(*ids, sort_key='order') return render_template('index/index.jinja.html', drinks=drinks, saved_orders=saved_orders, drink_components=drink_components, get_components=get_components)
def order(): drink = None drink_components = None drink_name = None order = None if request.args.get('d'): drink = Drink.get(int(request.args['d'])) if not drink: abort(404, "That drink doesn't exist") if not drink.in_stock: abort(400, "That drink is out of stock") elif request.args.get('s'): saved_order = SavedOrder.get(int(request.args['s'])) if not saved_order: abort(404, "No such saved order") drink_name = saved_order.drink_name drink_components = DrinkComponent.find(*saved_order.drink_components, sort_key='order') elif request.args.get('c'): drink_components = DrinkComponent.find(*map(int, request.args.getlist('c')), sort_key='order') if drink_components: if len(drink_components) < len(request.args.getlist('c')): abort(404, "Some of your choices do not exist") if any((not c.in_stock for c in drink_components)): abort(400, "Some of your choices are not in stock") name = None if not is_house_device(): name = session.get('saved_name') form = OrderForm(drink=drink, drink_name=drink_name, name=name) if form.validate_on_submit(): if not is_house_device(): session['saved_name'] = form.name.data params = { 'name': form.name.data, } if drink: params['drink'] = drink.doc_id else: params['drink_name'] = form.drink_name.data or None params['drink_components'] = [c.doc_id for c in drink_components] if not drink or drink.has_strengths: params['strength'] = form.strength.data if not drink and hasattr( form, 'save_for_later') and form.save_for_later.data: SavedOrder(drink_name=params['drink_name'], drink_components=params['drink_components']).save() order = Order(event=Event.get_current_id(), **params) order.save() try: queue_order_to_print(order=order, auto=True) except PrintError: # Should be because we're not auto cutting and there's already a job queued pass flash("Your order has been placed", 'success') return redirect(url_for('.index')) return render_template('index/order.jinja.html', form=form, drink=drink, drink_components=drink_components)
def drinks(): return render_template('admin/drinks.jinja.html', drinks=Drink.all(sort_key='order'))
def get_drink(id): return Drink.get(id)