Exemplo n.º 1
0
def create_drink(jwt):
    """
    POST /drinks
        it should create a new row in the drinks table
        it should require the 'post:drinks' permission
        it should contain the drink.long() data representation
        returns status code 200 and json {"success": True, "drinks": drink}
        where drink an array containing only the newly created drink
        or appropriate status code indicating reason for failure
    """
    input_data = request.get_json()
    title = input_data.get('title', None)
    recipe = input_data.get('recipe', None)
    if not title or not recipe:
        abort(422)
    new_drink = Drink(title=title, recipe=json.dumps(recipe))
    try:
        new_drink.insert()
    except Exception as e:
        print(str(e))
        abort(422)
    result = {
        "success": True,
        "drinks": new_drink.long()
    }
    return jsonify(result)
Exemplo n.º 2
0
def create_drinks(payload):
    error = False
    fieldErr = False
    try:
        body = request.get_json()
        title = body.get('title', None)
        recipe = body.get('recipe', None)
        id = body.get('id', None)
        # if search term is epmty
        if ((title is None) or (title == "") or (recipe is None)):
            fieldErr = True
        else:
            # add drink to db
            added_drink = Drink(title=title, recipe=json.dumps(recipe))
            added_drink.insert()
            formatted_drink = added_drink.long()

    except Exception as e:
        error = True
    finally:
        if error:
            abort(500)
        elif fieldErr:
            abort(400)
        elif formatted_drink is None:
            abort(404)
        else:
            return jsonify({
                'success': True,
                'drinks': formatted_drink
            })
Exemplo n.º 3
0
def update_drink(payload, drink_id):

    body = request.get_json()

    if not body:
        abort(400)

    if not ('title' in body or 'recipe' in body):
        abort(422)

    try:
        title = body.get('title')
        recipe = body.get('recipe')

        drink_obj = Drink.query.filter(Drink.id == drink_id).first()

        if not drink_obj:
            abort(404)

        if title:
            drink_obj.title = title
        if recipe:
            drink_obj.recipe = json.dumps(recipe)

        drink_obj.update()

        return jsonify({
            'success': True,
            'drinks': [Drink.long(drink_obj)]
        }), 200
    except Exception:
        abort(422)
Exemplo n.º 4
0
def add_drink(permissions):
    drink = request.get_json()
    drink = Drink(title=drink['title'], recipe=json.dumps(drink['recipe']))
    drink.insert()
    return jsonify({
        "success": True,
        "code": 200,
        "drinks": drink.long()
    })
Exemplo n.º 5
0
def create_drinks(jwt):
    try:
        drink = Drink(title=request.json["title"],
                      recipe=json.dumps(request.json["recipe"]))
        drink.insert()

        return jsonify({"success": True, "drinks": [drink.long()]})
    except:
        # If the payload is malformed.
        abort(400)
Exemplo n.º 6
0
 def create_drink(payload):
     try:
         body = request.get_json()
         new_drink = Drink(title=body['title'], recipe=json.dumps(body['recipe']))
         db.session.add(new_drink)
         db.session.commit()
     except:
         db.session.rollback()
         db.session.close()
     return jsonify({
     'success': True,
     'drinks': new_drink.long()
     })
Exemplo n.º 7
0
def drinks_insert(payload):
    try:
        data = json.loads(request.data)
        drink = Drink()
        if data.get('title', '') is None or data.get('title', '') == '':
            abort(400, "title must be submitted")
        drink.title = data.get('title', '')
        if data.get('recipe', []) is None or data.get('recipe', []) == []:
            abort(400, "recipe must be submitted")
        drink.recipe = str(data.get('recipe'))
        drink.insert()
        return jsonify({'success': True, 'drinks': [drink.long()]}), 200
    except exc.SQLAlchemyError as e:
        abort(500, ','.join(e.orig.args))
Exemplo n.º 8
0
def post_drink(payload):
    body = request.get_json()

    try:
        title = body['title']
        recipe = body['recipe']

        drink = Drink(title=title, recipe=recipe)
        drink.insert()

        return jsonify({"success": True, "drinks": [drink.long(drink)]})

    except:
        abort(422)
Exemplo n.º 9
0
Arquivo: api.py Projeto: kilauea/FSND
def create_drinks(jwt):
    body = request.get_json()
    try:
        # Create a new drink
        title = body.get('title', None)
        recipe = body.get('recipe', None)
        if not title:
            abort(422)
        drink = Drink(title=title, recipe=json.dumps(recipe))
        drink.insert()

        return jsonify({'success': True, 'drinks': [drink.long()]})
    except:
        print(sys.exc_info())
        abort(422)
Exemplo n.º 10
0
def post_drinks(payload):
    data = request.get_json()
    new_title = data['title']
    new_recipe = data['recipe']
    if isinstance(new_recipe, dict):
        new_recipe = [new_recipe]
    try:
        drink = Drink()
        drink.title = new_title
        drink.recipe = json.dumps(new_recipe)
        drink.insert()
    except:
        abort(422)
    long_drinks = drink.long()
    return jsonify({'success': True, 'drinks': long_drinks})
