quantity fields in food log and recipe upload drop zone
This commit is contained in:
+21
-17
@@ -27,7 +27,7 @@ class RecipeForm(forms.ModelForm):
|
||||
'description': forms.Textarea(attrs={'rows': 3}),
|
||||
'instructions_markdown': forms.Textarea(attrs={'rows': 10}),
|
||||
'notes_markdown': forms.Textarea(attrs={'rows': 4}),
|
||||
'photo': forms.ClearableFileInput(attrs={'accept': 'image/jpeg,image/png,image/webp'}),
|
||||
'photo': forms.FileInput(attrs={'accept': 'image/jpeg,image/png,image/webp'}),
|
||||
'preparation_minutes': forms.NumberInput(attrs={'min': 0}),
|
||||
'cooking_minutes': forms.NumberInput(attrs={'min': 0}),
|
||||
'default_servings': forms.NumberInput(attrs={'min': '.01', 'step': '.01'}),
|
||||
@@ -48,22 +48,7 @@ class RecipeForm(forms.ModelForm):
|
||||
|
||||
def clean_photo(self):
|
||||
photo = self.cleaned_data.get('photo')
|
||||
if not isinstance(photo, UploadedFile):
|
||||
return photo
|
||||
if photo.size > 5 * 1024 * 1024:
|
||||
raise forms.ValidationError('Choose an image smaller than 5 MB.')
|
||||
if photo.content_type not in ('image/jpeg', 'image/png', 'image/webp'):
|
||||
raise forms.ValidationError('Choose a JPEG, PNG, or WebP image.')
|
||||
try:
|
||||
image = Image.open(photo)
|
||||
if image.format not in ('JPEG', 'PNG', 'WEBP') or image.width * image.height > 25_000_000:
|
||||
raise forms.ValidationError('Choose a valid image smaller than 25 megapixels.')
|
||||
image.verify()
|
||||
except (UnidentifiedImageError, OSError):
|
||||
raise forms.ValidationError('Choose a valid JPEG, PNG, or WebP image.') from None
|
||||
finally:
|
||||
photo.seek(0)
|
||||
return photo
|
||||
return validate_recipe_photo(photo)
|
||||
|
||||
def save(self, commit=True):
|
||||
recipe = super().save(commit=False)
|
||||
@@ -73,6 +58,25 @@ class RecipeForm(forms.ModelForm):
|
||||
return recipe
|
||||
|
||||
|
||||
def validate_recipe_photo(photo):
|
||||
if not isinstance(photo, UploadedFile):
|
||||
return photo
|
||||
if photo.size > 5 * 1024 * 1024:
|
||||
raise forms.ValidationError('Choose an image smaller than 5 MB.')
|
||||
if photo.content_type not in ('image/jpeg', 'image/png', 'image/webp'):
|
||||
raise forms.ValidationError('Choose a JPEG, PNG, or WebP image.')
|
||||
try:
|
||||
image = Image.open(photo)
|
||||
if image.format not in ('JPEG', 'PNG', 'WEBP') or image.width * image.height > 25_000_000:
|
||||
raise forms.ValidationError('Choose a valid image smaller than 25 megapixels.')
|
||||
image.verify()
|
||||
except (UnidentifiedImageError, OSError):
|
||||
raise forms.ValidationError('Choose a valid JPEG, PNG, or WebP image.') from None
|
||||
finally:
|
||||
photo.seek(0)
|
||||
return photo
|
||||
|
||||
|
||||
class RecipeIngredientForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = RecipeIngredient
|
||||
|
||||
Reference in New Issue
Block a user