added ingredient merging
This commit is contained in:
parent
04cbe6cb2c
commit
46dffe2f63
@ -97,6 +97,23 @@ class UnitMergeForm(forms.Form):
|
||||
)
|
||||
|
||||
|
||||
class IngredientMergeForm(forms.Form):
|
||||
prefix = 'ingredient'
|
||||
|
||||
new_ingredient = forms.ModelChoiceField(
|
||||
queryset=Ingredient.objects.all(),
|
||||
widget=SelectWidget,
|
||||
label=_('New Ingredient'),
|
||||
help_text=_('New ingredient that other gets replaced by.'),
|
||||
)
|
||||
old_ingredient = forms.ModelChoiceField(
|
||||
queryset=Ingredient.objects.all(),
|
||||
widget=SelectWidget,
|
||||
label=_('Old Ingredient'),
|
||||
help_text=_('Ingredient that should be replaced.'),
|
||||
)
|
||||
|
||||
|
||||
class CommentForm(forms.ModelForm):
|
||||
prefix = 'comment'
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
{% block title %}{% trans "Cookbook" %}{% endblock %}
|
||||
|
||||
{% block extra_head %}
|
||||
{{ form.media }}
|
||||
{{ units_form.media }}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
@ -24,11 +24,19 @@
|
||||
<h4>{% trans 'Units' %}</h4>
|
||||
<form action="{% url 'edit_ingredient' %}" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form|crispy }}
|
||||
{{ units_form|crispy }}
|
||||
<button class="btn btn-danger" type="submit"
|
||||
onclick="confirm('{% trans 'Are you sure that you want to merge these two units ?' %}')"><i
|
||||
class="fas fa-sync-alt"></i> {% trans 'Merge' %}</button>
|
||||
</form>
|
||||
|
||||
<h4>{% trans 'Ingredients' %}</h4>
|
||||
<form action="{% url 'edit_ingredient' %}" method="post">
|
||||
{% csrf_token %}
|
||||
{{ ingredients_form|crispy }}
|
||||
<button class="btn btn-danger" type="submit"
|
||||
onclick="confirm('{% trans 'Are you sure that you want to merge these two ingredients ?' %}')"><i
|
||||
class="fas fa-sync-alt"></i> {% trans 'Merge' %}</button>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
@ -15,7 +15,7 @@ from django.utils.translation import gettext as _, ngettext
|
||||
from django.views.generic import UpdateView, DeleteView
|
||||
|
||||
from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm, CommentForm, \
|
||||
MealPlanForm, UnitMergeForm
|
||||
MealPlanForm, UnitMergeForm, IngredientMergeForm
|
||||
from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage, Comment, RecipeIngredient, RecipeBook, \
|
||||
RecipeBookEntry, MealPlan, Unit, Ingredient
|
||||
from cookbook.provider.dropbox import Dropbox
|
||||
@ -302,23 +302,41 @@ class RecipeUpdate(LoginRequiredMixin, UpdateView):
|
||||
@login_required
|
||||
def edit_ingredients(request):
|
||||
if request.method == "POST":
|
||||
form = UnitMergeForm(request.POST, prefix=UnitMergeForm.prefix)
|
||||
if form.is_valid():
|
||||
new_unit = form.cleaned_data['new_unit']
|
||||
old_unit = form.cleaned_data['old_unit']
|
||||
ingredients = RecipeIngredient.objects.filter(unit=old_unit).all()
|
||||
for i in ingredients:
|
||||
success = False
|
||||
units_form = UnitMergeForm(request.POST, prefix=UnitMergeForm.prefix)
|
||||
if units_form.is_valid():
|
||||
new_unit = units_form.cleaned_data['new_unit']
|
||||
old_unit = units_form.cleaned_data['old_unit']
|
||||
recipe_ingredients = RecipeIngredient.objects.filter(unit=old_unit).all()
|
||||
for i in recipe_ingredients:
|
||||
i.unit = new_unit
|
||||
i.save()
|
||||
|
||||
old_unit.delete()
|
||||
success = True
|
||||
messages.add_message(request, messages.SUCCESS, _('Units merged!'))
|
||||
else:
|
||||
messages.add_message(request, messages.WARNING, _('There was an error in your form.'))
|
||||
else:
|
||||
form = UnitMergeForm()
|
||||
|
||||
return render(request, 'forms/ingredients.html', {'form': form})
|
||||
ingredients_form = IngredientMergeForm(request.POST, prefix=IngredientMergeForm.prefix)
|
||||
if ingredients_form.is_valid():
|
||||
new_ingredient = ingredients_form.cleaned_data['new_ingredient']
|
||||
old_ingredient = ingredients_form.cleaned_data['old_ingredient']
|
||||
recipe_ingredients = RecipeIngredient.objects.filter(ingredient=old_ingredient).all()
|
||||
for i in recipe_ingredients:
|
||||
i.ingredient = new_ingredient
|
||||
i.save()
|
||||
|
||||
old_ingredient.delete()
|
||||
success = True
|
||||
messages.add_message(request, messages.SUCCESS, _('Ingredients merged!'))
|
||||
|
||||
if success:
|
||||
units_form = UnitMergeForm()
|
||||
ingredients_form = IngredientMergeForm()
|
||||
else:
|
||||
units_form = UnitMergeForm()
|
||||
ingredients_form = IngredientMergeForm()
|
||||
|
||||
return render(request, 'forms/ingredients.html', {'units_form': units_form, 'ingredients_form': ingredients_form})
|
||||
|
||||
|
||||
# Generic Delete views
|
||||
|
Loading…
Reference in New Issue
Block a user