import process

This commit is contained in:
vabene1111
2018-05-06 22:12:40 +02:00
parent 8d4506da54
commit c9f0192a2c
8 changed files with 76 additions and 52 deletions

View File

@ -1,8 +1,12 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.utils.translation import gettext as _
from django.views.generic import CreateView
from cookbook.forms import ImportRecipeForm, NewRecipe
from cookbook.models import Category, Keyword, Recipe
@ -12,6 +16,9 @@ class RecipeCreate(LoginRequiredMixin, CreateView): # this exists for completen
fields = ['name', 'category', 'keywords']
success_url = reverse_lazy('list_category')
def get_initial(self):
return {'value1': self.request.user}
def get_context_data(self, **kwargs):
context = super(RecipeCreate, self).get_context_data(**kwargs)
context['title'] = _("Recipe")
@ -40,3 +47,30 @@ class KeywordCreate(LoginRequiredMixin, CreateView):
context = super(KeywordCreate, self).get_context_data(**kwargs)
context['title'] = _("Keyword")
return context
@login_required
def create_new_recipe(request, import_id):
if request.method == "POST":
form = ImportRecipeForm(request.POST)
if form.is_valid():
recipe = Recipe()
recipe.name = form.cleaned_data['name']
recipe.path = form.cleaned_data['path']
recipe.category = form.cleaned_data['category']
recipe.save()
recipe.keywords.set(form.cleaned_data['keywords'])
NewRecipe.objects.get(id=import_id).delete()
messages.add_message(request, messages.SUCCESS, _('Imported new recipe!'))
return redirect('list_import')
else:
messages.add_message(request, messages.ERROR, _('There was an error importing this recipe!'))
else:
new_recipe = NewRecipe.objects.get(id=import_id)
form = ImportRecipeForm(initial={'path': new_recipe.path, 'name': new_recipe.name})
return render(request, 'forms/edit_import_recipe.html', {'form': form})