Recipes phase 6
This commit is contained in:
+70
-1
@@ -1,7 +1,9 @@
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
@@ -16,7 +18,8 @@ from PIL import Image
|
||||
|
||||
from .forms import (RecipeForm, RecipeIngredientFormSet, RecipeSubrecipeFormSet,
|
||||
RecipeSubstitutionForm)
|
||||
from .models import Recipe, RecipeIngredient, RecipeIngredientSubstitution, RecipeSubrecipe, RecipeUse
|
||||
from .models import (Recipe, RecipeIngredient, RecipeIngredientSubstitution,
|
||||
RecipeSelection, 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
|
||||
@@ -27,6 +30,62 @@ def _owned(user):
|
||||
return Recipe.objects.filter(owner=user)
|
||||
|
||||
|
||||
def _match_demo_user():
|
||||
username = settings.RECIPE_MATCH_DEMO_USERNAME
|
||||
return get_user_model().objects.filter(username=username, is_active=True).first() if username else None
|
||||
|
||||
|
||||
def _match_recipes(user):
|
||||
demo = _match_demo_user()
|
||||
visible = Q(owner=user)
|
||||
if demo and demo.pk != user.pk:
|
||||
visible |= Q(owner=demo, status=Recipe.Status.PUBLISHED)
|
||||
return Recipe.objects.filter(visible)
|
||||
|
||||
|
||||
@login_required
|
||||
def match(request):
|
||||
demo = _match_demo_user()
|
||||
recipes = _match_recipes(request.user).exclude(match_selections__user=request.user)
|
||||
query = request.GET.get('q', '').strip()[:100]
|
||||
category = request.GET.get('category', '').strip()[:80]
|
||||
tag = request.GET.get('tag', '').strip()[:40]
|
||||
time_limit = request.GET.get('time', '').strip()
|
||||
if query:
|
||||
recipes = recipes.filter(Q(title__icontains=query) | Q(description__icontains=query) |
|
||||
Q(ingredients__name__icontains=query)).distinct()
|
||||
if category:
|
||||
recipes = recipes.filter(category=category)
|
||||
if time_limit in {'30', '60'}:
|
||||
recipes = [item for item in recipes if item.total_minutes <= int(time_limit)]
|
||||
else:
|
||||
recipes = list(recipes)
|
||||
if tag:
|
||||
recipes = [item for item in recipes if tag in item.tags]
|
||||
recipes = recipes[:50]
|
||||
visible = _match_recipes(request.user)
|
||||
categories = list(visible.exclude(category='').values_list('category', flat=True).distinct().order_by('category'))
|
||||
tags = sorted({name for values in visible.values_list('tags', flat=True)
|
||||
if isinstance(values, list) for name in values if isinstance(name, str)})
|
||||
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()),
|
||||
})
|
||||
|
||||
|
||||
@login_required
|
||||
@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})
|
||||
|
||||
|
||||
def _library(request, selected=None):
|
||||
explicit_detail = selected is not None
|
||||
recipes = _owned(request.user).prefetch_related('ingredients')
|
||||
@@ -399,6 +458,16 @@ def toggle_favourite(request, identifier):
|
||||
@login_required
|
||||
def photo(request, identifier):
|
||||
recipe = get_object_or_404(_owned(request.user), api_id=identifier)
|
||||
return _photo_response(recipe)
|
||||
|
||||
|
||||
@login_required
|
||||
def match_photo(request, identifier):
|
||||
recipe = get_object_or_404(_match_recipes(request.user), api_id=identifier)
|
||||
return _photo_response(recipe)
|
||||
|
||||
|
||||
def _photo_response(recipe):
|
||||
if not recipe.photo:
|
||||
from django.http import Http404
|
||||
raise Http404
|
||||
|
||||
Reference in New Issue
Block a user