def recipe_create(): user = req_helper.force_session_get_user() if not user.canEditRecipes(): req_helper.throw_not_allowed() data = req_helper.force_json_key_list('name', 'desc', 'detail', 'img_url', 'cost', 'ingredients', 'time', 'src', 'category') if not data["name"].strip(): req_helper.throw_operation_failed("Name cannot be empty!") if not isinstance(data['ingredients'], list) or len(data['ingredients']) < 1: req_helper.throw_operation_failed( "Ingredients must be a nonempty list!") try: cost = float(data['cost']) time = int(data['time']) except: req_helper.throw_operation_failed("Cost and time need to be integers!") recipe_id = Recipe.create(data['name'], data['desc'], data['detail'], data['img_url'], cost, data['ingredients'], data['src'], time, data['category']) if recipe_id: return jsonify(message="Ok!", id=str(recipe_id)) else: req_helper.throw_operation_failed( "Could not create! Maybe check the ingredients ids or make sure quantities are numbers!" )
def tab_dispatch_order(tab_id, order_id): user = req_helper.force_session_get_user() if not user.is_staff(): req_helper.throw_not_allowed() # Look for tab and abort if not found tab = Tab.tab_from_id(tab_id) if not tab: req_helper.throw_not_found("Specified tab could not be found!") order = tab.get_order(order_id) if order is None: req_helper.throw_not_found("The order id was not found in this tab.") if order['status'] == Tab.Order.SERVED: req_helper.throw_operation_failed("This order has already reached the last status.") if user.is_cook(): if order['status'] == 0: result = tab.dispatch(order_id) else: req_helper.throw_operation_failed("A cook cannot do this.") elif user.is_waiter(): if order['status'] == 1: result = tab.dispatch(order_id) else: req_helper.throw_operation_failed("A cook cannot do this.") else: result = tab.dispatch(order_id) if result: return jsonify(message='Ok!', new_status=order['status']+1) else: req_helper.throw_operation_failed("Failed to dispatch order!")
def tab_close(tab_id): user = req_helper.force_session_get_user() if not user.is_staff(): req_helper.throw_not_allowed() tab = Tab.tab_from_id(tab_id) if not tab: req_helper.throw_not_found("Specified tab could not be found!") tab.close()
def tab_preview(tab_id): user = req_helper.force_session_get_user() tab = Tab.tab_from_id(tab_id) if not tab: req_helper.throw_not_found("Specified tab could not be found!") if not user.canEditTabs() and (user.id not in [val['id'] for val in tab.customers]): req_helper.throw_not_allowed(f"You're not allowed to view tab {tab_id}.") return jsonify(tab.toDict())
def user_query_type(kind): usr = req_helper.force_session_get_user() if not usr.is_admin(): req_helper.throw_not_allowed() if kind is not None and not User.valid_kind(kind): req_helper.throw_operation_failed("Invalid type!") users = User.query_users(kind=kind, remove=['password']) return jsonify(users)
def tables_get_available(): user = req_helper.force_session_get_user() if not user.canEditTabs(): req_helper.throw_not_allowed() tables = load_kv('tables') used_tables = get_db().tabs.find().distinct("table") available = [i for i in range(1, tables + 1) if i not in used_tables] return jsonify(available)
def inventory_query_expired(): user = req_helper.force_session_get_user() if not user.canEditInventory(): req_helper.throw_not_allowed() result = [ val.__dict__ for val in Item.query( {'expiration': { '$lt': datetime.datetime.today() }}) ] return jsonify(result)
def get_orders(): user = req_helper.force_session_get_user() if not user.is_staff(): req_helper.throw_not_allowed() if user.is_waiter(): # Get ready out = Tab.get_orders(1) elif user.is_cook(): # Get unready out = Tab.get_orders(0) else: # Get all out = Tab.get_orders() return jsonify(out)
def inventory_checkout(): user = req_helper.force_session_get_user() if not user.canEditInventory(): req_helper.throw_not_allowed() data = req_helper.force_json_key_list('inventory-id') item = Item.get_from_id(data['inventory-id']) if not item: req_helper.throw_not_found("Item not found!") if item.destroy() == 1: return jsonify(message="Ok!") else: req_helper.throw_operation_failed()
def tab_add_customer(tab_id): user = req_helper.force_session_get_user() tab = Tab.tab_from_id(tab_id) data = req_helper.force_json_key_list('username') if not tab: req_helper.throw_not_found("Specified tab could not be found!") if not user.canEditTabs() and (user.id not in [val['id'] for val in tab.customers]): req_helper.throw_not_allowed(f"You're not allowed to add costumers to tab {tab_id}.") if tab.addCustomer(data['username']): return jsonify(message="Ok!") else: req_helper.throw_operation_failed("Failed to add user!")
def inventory_query(material_id): user = req_helper.force_session_get_user() if not user.canEditInventory(): req_helper.throw_not_allowed() if req_helper.get_optional_key('filter-expired', default=False, force_instance=True): result = [ val.__dict__ for val in Item.query_items(material_id) if not val.expired ] else: result = [val.__dict__ for val in Item.query_items(material_id)] return jsonify(result)
def user_delete(): usr = req_helper.force_session_get_user() if not usr.canEditUsers(): req_helper.throw_not_allowed() data = req_helper.force_json_key_list('user-id') user = User.get_from_id(data['user-id']) if not user: req_helper.throw_not_found("User not found!") user.logout() if user.destroy() == 1: return jsonify(message="Ok!") else: req_helper.throw_operation_failed()
def tab_add_order(tab_id): user = req_helper.force_session_get_user() tab = Tab.tab_from_id(tab_id) data = req_helper.force_json_key_list('recipe-id') if not tab: req_helper.throw_not_found("Specified tab could not be found!") if not user.canEditTabs() and (user.id not in [val['id'] for val in tab.customers]): req_helper.throw_not_allowed(f"You're not allowed to add orders to tab {tab_id}.") out = tab.addOrder(data['recipe-id']) # Weird thing to make sure it catches only a False and not a 0 if not(out is False): return jsonify(message="Ok!", time=out) else: req_helper.throw_operation_failed("Failed to add order!")
def tab_create(): user = req_helper.force_session_get_user() if not user.canEditTabs(): req_helper.throw_not_allowed() data = req_helper.force_json_key_list('table') try: table = int(data['table']) except: req_helper.throw_operation_failed("Table must be an integer") if 'customers' in data and isinstance(data['customers'], list) and len(data['customers']) > 0: customers = data['customers'] else: customers = None tab_id = Tab.create(user, table, datetime.now(), customers) if not tab_id: req_helper.throw_operation_failed("Could not create! Maybe check usernames!") else: return jsonify(message='Ok!', id=tab_id)
def inventory_create(): user = req_helper.force_session_get_user() if not user.canEditInventory(): req_helper.throw_not_allowed() data = req_helper.force_json_key_list('material-id', 'expiration-date', 'size', 'cost', 'location') expiration_date = req_helper.validate_date_format(data['expiration-date']) if expiration_date < datetime.datetime.now(): req_helper.throw_operation_failed("Cannot add expired items!") if (data['location'] != 'bar') and (data['location'] != 'restaurant'): req_helper.throw_operation_failed( "Invalid location! Use 'bar' or 'restaurant'") amount = req_helper.get_optional_key('amount', 1) try: amount = int(amount) except: req_helper.throw_operation_failed("Boi, that amount is not a number!") if amount < 1: req_helper.throw_operation_failed( "Boi, don't send negative/zero amounts!") cost = data['cost'] / amount today = datetime.datetime.today() for _ in range(amount): item = Item.create(data['material-id'], expiration_date, today, cost, data['size'], data['location']) # if error if not item: req_helper.throw_operation_failed( "Could not create! Maybe check the material-id.") return jsonify(message="Ok!", id=str(item.id))
def tables_set(number): user = req_helper.force_session_get_user() if not user.is_admin(): req_helper.throw_not_allowed() save_kv('tables', number) return jsonify(message='Ok!')
def tab_all(): user = req_helper.force_session_get_user() if not user.is_staff(): req_helper.throw_not_allowed() out = [tab.toDict() for tab in Tab.query()] return jsonify(out)
def materials_all(): usr = req_helper.force_session_get_user() if usr.is_customer(): req_helper.throw_not_allowed() materials = Material.query_materials() return jsonify(materials)