Exemplo n.º 1
0
def create_ingredients(request):
    add_ingredient_form = AddIngredientForm(request.POST)

    if add_ingredient_form.is_valid():
        ingredient_name = add_ingredient_form.cleaned_data['name']
        add_ingredient_form.fields.pop('name')
        nutrient = add_ingredient_form.save()
        ingredient = Ingredient(name=ingredient_name, nutrient=nutrient)
        try:
            ingredient.save()

            edit_icon_html = _get_edit_icon_html()
            remove_icon_html = _get_remove_icon_html()

            return JsonResponse(
                {
                    'ingredient': ingredient.to_dict(),
                    'ingredient_actions': edit_icon_html + remove_icon_html
                },
                status=200)
        except IntegrityError:
            return JsonResponse(
                {'errors': {
                    'name': [u'Ingredient already exists.']
                }},
                status=400)
    return JsonResponse({'errors': add_ingredient_form.errors}, status=400)
Exemplo n.º 2
0
def addToIngredients():

    form = IngredientForm()
    form['csrf_token'].data = request.cookies['csrf_token']
    if form.validate_on_submit():
        ingredient = Ingredient(
            name=form.data['name'],
            type=form.data['type'],
            editable=True,
        )
        db.session.add(ingredient)
        db.session.commit()
        # print(ingredient)
        return ingredient.to_dict()
    return {'errors': validation_errors_to_error_messages(form.errors)}, 401
Exemplo n.º 3
0
def add_ingredient():
    print("PING!")
    ingredient_name = request.json.get('name').lower()
    print(ingredient_name)
    if ingredient_name == None:
        abort(400)

    ing = Ingredient.query.filter(Ingredient.name == ingredient_name)
    if ing.count() == 0:
        ing = Ingredient(name=ingredient_name)
        db.session.add(ing)
        db.session.commit()
        return jsonify(ing.to_dict(include_recipes=True)), 201
    else:
        response = jsonify(ing[0].to_dict(include_recipes=True))
        response.status_code = 409
        response.headers['location'] = '/ingredients/{}'.format(ing[0].id)
        return response
Exemplo n.º 4
0
def upload_Ingredient():
    if "file" not in request.files:
        return "No user_file key in request.files"

    file = request.files["file"]

    if file:
        file_url = upload_file_to_s3(file, Config.S3_BUCKET)

        file = Ingredient(name=request.form.get('name'),
                          content=request.form.get('content'),
                          recipeId=request.form.get('recipeId')

                          #  url=file_url
                          )
        db.session.add(file)
        db.session.commit()
        return file.to_dict()
    else:
        return "No File Attached!"