コード例 #1
0
def editRecipeIngredients(request, recipe):
    """
    Saves the current RecipeIngredients objects for the current recipe.
    """
    recipeIngredients = Recipeingredients.objects.filter(recipe=recipe)

    quantity_str = "quantity"
    ingredient_str = "ingredient"
    
    for x in recipeIngredients:
        i= request.POST.get(ingredient_str + str(x.id)).strip()
        quantity = request.POST.get(quantity_str + str(x.id)).strip()
        if  quantity and i:
            existingIngredient = Ingredient.objects.filter(name=i)
            if not len(existingIngredient) == 0:
                for ingredient in existingIngredient:
                    x.ingredient = ingredient
                    x.quantity = quantity
            else:
                ingredient = Ingredient()
                ingredient.name = i
                ingredient.save()
                x.ingredient = ingredient
                x.quantity = quantity
            x.save()
コード例 #2
0
def setRecipeIngredients(request, recipe):
    """
    Saves the current ingredients and quantity for Recipe.
    """
    quantity_str = "quantity"
    ingredient_str = "ingredient"
    
    for x in range(1, 12):
        try:
            i= request.POST.get(ingredient_str + str(x))
            quantity = request.POST.get(quantity_str + str(x))
        
            if  quantity and i:
                ri = Recipeingredients()
                existingIngredient = None
                existingIngredient = Ingredient.objects.filter(name=i)
                if not len(existingIngredient) == 0 :
                    for ingredient in existingIngredient:
                        ri.ingredient = ingredient
                        ri.recipe = recipe
                        ri.quantity = quantity
                        ri.save()
                        
                else:
                    ingredient = Ingredient()
                    ingredient.name = i
                    ingredient.save()
                    ri.ingredient = ingredient
                    ri.recipe = recipe
                    ri.quantity = quantity
                    ri.save()
        except:
            return