Recipes phase 2
This commit is contained in:
+27
-1
@@ -4,10 +4,11 @@ from django.core.files.uploadedfile import UploadedFile
|
||||
from PIL import Image, UnidentifiedImageError
|
||||
|
||||
from food.models import FoodProduct
|
||||
from .models import Recipe, RecipeIngredient
|
||||
from .models import Recipe, RecipeIngredient, RecipeSubrecipe
|
||||
|
||||
|
||||
class RecipeForm(forms.ModelForm):
|
||||
markdown_revision = forms.IntegerField(min_value=0, widget=forms.HiddenInput)
|
||||
tags_text = forms.CharField(required=False, label='Tags',
|
||||
help_text='Separate tags with commas.')
|
||||
remove_photo = forms.BooleanField(required=False, label='Remove current photo')
|
||||
@@ -32,6 +33,7 @@ class RecipeForm(forms.ModelForm):
|
||||
|
||||
def __init__(self, *args, user, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['markdown_revision'].initial = self.instance.markdown_revision
|
||||
self.fields['parent'].queryset = Recipe.objects.filter(owner=user).exclude(pk=self.instance.pk)
|
||||
if self.instance.pk:
|
||||
self.fields['tags_text'].initial = ', '.join(self.instance.tags)
|
||||
@@ -94,3 +96,27 @@ RecipeIngredientFormSet = inlineformset_factory(
|
||||
Recipe, RecipeIngredient, form=RecipeIngredientForm,
|
||||
formset=OwnedIngredientFormSet, extra=2, can_delete=True,
|
||||
)
|
||||
|
||||
|
||||
class RecipeSubrecipeForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = RecipeSubrecipe
|
||||
fields = ('component', 'quantity', 'unit', 'note', 'display_order')
|
||||
widgets = {'quantity': forms.NumberInput(attrs={'min': '.001', 'step': '.001'}),
|
||||
'display_order': forms.NumberInput(attrs={'min': 0})}
|
||||
|
||||
|
||||
class OwnedSubrecipeFormSet(BaseInlineFormSet):
|
||||
def __init__(self, *args, user, **kwargs):
|
||||
self.user = user
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def add_fields(self, form, index):
|
||||
super().add_fields(form, index)
|
||||
form.fields['component'].queryset = Recipe.objects.filter(owner=self.user).exclude(pk=self.instance.pk)
|
||||
|
||||
|
||||
RecipeSubrecipeFormSet = inlineformset_factory(
|
||||
Recipe, RecipeSubrecipe, form=RecipeSubrecipeForm,
|
||||
formset=OwnedSubrecipeFormSet, fk_name='recipe', extra=1, can_delete=True,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user