예제 #1
0
 def post(self):
     form = json.loads(self.request.get('form'))
     name = form['name']
     price = float(form['price']) * (4.0 / 3.0)  # 750ml to 1L
     abv = int(form['abv'])
     ingredient = Ingredient(name=name, price=price, abv=abv)
     ingredient.put()
예제 #2
0
파일: main.py 프로젝트: DanAlbert/bar2d2
 def post(self):
     form = json.loads(self.request.get('form'))
     name = form['name']
     price = float(form['price']) * (4.0 / 3.0)  # 750ml to 1L
     abv = int(form['abv'])
     ingredient = Ingredient(name=name, price=price, abv=abv)
     ingredient.put()
예제 #3
0
def recipes_new():
    # inserts a new recipe

    if not request.args.get('ingredients'):
        return json.dumps({"error": "No ingredients provided"}), 500

    if not request.args.get('instructions'):
        return json.dumps({"error": "No instructions provided"}), 500

    recipeName = request.args.get('name')

    if not recipeName:
        return json.dumps({"error": "No name provided"}), 500

    prepTime = request.args.get('prepTime')

    if not prepTime:
        return json.dumps({"error": "No prepTime provided"}), 500

    category = request.args.get('category')
    ingredientList = request.args.get('ingredients').split(',')
    ingredientList = [x.lower() for x in ingredientList]
    instructionList = request.args.get('instructions').split(',')

    # error handling
    if prepTime.isdigit():
        prepTime = int(prepTime)
        if prepTime <= 0:
            return json.dumps({"error": "Invalid prep time"}), 500
    else:
        return json.dumps({"error": "Invalid prep time"}), 500

    if category not in ['breakfast', 'lunch', 'dinner']:
        return json.dumps({
            "error":
            "Invalid category.  Valid categories are breakfast, lunch, and dinner"
        }), 500

    if len(ingredientList) < 1:
        return json.dumps({"error": "Need at least one ingredient"}), 500

    if len(instructionList) < 1:
        return json.dumps({"error": "Need at least one instruction"}), 500

    # get ingredient keys
    ingredient_keys = []
    new_ingredients = []
    for ingName in ingredientList:
        ings = db.GqlQuery("SELECT * FROM Ingredient WHERE name = :1", ingName)
        result_count = 0
        for item in ings:
            result_count += 1
            ingredient_keys.append(item.key())
        if result_count == 0:
            new_ingredients.append(ingName)

    newRecipe = Recipe(name=recipeName,
                       prepTime=prepTime,
                       category=category,
                       ingredients=[],
                       instructions=instructionList)
    newRecipe.put()
    recipeKey = newRecipe.key()

    # create new ingredients if necessary
    for n in new_ingredients:
        newIngredient = Ingredient(name=n, usedIn=[recipeKey])
        newIngredient.put()
        ingredient_keys.append(newIngredient.key())
    newRecipe.ingredients = ingredient_keys

    newRecipe.put()

    return json.dumps({
        'status': "OK",
        'message': "Recipe saved successfully"
    }), 200