import json
from django.shortcuts import render, redirect
from .models import RecipeData, Ingredient
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from .forms import IngredientInputForm, SelectMealTypeForm
from django import forms
from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt
from django.core import serializers
from django.middleware.csrf import get_token
from fridge_reciper import settings

from rest_framework import viewsets
from .serializers import RecipeSerializer, IngredientSerializer
from .models import RecipeData


class RecipeViewSet(viewsets.ModelViewSet):
    queryset = RecipeData.objects.all().order_by('name')
    serializer_class = RecipeSerializer


class IngredientViewSet(viewsets.ModelViewSet):
    queryset = Ingredient.objects.all().order_by('name')
    serializer_class = IngredientSerializer


@ensure_csrf_cookie
def csrf(request):
    return JsonResponse({'csrfToken': get_token(request)})


@ensure_csrf_cookie
def process_recipes(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        # extract contents and meal type from POST data
        contents = data["contents"]
        meal_type = data["meal_type"]
        # search for recipes
        all_recipes = RecipeData.objects.all()
        recipe_list = []
        for recipe in all_recipes:
            if recipe.meal_type == meal_type:
                check = all(item in contents for item in recipe.ingredients)
                if check is True:
                    recipe_list.append(recipe)
                else:
                    continue
            else:
                continue

        response = serializers.serialize("json", recipe_list)

        return JsonResponse(response, safe=False)

583b4e82a4d2c3048cb890dd2ad27b8cc0fdb89c
<mask token>


class RecipeViewSet(viewsets.ModelViewSet):
    <mask token>
    <mask token>


class IngredientViewSet(viewsets.ModelViewSet):
    queryset = Ingredient.objects.all().order_by('name')
    serializer_class = IngredientSerializer


<mask token>

<mask token>


class RecipeViewSet(viewsets.ModelViewSet):
    queryset = RecipeData.objects.all().order_by('name')
    serializer_class = RecipeSerializer


class IngredientViewSet(viewsets.ModelViewSet):
    queryset = Ingredient.objects.all().order_by('name')
    serializer_class = IngredientSerializer


<mask token>


@ensure_csrf_cookie
def process_recipes(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        contents = data['contents']
        meal_type = data['meal_type']
        all_recipes = RecipeData.objects.all()
        recipe_list = []
        for recipe in all_recipes:
            if recipe.meal_type == meal_type:
                check = all(item in contents for item in recipe.ingredients)
                if check is True:
                    recipe_list.append(recipe)
                else:
                    continue
            else:
                continue
        response = serializers.serialize('json', recipe_list)
        return JsonResponse(response, safe=False)

<mask token>


class RecipeViewSet(viewsets.ModelViewSet):
    queryset = RecipeData.objects.all().order_by('name')
    serializer_class = RecipeSerializer


class IngredientViewSet(viewsets.ModelViewSet):
    queryset = Ingredient.objects.all().order_by('name')
    serializer_class = IngredientSerializer


@ensure_csrf_cookie
def csrf(request):
    return JsonResponse({'csrfToken': get_token(request)})


@ensure_csrf_cookie
def process_recipes(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        contents = data['contents']
        meal_type = data['meal_type']
        all_recipes = RecipeData.objects.all()
        recipe_list = []
        for recipe in all_recipes:
            if recipe.meal_type == meal_type:
                check = all(item in contents for item in recipe.ingredients)
                if check is True:
                    recipe_list.append(recipe)
                else:
                    continue
            else:
                continue
        response = serializers.serialize('json', recipe_list)
        return JsonResponse(response, safe=False)

import json
from django.shortcuts import render, redirect
from .models import RecipeData, Ingredient
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from .forms import IngredientInputForm, SelectMealTypeForm
from django import forms
from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt
from django.core import serializers
from django.middleware.csrf import get_token
from fridge_reciper import settings
from rest_framework import viewsets
from .serializers import RecipeSerializer, IngredientSerializer
from .models import RecipeData


class RecipeViewSet(viewsets.ModelViewSet):
    queryset = RecipeData.objects.all().order_by('name')
    serializer_class = RecipeSerializer


class IngredientViewSet(viewsets.ModelViewSet):
    queryset = Ingredient.objects.all().order_by('name')
    serializer_class = IngredientSerializer


@ensure_csrf_cookie
def csrf(request):
    return JsonResponse({'csrfToken': get_token(request)})


@ensure_csrf_cookie
def process_recipes(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        contents = data['contents']
        meal_type = data['meal_type']
        all_recipes = RecipeData.objects.all()
        recipe_list = []
        for recipe in all_recipes:
            if recipe.meal_type == meal_type:
                check = all(item in contents for item in recipe.ingredients)
                if check is True:
                    recipe_list.append(recipe)
                else:
                    continue
            else:
                continue
        response = serializers.serialize('json', recipe_list)
        return JsonResponse(response, safe=False)

import json
from django.shortcuts import render, redirect
from .models import RecipeData, Ingredient
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from .forms import IngredientInputForm, SelectMealTypeForm
from django import forms
from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt
from django.core import serializers
from django.middleware.csrf import get_token
from fridge_reciper import settings

from rest_framework import viewsets
from .serializers import RecipeSerializer, IngredientSerializer
from .models import RecipeData


class RecipeViewSet(viewsets.ModelViewSet):
    queryset = RecipeData.objects.all().order_by('name')
    serializer_class = RecipeSerializer


class IngredientViewSet(viewsets.ModelViewSet):
    queryset = Ingredient.objects.all().order_by('name')
    serializer_class = IngredientSerializer


@ensure_csrf_cookie
def csrf(request):
    return JsonResponse({'csrfToken': get_token(request)})


@ensure_csrf_cookie
def process_recipes(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        # extract contents and meal type from POST data
        contents = data["contents"]
        meal_type = data["meal_type"]
        # search for recipes
        all_recipes = RecipeData.objects.all()
        recipe_list = []
        for recipe in all_recipes:
            if recipe.meal_type == meal_type:
                check = all(item in contents for item in recipe.ingredients)
                if check is True:
                    recipe_list.append(recipe)
                else:
                    continue
            else:
                continue

        response = serializers.serialize("json", recipe_list)

        return JsonResponse(response, safe=False)
