コード例 #1
0
def get_by_id(recipe_id):
    req_helper.force_session_get_user()
    result = Recipe.query_id(recipe_id)
    if result is not None:
        return jsonify(result.__dict__)
    else:
        req_helper.throw_not_found()
コード例 #2
0
def tables_get():
    req_helper.force_session_get_user()
    tables = load_kv('tables')
    if tables is not None:
        return jsonify(tables=tables)
    else:
        req_helper.throw_teapot()
コード例 #3
0
def recipe_query_name(name):
    req_helper.force_session_get_user()
    recipes = [
        val.__dict__
        for val in Recipe.query({'name': {
            '$regex': '(?i)' + name
        }})
    ]
    return jsonify(recipes)
コード例 #4
0
def material_find():
    req_helper.force_session_get_user()
    data = req_helper.force_json_key_list('material-id')

    mat = Material.get_from_id(data['material-id'])

    if mat:
        return jsonify(mat.__dict__)
    else:
        req_helper.throw_not_found("Material not found!")
コード例 #5
0
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!"
        )
コード例 #6
0
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!")
コード例 #7
0
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()
コード例 #8
0
def tabs_get_by_user():
    user = req_helper.force_session_get_user()
    if user.is_staff():
        results = Tab.get_waiter_tabs(user.id)
    else:
        results = Tab.get_customer_tabs(user.id)
    out = [tab.toDict() for tab in results]
    return jsonify(out)
コード例 #9
0
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())
コード例 #10
0
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)
コード例 #11
0
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)
コード例 #12
0
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)
コード例 #13
0
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)
コード例 #14
0
def material_create():
    usr = req_helper.force_session_get_user()
    if not usr.canEditMaterials():
        abort(make_response(jsonify(message="Cannot create materials"), 403))

    data = req_helper.force_json_key_list('name', 'img_url', 'units')

    if not data["name"].strip():
        req_helper.throw_operation_failed("Name cannot be empty!")

    if (data['units'] != 'mL') and (data['units'] != 'g'):
        req_helper.throw_operation_failed("Invalid units! Use 'mL' or 'g'")

    mat = Material.create(data['name'], data['img_url'], data['units'])
    return jsonify(message="Success!", id=mat.get_id())
コード例 #15
0
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()
コード例 #16
0
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)
コード例 #17
0
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!")
コード例 #18
0
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()
コード例 #19
0
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!")
コード例 #20
0
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)
コード例 #21
0
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))
コード例 #22
0
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)
コード例 #23
0
def recipe_get_all():
    req_helper.force_session_get_user()
    recipes = [val.__dict__ for val in Recipe.query()]
    return jsonify(recipes)
コード例 #24
0
def user_logout():
    usr = req_helper.force_session_get_user()
    usr.logout()
    return "Ok!"
コード例 #25
0
def get_categories():
    req_helper.force_session_get_user()
    cats = Recipe.get_categories()
    return jsonify(cats)
コード例 #26
0
def get_by_category(category):
    req_helper.force_session_get_user()
    recipes = [
        val.__dict__ for val in Recipe.query({'category': category.lower()})
    ]
    return jsonify(recipes)
コード例 #27
0
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!')
コード例 #28
0
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)