Ejemplo n.º 1
0
def add_item():
    j = request.get_json()
    c = DatabaseConnection()

    auth = check_auth(j, c)
    if auth:
        return auth

    vs = [None, None, None, None, None, None]
    varnames = ["listid", "label", "descr", "img", "url", "price"]

    for i in range(len(varnames)):
        vs[i] = j.get(varnames[i], None)
        if not vs[i]:
            return {"err": varnames[i] + " must not be empty"}, 400

    user = c.get_user(username=j["username"])
    l = c.get_list(vs[0])
    if not l:
        return {"err": "list does not exist"}, 409

    if user["id"] != l["userid"]:
        return {"err": "list does not belong to user"}, 400

    if not c.add_item(vs[0], vs[1], vs[2], vs[3], vs[4], vs[5]):
        return {
            "err": "attempting to add item with duplicate label to list"
        }, 409

    return {"msg": "successfully added item to list"}, 201