Example #1
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!"
        )
Example #2
0
def user_login():
    data = req_helper.force_json_key_list('username', 'password')
    token, user = User.login(data['username'], data['password'])
    if token:
        return jsonify(token=token, kind=user.kind)
    else:
        abort(make_response(jsonify(message="Incorrect login!"), 400))
Example #3
0
def user_create():
    data = req_helper.force_json_key_list('username', 'password', 'email',
                                          'name', 'kind')

    # If trying to create a non-costumer without token
    if data['kind'] is not User.COSTUMER:
        if 'token' not in data:
            abort(make_response(jsonify(message="Missing token!"), 403))
        usr = User.usr_from_token(data['token'])
        # if token is not valid or user cannot create users
        if not usr or not usr.canEditUsers():
            abort(make_response(jsonify(message="Invalid token!"), 403))

    if not data['username'].strip():
        req_helper.throw_operation_failed("Username empty!")

    if not data['password'].strip() or len(data['password']) < 4:
        req_helper.throw_operation_failed(
            "Password empty or shorter than 4 chars!")

    user = User.create(data['username'], data['password'], data['name'],
                       data['email'], data['kind'])
    if user:
        return jsonify(message="Ok!", id=user.get_id(), kind=user.kind)
    else:
        abort(
            make_response(jsonify(message="Username taken or invalid kind!"),
                          400))
Example #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!")
Example #5
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()
Example #6
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())
Example #7
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!")
Example #8
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()
Example #9
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!")
Example #10
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)
Example #11
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))