refactor
This commit is contained in:
parent
45b5d9d0eb
commit
f38fb8d410
|
|
@ -6,6 +6,10 @@ from django.urls import reverse
|
||||||
from django.http.response import HttpResponseRedirect
|
from django.http.response import HttpResponseRedirect
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
RECIPE_FORM_PREFIX = 'recipe'
|
||||||
|
VERSION_FORM_PREFIX = 'version'
|
||||||
|
INGREDIENTS_FORMSET_PREFIX = 'ingredient'
|
||||||
|
|
||||||
def get_name_of_user(user):
|
def get_name_of_user(user):
|
||||||
return user.first_name if user.first_name else user.username
|
return user.first_name if user.first_name else user.username
|
||||||
|
|
||||||
|
|
@ -32,9 +36,17 @@ def recipe(request, slug):
|
||||||
else:
|
else:
|
||||||
return render(request, 'recipe.html', context={'recipe': recipe, 'versions': versions})
|
return render(request, 'recipe.html', context={'recipe': recipe, 'versions': versions})
|
||||||
|
|
||||||
RECIPE_FORM_PREFIX = 'recipe'
|
def create_version(request, recipe: Recipe, version_form: VersionForm) -> Version:
|
||||||
VERSION_FORM_PREFIX = 'version'
|
version = version_form.save(commit=False)
|
||||||
INGREDIENTS_FORMSET_PREFIX = 'ingredient'
|
version.user = request.user
|
||||||
|
version.recipe = recipe
|
||||||
|
version.save()
|
||||||
|
return version
|
||||||
|
|
||||||
|
def save_ingredients(version: Version, ingredients_formset: IngredientFormSet): # type: ignore
|
||||||
|
for ingredient in ingredients_formset.save(commit=False):
|
||||||
|
ingredient.version = version
|
||||||
|
ingredient.save()
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def add_recipe(request):
|
def add_recipe(request):
|
||||||
|
|
@ -45,15 +57,8 @@ def add_recipe(request):
|
||||||
|
|
||||||
if recipe_form.is_valid() and version_form.is_valid() and ingredients_formset.is_valid():
|
if recipe_form.is_valid() and version_form.is_valid() and ingredients_formset.is_valid():
|
||||||
recipe = recipe_form.save(commit=True)
|
recipe = recipe_form.save(commit=True)
|
||||||
|
version = create_version(request, recipe, version_form)
|
||||||
version = version_form.save(commit=False)
|
save_ingredients(version, ingredients_formset)
|
||||||
version.user = request.user
|
|
||||||
version.recipe = recipe
|
|
||||||
version.save()
|
|
||||||
|
|
||||||
for ingredient in ingredients_formset.save(commit=False):
|
|
||||||
ingredient.version = version
|
|
||||||
ingredient.save()
|
|
||||||
|
|
||||||
return HttpResponseRedirect(reverse('recipe', kwargs={'slug': recipe.slug}))
|
return HttpResponseRedirect(reverse('recipe', kwargs={'slug': recipe.slug}))
|
||||||
else:
|
else:
|
||||||
|
|
@ -92,14 +97,8 @@ def add_version(request, slug):
|
||||||
version_form.recipe_id = recipe.id # type: ignore
|
version_form.recipe_id = recipe.id # type: ignore
|
||||||
|
|
||||||
if version_form.is_valid() and ingredients_formset.is_valid():
|
if version_form.is_valid() and ingredients_formset.is_valid():
|
||||||
version = version_form.save(commit=False)
|
version = create_version(request, recipe, version_form)
|
||||||
version.user = request.user
|
save_ingredients(version, ingredients_formset)
|
||||||
version.recipe = recipe
|
|
||||||
version.save()
|
|
||||||
|
|
||||||
for ingredient in ingredients_formset.save(commit=False):
|
|
||||||
ingredient.version = version
|
|
||||||
ingredient.save()
|
|
||||||
|
|
||||||
return HttpResponseRedirect(reverse('version', kwargs={'slug_recipe': version.recipe.slug, 'slug_version': version.slug}))
|
return HttpResponseRedirect(reverse('version', kwargs={'slug_recipe': version.recipe.slug, 'slug_version': version.slug}))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue