Recipes phase 4
This commit is contained in:
@@ -9,12 +9,16 @@ from django.http import FileResponse, Http404, HttpResponseBadRequest, JsonRespo
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.urls import reverse
|
||||
from django.views.decorators.http import require_http_methods, require_POST
|
||||
from django.utils.dateparse import parse_date
|
||||
from planner.timezones import localdate_for_user
|
||||
from food.models import MealType
|
||||
from PIL import Image
|
||||
|
||||
from .forms import (RecipeForm, RecipeIngredientFormSet, RecipeSubrecipeFormSet,
|
||||
RecipeSubstitutionForm)
|
||||
from .models import Recipe, RecipeIngredient, RecipeIngredientSubstitution, RecipeSubrecipe, RecipeUse
|
||||
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
|
||||
|
||||
|
||||
@@ -77,6 +81,87 @@ def detail(request, identifier):
|
||||
return _library(request, recipe)
|
||||
|
||||
|
||||
def _cooking_analysis(request, recipe):
|
||||
selected = {key[4:]: value for key, value in request.GET.items()
|
||||
if key.startswith('sub_') and value}
|
||||
return analyze(recipe, request.user, request.GET.get('servings'),
|
||||
include_optional=request.GET.get('optional') == '1',
|
||||
substitutions=selected)
|
||||
|
||||
|
||||
@login_required
|
||||
def cook(request, identifier):
|
||||
recipe = get_object_or_404(_owned(request.user), api_id=identifier)
|
||||
try:
|
||||
result = _cooking_analysis(request, recipe)
|
||||
error = ''
|
||||
except AnalysisError as exc:
|
||||
error = str(exc)
|
||||
result = analyze(recipe, request.user, recipe.default_servings)
|
||||
components = []
|
||||
|
||||
def gather(current, scale, ancestors, depth):
|
||||
if depth > 8 or current.pk in ancestors:
|
||||
return
|
||||
for item in current.subrecipes.select_related('component'):
|
||||
component = item.component
|
||||
if item.unit == 'servings':
|
||||
quantity = item.quantity * scale
|
||||
child_scale = quantity / component.default_servings
|
||||
elif component.yield_amount and component.yield_unit == item.unit:
|
||||
quantity = item.quantity * scale
|
||||
child_scale = quantity / component.yield_amount
|
||||
else:
|
||||
continue
|
||||
components.append({'title': component.title, 'quantity': quantity,
|
||||
'unit': item.unit, 'depth': depth,
|
||||
'steps': instruction_steps(component.instructions_markdown)})
|
||||
gather(component, child_scale, ancestors | {current.pk}, depth + 1)
|
||||
|
||||
gather(recipe, result['servings'] / recipe.default_servings, set(), 1)
|
||||
return render(request, 'recipes/cook.html', {'recipe': recipe, 'analysis': result,
|
||||
'error': error, 'steps': instruction_steps(recipe.instructions_markdown),
|
||||
'components': components})
|
||||
|
||||
|
||||
@login_required
|
||||
@require_http_methods(['GET', 'POST'])
|
||||
def cook_finish(request, identifier):
|
||||
recipe = get_object_or_404(_owned(request.user), api_id=identifier)
|
||||
if request.method == 'POST':
|
||||
log_food = request.POST.get('log_food') == '1'
|
||||
day_text = request.POST.get('date', '')
|
||||
day = parse_date(day_text) if day_text else None
|
||||
if log_food and (day is None or day.isoformat() != day_text):
|
||||
messages.error(request, 'Choose a valid Food Diary date.')
|
||||
return redirect('recipes:cook-finish', identifier=identifier)
|
||||
try:
|
||||
run = complete(
|
||||
request.user, recipe, request.POST.get('confirmation', ''),
|
||||
log_food=log_food,
|
||||
deduct_stock=request.POST.get('deduct_stock') == '1',
|
||||
consumed_servings=request.POST.get('consumed_servings'),
|
||||
meal=request.POST.get('meal'), day=day)
|
||||
except CookingError as exc:
|
||||
messages.error(request, str(exc))
|
||||
return redirect('recipes:cook-finish', identifier=identifier)
|
||||
messages.success(request, 'Cooking completion saved.')
|
||||
if run.food_logged_at:
|
||||
return redirect(f'{reverse("food:diary")}?date={day.isoformat()}')
|
||||
return redirect('recipes:detail', identifier=recipe.api_id)
|
||||
try:
|
||||
result = _cooking_analysis(request, recipe)
|
||||
error = ''
|
||||
except AnalysisError as exc:
|
||||
error = str(exc)
|
||||
result = analyze(recipe, request.user, recipe.default_servings)
|
||||
return render(request, 'recipes/cook_finish.html', {
|
||||
'recipe': recipe, 'analysis': result, 'error': error,
|
||||
'confirmation': make_completion(result) if not error else '',
|
||||
'today': localdate_for_user(request.user), 'meals': MealType.choices,
|
||||
})
|
||||
|
||||
|
||||
@login_required
|
||||
def stock_preview(request, identifier):
|
||||
recipe = get_object_or_404(_owned(request.user), api_id=identifier)
|
||||
|
||||
Reference in New Issue
Block a user