fixup! Add forms for adding recipes and versions
fix slug uniqueness testing
This commit is contained in:
parent
312c02cd51
commit
d9b1837f66
|
|
@ -2,13 +2,16 @@ from django.forms import ModelForm, BooleanField, ValidationError
|
||||||
from .models import Recipe, Version, Ingredient
|
from .models import Recipe, Version, Ingredient
|
||||||
|
|
||||||
class VersionForm(ModelForm):
|
class VersionForm(ModelForm):
|
||||||
|
recipe_id: int
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Version
|
model = Version
|
||||||
fields = ['label', 'slug', 'body']
|
fields = ['label', 'slug', 'body']
|
||||||
|
|
||||||
def clean_slug(self):
|
def clean_slug(self):
|
||||||
|
recipe = Recipe.objects.get(id=self.recipe_id)
|
||||||
slug = self.cleaned_data['slug']
|
slug = self.cleaned_data['slug']
|
||||||
if Version.objects.filter(slug=slug).exists():
|
if recipe.versions.filter(slug=slug).count() > 0: # type: ignore
|
||||||
raise ValidationError('A recipe version with this slug already exists.')
|
raise ValidationError('A recipe version with this slug already exists.')
|
||||||
return slug
|
return slug
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ def add_version(request, slug_recipe):
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = VersionForm(request.POST)
|
form = VersionForm(request.POST)
|
||||||
|
form.recipe_id = recipe.id # type: ignore
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
version = form.save(commit=False)
|
version = form.save(commit=False)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue