TandoorRecipes/cookbook/views/edit.py
2018-03-30 22:41:23 +02:00

51 lines
1.8 KiB
Python

from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import redirect, render
from django.urls import reverse_lazy
from django.utils.translation import gettext as _
from django.views.generic import UpdateView
from cookbook.forms import EditRecipeForm
from cookbook.models import Recipe, Category, Monitor, Keyword
class MonitorUpdate(LoginRequiredMixin, UpdateView):
template_name = "generic\edit_template.html"
model = Monitor
fields = ['path']
class CategoryUpdate(LoginRequiredMixin, UpdateView):
template_name = "generic\edit_template.html"
model = Category
fields = ['name', 'description']
class KeywordUpdate(LoginRequiredMixin, UpdateView):
template_name = "generic\edit_template.html"
model = Keyword
fields = ['name', 'description']
@login_required
def recipe(request, recipe_id):
recipe_obj = Recipe.objects.get(id=recipe_id)
if request.method == "POST":
form = EditRecipeForm(request.POST)
if form.is_valid():
recipe_obj.name = form.cleaned_data['name']
recipe_obj.path = form.cleaned_data['path']
recipe_obj.category = Category.objects.get(name=form.cleaned_data['category'])
recipe_obj.keywords.clear()
recipe_obj.keywords.add(*list(form.cleaned_data['keywords']))
recipe_obj.save()
messages.add_message(request, messages.SUCCESS, _('Recipe updated'))
return redirect(reverse_lazy('edit_recipe', args=[recipe_id]))
else:
messages.add_message(request, messages.ERROR, _('Recipe update failed'))
else:
form = EditRecipeForm(instance=recipe_obj)
return render(request, 'edit/recipe.html', {'form': form})