Beispiel #1
0
def add_recipe():
    with open("recipe_final.json", 'r') as f:
        data = json.load(f)

    user = User.objects.get(id=user_id)
    for idx, dt in enumerate(data):
        path_imgs, _ = get_list_images(os.path.join('images',dt['id']))
        # print(len(path_imgs))
        print(dt['name'])
        rec = Recipe()
        rec.name = dt['name']
        rec.user = user
        rec.servings = int(dt['servings'])
        rec.prep = dt['prep']
        # rec.cook = request.POST['cook']
        rec.total = dt['total']
        rec.total_min = parseTimes(dt['total'])
        rec.note = dt['notes']
        rec.rate = 0
        rec.description = dt['description']
        filename = os.path.join('images', dt['images'].split('%')[-1])
        rec.images = filename
        rec.save()

        for i in path_imgs:
            img = ImageRecipe()
            img.recipe = rec
            img.images = i.replace('images/','')
            img.save()

        categorys = dt['category']
        for i in categorys:
            cate = Category()
            cate.recipe = rec
            cate.name = i
            cate.save()

        ingredients = dt['ingredients']
        for i in ingredients:
            ing = Ingredient()
            ing.recipe = rec
            ing.content = i
            ing.save()

        ing_food = IngredientList(dt['ingredients'])
        for i in ing_food.all:
            math_food = MatchFood()
            math_food.recipe = rec
            math_food.food = Food.objects.get(id=i.matched_food.id)
            math_food.save()

        direction = dt['directions']
        for i in direction:
            dire = Direction()
            dire.recipe = rec
            dire.content = i
            dire.save()
        if idx == 100: break
Beispiel #2
0
    def post(self, request):
        rec = Recipe()
        rec.name = request.POST['name']
        if request.POST['note']:
            rec.note = request.POST['note']
        rec.description = request.POST['description']
        rec.servings = int(request.POST['servings'])
        prep, prep_min = parseTimes(request.POST['prep'],
                                    request.POST['prepTimeUnit'])
        cook, cook_min = parseTimes(request.POST['cook'],
                                    request.POST['cookTimeUnit'])
        rec.prep = prep
        rec.cook = cook
        rec.total_min = prep_min + cook_min
        image = request.FILES["image-file"]
        fs = FileSystemStorage()
        name = id_generator() + image.name
        filename = fs.save(name, image)
        rec.images = filename
        rec.rate = 0
        rec.user = request.user
        rec.save()

        img = ImageRecipe()
        img.recipe = rec
        img.images = filename
        img.save()

        categorys = request.POST['category']
        categorys = categorys.split(',')
        for i in categorys:
            if i.strip() == '':
                continue
            cate = Category()
            cate.recipe = rec
            cate.name = i.strip()
            cate.save()

        ingredients = request.POST['ingredient']
        ingredients = ingredients.split('\n')
        for i in ingredients:
            if i.strip() == '':
                continue
            ing = Ingredient()
            ing.recipe = rec
            ing.content = i.strip()
            ing.save()

        direction = request.POST['direction']
        direction = direction.split('\n')
        for i in direction:
            if i.strip() == '':
                continue
            dire = Direction()
            dire.recipe = rec
            dire.content = i.strip()
            dire.save()
        return HttpResponseRedirect('/share_recipe')