Add cook later

This commit is contained in:
Joren de Graaf
2026-10-15 19:58:33 +02:00
parent b72283818c
commit f36f05b9bc
27 changed files with 523 additions and 129 deletions
+34 -10
View File
@@ -18,8 +18,9 @@ from PIL import Image
from .forms import (RecipeForm, RecipeIngredientFormSet, RecipeSubrecipeFormSet,
RecipeSubstitutionForm)
from .models import (Recipe, RecipeIngredient, RecipeIngredientSubstitution,
from .models import (CookLater, Recipe, RecipeIngredient, RecipeIngredientSubstitution,
RecipeSelection, RecipeSubrecipe, RecipeUse)
from .services import cook_later
from .services.analysis import AnalysisError, analyze
from .services.cooking import CookingError, complete, instruction_steps, make_completion
from .services.stock_use import RecipeStockError, confirm, make_confirmation, reverse_use
@@ -45,8 +46,7 @@ def _match_recipes(user):
@login_required
def match(request):
demo = _match_demo_user()
recipes = _match_recipes(request.user).exclude(match_selections__user=request.user)
recipes = _match_recipes(request.user)
query = request.GET.get('q', '').strip()[:100]
category = request.GET.get('category', '').strip()[:80]
tag = request.GET.get('tag', '').strip()[:40]
@@ -70,7 +70,7 @@ def match(request):
return render(request, 'recipes/match.html', {
'recipes': recipes, 'query': query, 'category': category, 'tag': tag,
'time_limit': time_limit, 'categories': categories, 'tags': tags,
'demo_ready': bool(demo and RecipeSelection.objects.filter(user=demo).exists()),
'saved_ids': set(CookLater.objects.filter(user=request.user).values_list('recipe_id', flat=True)),
})
@@ -86,12 +86,34 @@ def match_reset(request):
@require_POST
def match_select(request, identifier):
recipe = get_object_or_404(_match_recipes(request.user), api_id=identifier)
RecipeSelection.objects.get_or_create(user=request.user, recipe=recipe)
demo = _match_demo_user()
matched = bool(demo and demo.pk != request.user.pk and
RecipeSelection.objects.filter(user=demo, recipe=recipe).exists())
return JsonResponse({'matched': matched, 'recipe_id': str(recipe.api_id),
'title': recipe.title})
result = cook_later.add(request.user, recipe.api_id)
return JsonResponse({**result, 'title': recipe.title})
@login_required
def cook_later_list(request):
return render(request, 'recipes/cook_later.html', {
'entries': CookLater.objects.filter(user=request.user, recipe__in=_match_recipes(request.user))
.select_related('recipe'),
})
@login_required
@require_POST
def cook_later_remove(request, identifier):
try:
cook_later.remove(request.user, identifier)
except Recipe.DoesNotExist:
raise Http404
messages.success(request, 'Removed from Cook Later.')
return redirect('recipes:cook-later')
@login_required
def discover_detail(request, identifier):
recipe = get_object_or_404(_match_recipes(request.user).prefetch_related('ingredients'),
api_id=identifier)
return render(request, 'recipes/discover_detail.html', {'recipe': recipe})
def _library(request, selected=None):
@@ -112,6 +134,7 @@ def _library(request, selected=None):
if tag:
recipes = [recipe for recipe in recipes if tag in recipe.tags]
recipes = recipes[:100]
saved_ids = set(CookLater.objects.filter(user=request.user).values_list('recipe_id', flat=True))
if selected is None and recipes:
selected = recipes[0]
analysis = None
@@ -134,6 +157,7 @@ def _library(request, selected=None):
'category': category, 'tag': tag, 'favourite': favourite,
'categories': categories, 'tags': tags, 'explicit_detail': explicit_detail,
'analysis': analysis, 'analysis_error': analysis_error, 'last_use': last_use,
'saved_ids': saved_ids,
})