Ejemplo n.º 1
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
            })
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def post_drink(payload):
    res = request.get_json()
    if res is None: 
        abort(401)

    if not (res['title'] or res['recipe']):
        abort(400)
    if Drink.query.filter_by(title=res['title']).first():
        abort(400)

    new_title = res['title']
    if isinstance(res['recipe'], str) :
        new_recipe = res['recipe']
    else:
        new_recipe =  '[' + json.dumps(res['recipe']) + ']'
    
    try:
        Drink(title=new_title, recipe=new_recipe).insert()
        
        drink = Drink.query.filter_by(
            title=res['title']).first()
        return jsonify({
            'success': True,
            'drinks': drink.long()
        }), 200
    except:
        abort(401)
Ejemplo n.º 4
0
Archivo: api.py Proyecto: joreeliu/FSND
def add_drinks(jwt):
    if request.data:
        new_drink_data = json.loads(request.data.decode('utf-8'))
        new_drink = Drink(title=new_drink_data['title'],
                          recipe=json.dumps(new_drink_data['recipe']))
        Drink.insert(new_drink)
        drinks = list(map(Drink.long, Drink.query.all()))
        result = {"success": True, "drinks": drinks}
        return jsonify(result)
Ejemplo n.º 5
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()
    })
Ejemplo n.º 6
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)
Ejemplo n.º 7
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()
     })
Ejemplo 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)
Ejemplo n.º 9
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))
Ejemplo 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})
Ejemplo n.º 11
0
Archivo: api.py Proyecto: 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)
Ejemplo n.º 12
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)
Ejemplo n.º 13
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)
Ejemplo n.º 14
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)
Ejemplo n.º 15
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()]})
Ejemplo n.º 16
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)
Ejemplo n.º 17
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)
Ejemplo n.º 18
0
def create_drink(payload):
    data = request.get_json()
    drink = Drink(**data)
    result = drink.insert()

    if result["error"]:
        abort(500)

    _id = result["id"]

    return (
        jsonify({
            "drinks": [Drink.get_by_id(_id).long()],
            "success": True,
        }),
        200,
    )
Ejemplo n.º 19
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()]})
Ejemplo n.º 20
0
def add_drink(payload):

    try:
        body = request.get_json()
        title = body['title']
        recipe = body['recipe']

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

        drink_formatted = drink.format()

        return jsonify({
            'status_code': 200,
            'success': True,
            'drinks': drink_formatted
        })

    except:
        abort(422)
Ejemplo n.º 21
0
def insert_drinks(valid):
    if valid:
        new_drink = request.get_json()
        # the required datatype is [{'color': string, 'name':string, 'parts':number}]
        drink = Drink(
            title=new_drink['title'],
            recipe=json.dumps(new_drink['recipe'])
        )

        try:
            Drink.insert(drink)
        except:
            db.session.rollback()

        finally:
            return jsonify(
                {
                    'success': True,
                    'drinks': list(map(Drink.long, Drink.query.filter(Drink.title == new_drink['title']).all()))
                }
            )
Ejemplo n.º 22
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)
Ejemplo n.º 23
0
def add_new_drink(payload):
    body = request.get_json()

    if not body:
        abort(400)

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

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

    drink_obj = Drink(title=title, recipe=json.dumps(recipe))

    drink_obj.insert()

    newly_added_drink = Drink.query.filter_by(id=drink_obj.id).first()

    return jsonify({
        'success': True,
        'drinks': [newly_added_drink.long()]
    }), 200
Ejemplo n.º 24
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)