added ingredient merging

This commit is contained in:
vabene1111 2020-02-18 22:25:21 +01:00
parent 04cbe6cb2c
commit 46dffe2f63
3 changed files with 57 additions and 14 deletions

View File

@ -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): class CommentForm(forms.ModelForm):
prefix = 'comment' prefix = 'comment'

View File

@ -7,7 +7,7 @@
{% block title %}{% trans "Cookbook" %}{% endblock %} {% block title %}{% trans "Cookbook" %}{% endblock %}
{% block extra_head %} {% block extra_head %}
{{ form.media }} {{ units_form.media }}
{% endblock %} {% endblock %}
{% block content %} {% block content %}
@ -24,11 +24,19 @@
<h4>{% trans 'Units' %}</h4> <h4>{% trans 'Units' %}</h4>
<form action="{% url 'edit_ingredient' %}" method="post"> <form action="{% url 'edit_ingredient' %}" method="post">
{% csrf_token %} {% csrf_token %}
{{ form|crispy }} {{ units_form|crispy }}
<button class="btn btn-danger" type="submit" <button class="btn btn-danger" type="submit"
onclick="confirm('{% trans 'Are you sure that you want to merge these two units ?' %}')"><i onclick="confirm('{% trans 'Are you sure that you want to merge these two units ?' %}')"><i
class="fas fa-sync-alt"></i> {% trans 'Merge' %}</button> class="fas fa-sync-alt"></i> {% trans 'Merge' %}</button>
</form> </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 %} {% endblock %}

View File

@ -15,7 +15,7 @@ from django.utils.translation import gettext as _, ngettext
from django.views.generic import UpdateView, DeleteView from django.views.generic import UpdateView, DeleteView
from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm, CommentForm, \ 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, \ from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage, Comment, RecipeIngredient, RecipeBook, \
RecipeBookEntry, MealPlan, Unit, Ingredient RecipeBookEntry, MealPlan, Unit, Ingredient
from cookbook.provider.dropbox import Dropbox from cookbook.provider.dropbox import Dropbox
@ -302,23 +302,41 @@ class RecipeUpdate(LoginRequiredMixin, UpdateView):
@login_required @login_required
def edit_ingredients(request): def edit_ingredients(request):
if request.method == "POST": if request.method == "POST":
form = UnitMergeForm(request.POST, prefix=UnitMergeForm.prefix) success = False
if form.is_valid(): units_form = UnitMergeForm(request.POST, prefix=UnitMergeForm.prefix)
new_unit = form.cleaned_data['new_unit'] if units_form.is_valid():
old_unit = form.cleaned_data['old_unit'] new_unit = units_form.cleaned_data['new_unit']
ingredients = RecipeIngredient.objects.filter(unit=old_unit).all() old_unit = units_form.cleaned_data['old_unit']
for i in ingredients: recipe_ingredients = RecipeIngredient.objects.filter(unit=old_unit).all()
for i in recipe_ingredients:
i.unit = new_unit i.unit = new_unit
i.save() i.save()
old_unit.delete() old_unit.delete()
success = True
messages.add_message(request, messages.SUCCESS, _('Units merged!')) 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 # Generic Delete views