Ejemplo n.º 1
0
def update_drink(payload, drink_id):
    drink = Drink.get_by_id(drink_id)

    if drink is None:
        abort(404)

    data = request.get_json()
    title = data.get("title", None)
    recipe = json.dumps(data.get("recipe", None))

    if title is None or recipe is None:
        abort(400)

    drink.title = title
    drink.recipe = recipe
    result = drink.insert()

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

    return (
        jsonify({
            "drinks": [Drink.get_by_id(drink_id).long()],
            "success": True,
        }),
        200,
    )
Ejemplo 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
            })
Ejemplo n.º 3
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.º 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()
    })
Ejemplo n.º 5
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.º 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 delete_drinks(valid, id):
    if valid:
        data = Drink.query.get(id)
        if data:
            Drink.delete(data)
        else:
            raise abort(404)

        return jsonify({
            'success': True,
            'delete': id
        })
Ejemplo n.º 8
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.º 9
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.º 10
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.º 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)
Ejemplo 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)
Ejemplo n.º 13
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.º 14
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)
Ejemplo n.º 15
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.º 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(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.º 18
0
Archivo: api.py Proyecto: joreeliu/FSND
def delete_drinks(jwt, drink_id):
    drink_data = None
    try:
        drink_data = Drink.query.get(drink_id)
    except Exception as e:
        logging.error(e)
        abort(404)
    if not drink_data:
        abort(404)
    try:
        Drink.delete(drink_data)
    except Exception as e:
        logging.error(e)
        abort(422)
    drinks = list(map(Drink.long, Drink.query.all()))
    result = {"success": True, "drinks": drinks}
    return jsonify(result), 200
Ejemplo n.º 19
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.º 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 update_drinks(valid, id):

    if valid:
        drink = Drink.query.filter(Drink.id == id).one_or_none()

        if drink:
            new_drink = request.get_json()
            if 'title' in new_drink:
                drink.title = new_drink['title']
            if 'recipe' in new_drink:
                drink.recipe = json.dumps(new_drink['recipe'])

            try:
                Drink.update(drink)
            except Exception as e:
                db.session.rollback()

            return jsonify({
                'success': True,
                'drinks': list(map(Drink.long, Drink.query.filter(Drink.title == drink.title).all()))
            })
Ejemplo n.º 23
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.º 24
0
def get_drinks_detail(payload):
    drinks = Drink.get_all()

    if len(drinks) == 0:
        abort(404)

    return (
        jsonify({
            "drinks": [drink.long() for drink in drinks],
            "success": True
        }),
        200,
    )
Ejemplo n.º 25
0
def get_drinks():
    drinks = Drink.get_all()

    if len(drinks) == 0:
        abort(404)

    return (
        jsonify({
            "drinks": [drink.short() for drink in drinks],
            "success": True
        }),
        200,
    )
Ejemplo n.º 26
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.º 27
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)
Ejemplo n.º 28
0
def delete_drink(payload, drink_id):
    drink = Drink.get_by_id(drink_id)

    if drink is None:
        abort(404)

    result = drink.delete()

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

    return jsonify({
        "delete": drink_id,
        "success": True,
    })
Ejemplo n.º 29
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.º 30
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})