quantity fields in food log and recipe upload drop zone
This commit is contained in:
+26
-1
@@ -2,6 +2,7 @@ import json
|
||||
import uuid
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.decorators import login_required
|
||||
@@ -17,7 +18,7 @@ from food.models import MealType
|
||||
from PIL import Image
|
||||
|
||||
from .forms import (RecipeForm, RecipeIngredientFormSet, RecipeSubrecipeFormSet,
|
||||
RecipeSubstitutionForm)
|
||||
RecipeSubstitutionForm, validate_recipe_photo)
|
||||
from .models import (CookLater, Recipe, RecipeIngredient, RecipeIngredientSubstitution,
|
||||
RecipeSelection, RecipeSubrecipe, RecipeUse)
|
||||
from .services import cook_later
|
||||
@@ -364,6 +365,7 @@ def _edit(request, recipe=None, allow_draft_resume=False):
|
||||
return render(request, 'recipes/form.html', {
|
||||
'form': form, 'ingredients': ingredients, 'subrecipes': subrecipes, 'recipe': recipe,
|
||||
'creating': creating, 'new_draft_id': recipe.api_id if creating else None,
|
||||
'existing_photo': bool(old_photo),
|
||||
})
|
||||
|
||||
|
||||
@@ -389,6 +391,29 @@ def edit(request, identifier):
|
||||
return _edit(request, get_object_or_404(_owned(request.user), api_id=identifier))
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def photo_upload(request, identifier):
|
||||
recipe = get_object_or_404(_owned(request.user), api_id=identifier)
|
||||
uploaded = request.FILES.get('photo')
|
||||
if uploaded is None:
|
||||
messages.error(request, 'Choose a photo to upload.')
|
||||
return redirect('recipes:detail', identifier=identifier)
|
||||
try:
|
||||
validate_recipe_photo(uploaded)
|
||||
except ValidationError as exc:
|
||||
messages.error(request, exc.messages[0])
|
||||
return redirect('recipes:detail', identifier=identifier)
|
||||
old_name = recipe.photo.name if recipe.photo else ''
|
||||
storage = recipe.photo.storage
|
||||
recipe.photo = uploaded
|
||||
recipe.save(update_fields=('photo', 'updated_at'))
|
||||
if old_name and old_name != recipe.photo.name:
|
||||
transaction.on_commit(lambda: storage.delete(old_name))
|
||||
messages.success(request, 'Recipe photo uploaded.')
|
||||
return redirect('recipes:detail', identifier=identifier)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def autosave(request, identifier):
|
||||
|
||||
Reference in New Issue
Block a user