Exemplo n.º 11
0
def create_drink(token):
    body = request.get_json()

    new_title = body.get('title', None)
    new_recipe = body.get('recipe', None)

    try:
        drink = Drink(title=new_title, recipe=new_recipe)
        drink.insert()
        drinks = Drink.query.all()
        drink_long = [drink.long() for drink in drinks]

        return jsonify({'success': True, 'drinks': drink_long})
    except:
        abort(422)
Exemplo n.º 12
0
def create_drink(payload):
    try:
        body = request.get_json()
        new_title = body.get('title')
        new_recipe = json.dumps(body.get('recipe'))
        drink = Drink(title=new_title, recipe=new_recipe)
        drink.insert()
        new_drinks = [drink.long()]
        return jsonify({
            'success': True,
            'drinks': new_drinks
        })

    except:
        abort(422)
Exemplo n.º 13
0
def post_drinks(payload):
    if request.data == b'':
        abort(400)
    body = request.get_json()
    if 'title' not in body or 'recipe' not in body:
        abort(400)

    try:
        drink = Drink(title=body['title'], recipe=json.dumps(body['recipe']))
        drink.insert()
        drinks = [drink.long()]
        return jsonify({'success': True, 'drinks': drinks})
    except Exception as e:
        print(e)
        abort(422)
Exemplo n.º 14
0
def create_drink(payload):
    # Get the body
    req = request.get_json()
    try:
        # create new Drink
        drink = Drink()
        drink.title = req['title']
        # convert recipe to String
        drink.recipe = json.dumps(req['recipe'])
        # insert the new Drink
        drink.insert()

    except Exception:
        abort(400)

    return jsonify({'success': True, 'drinks': [drink.long()]})
Exemplo n.º 15
0
def postDrinks(jwt):
    try:
        data_json = request.get_json()

        if not ("title" in data_json and "recipe" in data_json):
            abort(400)

        new_title = data_json["title"]
        new_recipe = data_json["recipe"]

        new_drink = Drink(title=new_title, recipe=json.dumps(new_recipe))
        new_drink.insert()

        return jsonify({"success": True, "drinks": [new_drink.long()]})

    except:
        abort(422)
Exemplo n.º 16
0
def create_drink():
    try:
        body = request.get_json()
        drink_title = body.get('title', None)
        drink_recipe = body.get('recipe', None)
        drink = Drink(
            title=drink_title,
            recipe=json.dumps(drink_recipe)
        )
        drink.insert()
        return jsonify({
            'success': True,
            'drinks': [drink.long()]
        })
    except:
        print(sys.exc_info())
        abort(422)
Exemplo n.º 17
0
def create_drink(payload):
    req = request.get_json()

    try:
        req_recipe = req['recipe']
        if isinstance(req_recipe, dict):
            req_recipe = [req_recipe]

        drink = Drink()

        drink.title = req['title']
        drink.recipe = json.dumps(req_recipe)

        drink.insert()

    except BaseException:
        abort(400)

    return jsonify({'success': True, 'drinks': [drink.long()]})
Exemplo n.º 18
0
def patch_drink(payload, drink_id):
    res = request.get_json()
    
    if not res:
        abort(401)
    updated_title = res.get('title', None)
    updated_recipe = res.get('recipe', None)

    selected_drink = Drink.query.get(drink_id)
    if selected_drink:
    
        if updated_title:
            selected_drink.title = updated_title
        if updated_recipe:
            selected_drink.recipe =  json.dumps(res['recipe'])
        selected_drink.update()
        return jsonify({
            'success': True,
            'drinks': [Drink.long(selected_drink)]
        }), 200
Exemplo n.º 19
0
def create_drink(payload):
    print(payload)
    body = request.get_json(payload)

    if 'title' and 'recipe' not in body:
        abort(422)

    try:
        title = body['title']
        recipe = body['recipe']

        # if type(recipe)!=list:
        #     recipe=[recipe]

        drink = Drink(title=title, recipe=json.dumps(recipe))
        drink.insert()

        return jsonify({'success': True, 'drinks': [drink.long()]}), 200

    except Exception as e:
        print(e)
        abort(422)
Exemplo n.º 20
0
 def post_drink(payload):
     body = request.get_json()
     if body is None:
         abort(404)
     title = body.get('title', None)
     recipe = body.get('recipe', None)
     # verify id there is no duplicate
     duplicate = Drink.query.filter(Drink.title == title).one_or_none()
     if duplicate is not None:
         abort(400)
     # if the reciepe has not been inputed as a list
     if type(recipe) is not list:
         recipe = [recipe]
     try:
         new_drink = Drink(title=title, recipe=json.dumps(recipe))
         new_drink.insert()
         return jsonify({
             'success': True,
             'drinks': [new_drink.long()]
         })
     except Exception as error:
         abort(422)